Class: WebLoader::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/web_loader/downloader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Downloader

Returns a new instance of Downloader.



34
35
36
# File 'lib/web_loader/downloader.rb', line 34

def initialize(opts)
  @opts = opts
end

Class Method Details

.run(argv) ⇒ Object



5
6
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
# File 'lib/web_loader/downloader.rb', line 5

def self.run(argv)
  STDOUT.sync = true
  opts = {}
  opt = OptionParser.new(argv)
  opt.banner = "Usage: #{opt.program_name} [-h|--help] [Options] <URL> "
  opt.version = WebLoader::VERSION
  opt.separator('')
  opt.separator("Options:")
  opt.on_head('-h', '--help', 'Show this message') do |v|
    puts opt.help
    exit
  end
  opt.on('-v', '--verbose', 'Verbose message') {|v| opts[:v] = v}
  drivers = ['pureruby', 'selenium']
  opt.on('-d DRIVER', '--driver=DRIVER', drivers, drivers.join("|") + "(default pureruby)") {|v| opts[:d] = v }
  opt.on("--disable-cache", "Disable cache") {|v| opts[:disable_cache] = v }
  opt.on('--user-agent=USERAGENT', 'Set User-Agent header') {|v| opts[:user_agent] = v }
  opt.on('-b', '--binary', 'Download binary files') {|v| opts[:binary] = v }
  opt.parse!(argv)
  if argv.empty?
    puts "Error: URL is required."
    puts opt.help
    exit
  end
  command = self.new(opts)
  url = argv[0]
  command.execute(url)
end

Instance Method Details

#execute(url) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/web_loader/downloader.rb', line 38

def execute(url)
  driver = create_driver
  loader = WebLoader::Command.new(driver)
  if @opts[:disable_cache]
    loader.use_cache = false
  end
  if @opts[:user_agent]
    loader.user_agent = @opts[:user_agent]
  end
  if @opts[:binary]
    loader.binary = true
  end
  loader.load(url)
end