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.



14
15
16
# File 'lib/onboard/download.rb', line 14

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.



12
13
14
# File 'lib/onboard/download.rb', line 12

def cache_dir
  @cache_dir
end

Instance Method Details



18
19
20
# File 'lib/onboard/download.rb', line 18

def build_link(project, version)
  DRUPAL_DL_LINK + "#{project}-#{version}.tar.gz"
end

#fetch(url, max_age = 1800) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/onboard/download.rb', line 26

def fetch(url, max_age = 1800)
  FileUtils.mkdir_p(cache_dir) unless File.directory?(cache_dir)
  file_path = path(url)
  if File.exist? 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



22
23
24
# File 'lib/onboard/download.rb', line 22

def path(url)
  File.join('', @cache_dir, Digest::MD5.hexdigest(url))
end