Class: Onboard::Download
- Inherits:
-
Object
- Object
- Onboard::Download
- Defined in:
- lib/onboard/download.rb
Instance Attribute Summary collapse
-
#cache_dir ⇒ Object
readonly
Returns the value of attribute cache_dir.
Instance Method Summary collapse
- #fetch(url, max_age = 1800) ⇒ Object
-
#initialize(cache_dir = '/tmp/onboard/cache') ⇒ Download
constructor
A new instance of Download.
- #path(url) ⇒ Object
Constructor Details
#initialize(cache_dir = '/tmp/onboard/cache') ⇒ Download
Returns a new instance of Download.
10 11 12 |
# File 'lib/onboard/download.rb', line 10 def initialize(cache_dir='/tmp/onboard/cache') @cache_dir = cache_dir end |
Instance Attribute Details
#cache_dir ⇒ Object (readonly)
Returns the value of attribute cache_dir.
8 9 10 |
# File 'lib/onboard/download.rb', line 8 def cache_dir @cache_dir end |
Instance Method Details
#fetch(url, max_age = 1800) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/onboard/download.rb', line 18 def fetch(url, max_age=1800) unless File.directory?(cache_dir) FileUtils.mkdir_p(cache_dir) end 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
14 15 16 |
# File 'lib/onboard/download.rb', line 14 def path(url) File.join("", @cache_dir, Digest::MD5.hexdigest(url)) end |