Module: DownloadFiles

Defined in:
lib/download_files.rb,
lib/download_files/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.download_files(page_address, pattern, dir = FileUtils.pwd, logger = Null::Object.instance) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/download_files.rb', line 21

def self.download_files(page_address, pattern, dir=FileUtils.pwd, logger=Null::Object.instance)
  logger.debug "DownloadFiles.download_files: " + {page_address: page_address, pattern: pattern, dir: dir, logger: logger}.inspect

  abs_dir = File.expand_path(dir, FileUtils.pwd)

  Mechanize.go(page_address) do |agent, page|
    logger.debug page.inspect
    logger.info "On #{page.uri}"

    agent.pluggable_parser.default = Mechanize::Download

    page.links.each do |link|
      logger.debug "Checking link #{link.href}"

      next unless link.href && link.href.match(pattern)

      full_file_name = File.expand_path(File.basename(link.href), abs_dir)
      logger.info "Downloading #{link.click.uri} to #{full_file_name}"

      agent.get(link.click.uri).save(full_file_name)
    end
  end
end