Class: Botz::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/botz/shell.rb

Overview

botz shell interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition_file) ⇒ Shell

Returns a new instance of Shell.



10
11
12
# File 'lib/botz/shell.rb', line 10

def initialize(definition_file)
  @definition_file = definition_file
end

Instance Attribute Details

#definition_fileObject (readonly)

Returns the value of attribute definition_file.



7
8
9
# File 'lib/botz/shell.rb', line 7

def definition_file
  @definition_file
end

Instance Method Details

#build(name) ⇒ Object

rubocop:disable Metrics/MethodLength



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/botz/shell.rb', line 52

def build(name)
  File.open("#{name}.rb", 'w') do |f|
    f.write "      # frozen_string_literal: true\n\n      Botz.define(:\#{name}) do\n        spider(:example, 'http://example.com') do |html, yielder|\n          #  yielder.call(url or resource)\n        end\n\n        scraper(:example) do\n        end\n      end\n    RUBY\n  end\n\n  File.open(\"\#{name}.sh\", 'w') do |f|\n    f.write <<~SHELL\n      #!/bin/bash\n      eval \"$(botz $(dirname \"${0}\")/\#{name}.rb shell)\"\n      spider example\n    SHELL\n  end\nend\n"

#functionObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/botz/shell.rb', line 40

def function
  print "    function spider() {\n      botz spider \#{definition_file.path} $1\n    }\n    function scraper() {\n      botz scraper \#{definition_file.path} $1\n    }\n  SHELL\nend\n"

#scraper(name) ⇒ Object

rubocop:disable Lint/AssignmentInCondition, Style/RescueStandardError



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/botz/shell.rb', line 15

def scraper(name)
  command = scrapers[name.to_sym]
  while line = STDIN.gets
    url = line.strip
    begin
      command.call(url, &definition_file.output)
    rescue => e
      STDERR.puts "ERROR #{e}"
    end
  end
end

#spider(name) ⇒ Object

rubocop:enable Lint/AssignmentInCondition, Style/RescueStandardError



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/botz/shell.rb', line 28

def spider(name)
  command = spiders[name.to_sym]
  if File.pipe?(STDIN)
    STDIN.each_line do |line|
      start_url = line.strip
      command.call(start_url) { |url| puts url }
    end
  else
    command.call { |url| puts url }
  end
end