class Spidy::CommandLine

spidy shell interface

Public Class Methods

new(definition_file) click to toggle source
# File lib/spidy/command_line.rb, line 11
def initialize(definition_file)
  @definition_file = definition_file
  raise 'unloaded spidy' if definition_file.spidy.nil?
end

Public Instance Methods

build(name) click to toggle source
# File lib/spidy/command_line.rb, line 61
def build(name)
  build_shell(name)
  build_ruby(name)
end
build_ruby(name) click to toggle source
# File lib/spidy/command_line.rb, line 76
  def build_ruby(name)
    File.open("#{name}.rb", 'w') do |f|
      f.write <<~RUBY
        # frozen_string_literal: true

        Spidy.define do
          spider(:example) do |yielder, connector|
            # connector.call(url) do |resource|
            #   yielder.call(url or resource)
            # end
          end

          define(:example) do
          end
        end
      RUBY
    end
  end
build_shell(name) click to toggle source
# File lib/spidy/command_line.rb, line 66
  def build_shell(name)
    File.open("#{name}.sh", 'w') do |f|
      f.write <<~SHELL
        #!/bin/bash
        eval "$(spidy $(dirname "${0}")/#{name}.rb shell)"
        spider example
      SHELL
    end
  end
call(name) click to toggle source
# File lib/spidy/command_line.rb, line 36
def call(name)
  return call_stdin_lines(name) if FileTest.pipe?(STDIN)
  spidy.call(name: name, &output) unless FileTest.pipe?(STDIN)
rescue => e
  error_handler.call(e, nil)
end
call_stdin_lines(name) click to toggle source
# File lib/spidy/command_line.rb, line 26
def call_stdin_lines(name)
  STDIN.each_line do |url|
    begin
      spidy.call(url.strip, name: name, &output)
    rescue => e
      error_handler.call(e, url)
    end
  end
end
each(name) click to toggle source
# File lib/spidy/command_line.rb, line 43
def each(name)
  return each_stdin_lines(name) if FileTest.pipe?(STDIN)
  spidy.each(name: name, &output)
rescue => e
  error_handler.call(e, nil)
end
each_stdin_lines(name) click to toggle source
# File lib/spidy/command_line.rb, line 16
def each_stdin_lines(name)
  STDIN.each_line do |url|
    begin
      spidy.each(url.strip, name: name, &output)
    rescue => e
      error_handler.call(e, url)
    end
  end
end
function() click to toggle source
# File lib/spidy/command_line.rb, line 50
  def function
    print <<~SHELL
      function spider() {
        spidy spider #{definition_file.path} $1
      }
      function scraper() {
        spidy call #{definition_file.path} $1
      }
    SHELL
  end