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

#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
      STDERR.puts "ERROR #{command} #{url}"
    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