Class: Spidy::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/spidy/command_line.rb

Overview

spidy shell interface

Instance Method Summary collapse

Constructor Details

#initialize(definition_file) ⇒ CommandLine

Returns a new instance of CommandLine.

[View source]

25
26
27
28
# File 'lib/spidy/command_line.rb', line 25

def initialize(definition_file)
  @definition_file = definition_file
  fail 'unloaded spidy' if definition_file.spidy.nil?
end

Instance Method Details

#build(name) ⇒ Object

[View source]

71
72
73
74
# File 'lib/spidy/command_line.rb', line 71

def build(name)
  File.write("#{name}.sh", build_shell_script(name))
  File.write("#{name}.rb", build_ruby_script)
end

#build_rubyObject

[View source]

84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/spidy/command_line.rb', line 84

def build_ruby
  <<~RUBY
    Spidy.define do
      spider(as: :html) do |yielder, connector|
        # connector.call(url) do |resource|
        #   yielder.call(url or resource)
        # end
      end

      define(as: :html) do
      end
    end
  RUBY
end

#build_shell(name) ⇒ Object

[View source]

76
77
78
79
80
81
82
# File 'lib/spidy/command_line.rb', line 76

def build_shell(name)
  <<~SHELL
    #!/bin/bash
    eval "$(spidy $(dirname "${0}")/#{name}.rb shell)"
    spider
  SHELL
end

#call(name) ⇒ Object

[View source]

46
47
48
49
50
51
# File 'lib/spidy/command_line.rb', line 46

def call(name)
  return call_stdin_lines(name) if FileTest.pipe?($stdin)
  spidy.call(name: name, &output) unless FileTest.pipe?($stdin)
rescue StandardError => e
  error_handler.call(e, nil)
end

#call_stdin_lines(name) ⇒ Object

[View source]

38
39
40
41
42
43
44
# File 'lib/spidy/command_line.rb', line 38

def call_stdin_lines(name)
  $stdin.each_line do |url|
    spidy.call(url.strip, name: name, &output)
  rescue StandardError => e
    error_handler.call(e, url)
  end
end

#each(name) ⇒ Object

[View source]

53
54
55
56
57
58
# File 'lib/spidy/command_line.rb', line 53

def each(name)
  return each_stdin_lines(name) if FileTest.pipe?($stdin)
  spidy.each(name: name, &output)
rescue StandardError => e
  error_handler.call(e, nil)
end

#each_stdin_lines(name) ⇒ Object

[View source]

30
31
32
33
34
35
36
# File 'lib/spidy/command_line.rb', line 30

def each_stdin_lines(name)
  $stdin.each_line do |url|
    spidy.each(url.strip, name: name, &output)
  rescue StandardError => e
    error_handler.call(e, url)
  end
end

#eval_call(script) ⇒ Object

[View source]

21
22
23
# File 'lib/spidy/command_line.rb', line 21

def eval_call(script)
  @definition_file.spidy.instance_eval(script)
end

#functionObject

[View source]

60
61
62
63
64
65
66
67
68
69
# File 'lib/spidy/command_line.rb', line 60

def function
  print <<~SHELL
    function spider() {
      spidy spider #{definition_file.path} $1
    }
    function scraper() {
      spidy call #{definition_file.path} $1
    }
  SHELL
end