Class: Spidy::CommandLine
- Inherits:
-
Object
- Object
- Spidy::CommandLine
- Defined in:
- lib/spidy/command_line.rb
Overview
spidy shell interface
Instance Method Summary collapse
- #build(name) ⇒ Object
- #build_ruby ⇒ Object
- #build_shell(name) ⇒ Object
- #call(name) ⇒ Object
- #call_stdin_lines(name) ⇒ Object
- #each(name) ⇒ Object
- #each_stdin_lines(name) ⇒ Object
- #eval_call(script) ⇒ Object
- #function ⇒ Object
-
#initialize(definition_file) ⇒ CommandLine
constructor
A new instance of CommandLine.
Constructor Details
#initialize(definition_file) ⇒ CommandLine
Returns a new instance of CommandLine.
17 18 19 20 |
# File 'lib/spidy/command_line.rb', line 17 def initialize(definition_file) @definition_file = definition_file fail 'unloaded spidy' if definition_file.spidy.nil? end |
Instance Method Details
#build(name) ⇒ Object
63 64 65 66 |
# File 'lib/spidy/command_line.rb', line 63 def build(name) File.write("#{name}.sh", build_shell_script(name)) File.write("#{name}.rb", build_ruby_script) end |
#build_ruby ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/spidy/command_line.rb', line 76 def build_ruby " # frozen_string_literal: true\n\n Spidy.define do\n spider(as: :html) do |yielder, connector|\n # connector.call(url) do |resource|\n # yielder.call(url or resource)\n # end\n end\n\n define(as: :html) do\n end\n end\n RUBY\nend\n" |
#build_shell(name) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/spidy/command_line.rb', line 68 def build_shell(name) " #!/bin/bash\n eval \"$(spidy $(dirname \"${0}\")/\#{name}.rb shell)\"\n spider\n SHELL\nend\n" |
#call(name) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/spidy/command_line.rb', line 38 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
30 31 32 33 34 35 36 |
# File 'lib/spidy/command_line.rb', line 30 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
45 46 47 48 49 50 |
# File 'lib/spidy/command_line.rb', line 45 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
22 23 24 25 26 27 28 |
# File 'lib/spidy/command_line.rb', line 22 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
13 14 15 |
# File 'lib/spidy/command_line.rb', line 13 def eval_call(script) @definition_file.spidy.instance_eval(script) end |
#function ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/spidy/command_line.rb', line 52 def function print " function spider() {\n spidy spider \#{definition_file.path} $1\n }\n function scraper() {\n spidy call \#{definition_file.path} $1\n }\n SHELL\nend\n" |