lib/prss/downloader.rb in prss-0.0.4 vs lib/prss/downloader.rb in prss-0.1.0
- old
+ new
@@ -8,17 +8,22 @@
def initialize(links)
@links = links
@hydra = Typhoeus::Hydra.new
end
- def download_to(output_path)
+ def self.verify!(output_path)
output = Pathname.new(output_path).expand_path
raise 'output path is not directory' unless output.directory?
+ output
+ end
+ def download_to(output_path)
+ output = self.class.verify!(output_path)
+
@files = []
@links.each do |link|
- request = Typhoeus::Request.new(link, :follow_location => true)
+ request = Typhoeus::Request.new(link, followlocation: true)
request.on_complete do |response|
@files << save_file(response, output)
end
@@ -28,12 +33,14 @@
hydra.run
@files
end
- def save_file response, output
+ def save_file(response, output)
filename = response.headers_hash['Content-Disposition'][/filename="(.+)"$/ ,1]
file = output.join(filename)
+
+ return if file.exist?
open(file, 'wb') do |file|
file.write(response.body)
end