Module: Scrapers::Download
- Defined in:
- lib/scrapers/download.rb
Class Method Summary collapse
Class Method Details
.download(url, dir = ".", overwrite = false) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/scrapers/download.rb', line 18 def self.download(url,dir=".",overwrite=false) # need a new agent each time! agent = Mechanize.new agent.pluggable_parser.default = Mechanize::Download @dir = validate_directory(dir) dl = agent.get(url) Dir.chdir(@dir) do |dir| if overwrite dl.save!(dl.filename) else dl.save() end end File.join(@dir,dl.filename) end |
.validate_directory(d) ⇒ Object
35 36 37 38 |
# File 'lib/scrapers/download.rb', line 35 def self.validate_directory(d) raise "#{d} is not a writable directory!" unless File.directory?(d) and File.writable?(d) d end |