Class: Scrape::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/scrape/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, argv = "") ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scrape/cli.rb', line 7

def initialize command, argv = ""
  @command = command
  @options = {:file => File.join(Dir.pwd, 'Scrapefile'), :ignore_robots_txt => false}

  opts = OptionParser.new do |opts|
    opts.banner = "Scrape #{Scrape::VERSION} - Usage: #{command} [options]"
    opts.separator ""
    opts.separator "Specific options:"

    opts.on "-f", "--scrapefile [FILE]", "Use FILE as scrapefile" do |file|
      @options[:file] = File.expand_path file.strip
    end
    opts.on "-i", "--ignore-robots-txt", "Ignore robots.txt" do
      @options[:ignore_robots_txt] = true
    end
    opts.on "-u", "--user-agent [AGENT]", "Change the user agent" do |agent|
      Scrape.user_agent = agent.strip
    end
    opts.on_tail "-h", "--help", "Show this message" do
      puts opts
      exit
    end
    opts.on_tail "-v", "--version", "Show version" do
      puts Scrape::VERSION
      exit
    end
  end
  opts.parse argv

  @app = Scrape::Application.new options[:file], options
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/scrape/cli.rb', line 5

def app
  @app
end

#commandObject (readonly)

Returns the value of attribute command.



5
6
7
# File 'lib/scrape/cli.rb', line 5

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/scrape/cli.rb', line 5

def options
  @options
end

Instance Method Details

#runObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/scrape/cli.rb', line 39

def run
  app.run
  exit
rescue SystemExit, Interrupt
  puts ""
  exit
rescue Scrape::FileNotFound
  puts "#{command} aborted!"
  puts "No Scrapefile found"
  exit -1
end