Class: PRSS::Downloader
- Inherits:
-
Object
- Object
- PRSS::Downloader
- Defined in:
- lib/prss/downloader.rb
Instance Attribute Summary collapse
-
#hydra ⇒ Object
readonly
Returns the value of attribute hydra.
Class Method Summary collapse
Instance Method Summary collapse
- #download_to(output_path) ⇒ Object
-
#initialize(links) ⇒ Downloader
constructor
A new instance of Downloader.
- #save_file(response, output) ⇒ Object
Constructor Details
#initialize(links) ⇒ Downloader
Returns a new instance of Downloader.
8 9 10 11 |
# File 'lib/prss/downloader.rb', line 8 def initialize(links) @links = links @hydra = Typhoeus::Hydra.new end |
Instance Attribute Details
#hydra ⇒ Object (readonly)
Returns the value of attribute hydra.
6 7 8 |
# File 'lib/prss/downloader.rb', line 6 def hydra @hydra end |
Class Method Details
.verify!(output_path) ⇒ Object
13 14 15 16 17 |
# File 'lib/prss/downloader.rb', line 13 def self.verify!(output_path) output = Pathname.new(output_path). raise 'output path is not directory' unless output.directory? output end |
Instance Method Details
#download_to(output_path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/prss/downloader.rb', line 19 def download_to(output_path) output = self.class.verify!(output_path) @files = [] @links.each do |link| request = Typhoeus::Request.new(link, followlocation: true) request.on_complete do |response| @files << save_file(response, output) end hydra.queue request end hydra.run @files end |
#save_file(response, output) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/prss/downloader.rb', line 38 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 filename end |