Class: Onboard::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/onboard/download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dirObject (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