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 <<~RUBY
      # frozen_string_literal: true

      Botz.define(:#{name}) do
        spider(:example, 'http://example.com') do |html, yielder|
          #  yielder.call(url or resource)
        end

        scraper(:example) do
        end
      end
    RUBY
  end

  File.open("#{name}.sh", 'w') do |f|
    f.write <<~SHELL
      #!/bin/bash
      eval "$(botz $(dirname "${0}")/#{name}.rb shell)"
      spider example
    SHELL
  end
end

#functionObject



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

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

#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