Class: PomPomPom::CachingDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/pompompom/downloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir, downloader = Downloader.new) ⇒ CachingDownloader

Returns a new instance of CachingDownloader.



21
22
23
# File 'lib/pompompom/downloader.rb', line 21

def initialize(cache_dir, downloader=Downloader.new)
  @cache_dir, @downloader = cache_dir, downloader
end

Instance Method Details

#get(url) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pompompom/downloader.rb', line 25

def get(url)
  cached = cache_path(url)
  if File.exists?(cached)
    File.read(cached)
  else
    data = @downloader.get(url)
    FileUtils.mkdir_p(File.dirname(cached))
    File.open(cached, 'w') { |f| f.write(data) }
    data
  end
end