Class: Onboard::Download
- Inherits:
-
Object
- Object
- Onboard::Download
- Defined in:
- lib/onboard/download.rb
Instance Method Summary collapse
- #fetch(url, max_age = 1800) ⇒ Object
-
#initialize(cache_dir = '/tmp') ⇒ Download
constructor
A new instance of Download.
- #path(url) ⇒ Object
Constructor Details
#initialize(cache_dir = '/tmp') ⇒ Download
Returns a new instance of Download.
7 8 9 |
# File 'lib/onboard/download.rb', line 7 def initialize(cache_dir='/tmp') @cache_dir = cache_dir end |
Instance Method Details
#fetch(url, max_age = 1800) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/onboard/download.rb', line 15 def fetch(url, max_age=1800) file_path = self.path(url) if File.exists? file_path return File.new(file_path).read if Time.now-File.mtime(file_path)<max_age end File.open(file_path, "w") do |data| data << Net::HTTP.get_response(URI.parse(url)).body end end |
#path(url) ⇒ Object
11 12 13 |
# File 'lib/onboard/download.rb', line 11 def path(url) File.join("", @cache_dir, Digest::MD5.hexdigest(url)) end |