Class: Railsmdb::Downloader
- Inherits:
-
Object
- Object
- Railsmdb::Downloader
- Defined in:
- lib/railsmdb/downloader.rb
Overview
A utility class for downloading a file from a given URL.
Instance Attribute Summary collapse
-
#destination ⇒ String
readonly
Where the file should be saved to.
-
#url ⇒ String
readonly
The url to download from.
Class Method Summary collapse
-
.fetch(url, destination, &callback) ⇒ Object
A helper method for fetching the file in a single call.
Instance Method Summary collapse
-
#fetch ⇒ Object
Perform the fetch, pulling from the url and writing to the destination.
-
#initialize(url, destination) ⇒ Downloader
constructor
Create a new Downloader object.
Constructor Details
#initialize(url, destination) ⇒ Downloader
Create a new Downloader object.
29 30 31 32 |
# File 'lib/railsmdb/downloader.rb', line 29 def initialize(url, destination) @url = url @destination = destination end |
Instance Attribute Details
#destination ⇒ String (readonly)
Returns where the file should be saved to.
12 13 14 |
# File 'lib/railsmdb/downloader.rb', line 12 def destination @destination end |
#url ⇒ String (readonly)
Returns the url to download from.
9 10 11 |
# File 'lib/railsmdb/downloader.rb', line 9 def url @url end |
Class Method Details
.fetch(url, destination, &callback) ⇒ Object
A helper method for fetching the file in a single call.
21 22 23 |
# File 'lib/railsmdb/downloader.rb', line 21 def self.fetch(url, destination, &callback) new(url, destination).fetch(&callback) end |
Instance Method Details
#fetch ⇒ Object
Perform the fetch, pulling from the url and writing to the destination.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/railsmdb/downloader.rb', line 35 def fetch File.open(destination, 'w:BINARY') do |io| connection.get(url) do |req| req..on_data = lambda do |chunk, total, _env| yield total if block_given? io << chunk end end end true end |