Class: EagleClaw::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/eagleclaw/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



10
11
12
# File 'lib/eagleclaw/runner.rb', line 10

def initialize
  @options = OpenStruct.new(:require => [], :format => :json)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/eagleclaw/runner.rb', line 8

def options
  @options
end

Instance Method Details

#parse!(args = ARGV) ⇒ Object



36
37
38
# File 'lib/eagleclaw/runner.rb', line 36

def parse!(args = ARGV)
  parser.parse!(args)
end

#parserObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/eagleclaw/runner.rb', line 14

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options] ScraperName"
    
    # ---- MAIN ----
    
    opts.on('-r', '--require LIBRARY',
            "Require LIBRARY before loading scrapers") do |lib|
      options.require << lib
    end
    
    opts.on('-o', '--output FORMAT',
            "Output data in format FORMAT (default: JSON)") do |format|
      options.format = format.downcase.to_sym
    end
    
    # ---- TAIL ----
    
    opts.on_tail('-h', '--help', "Show this help message") { puts opts and exit }
  end
end

#run!(args = ARGV) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/eagleclaw/runner.rb', line 40

def run!(args = ARGV)
  parse!(args)
  @options.require.each { |lib| require(lib) }
  formatter = EagleClaw::Formatters[@options.format]
  
  scraper = EagleClaw::Scrapers[args.shift.downcase.to_sym].new
  scraper.run
  data, problems = scraper.data, scraper.problems
  output = formatter.format(data, problems)
  puts output
end