class Plumnailer::CachingFetcher

Fetch the contents of a url and cache result on filesystem.

Attributes

cache_dir[RW]

Public Class Methods

new(cache_dir) click to toggle source
# File lib/plumnailer/caching_fetcher.rb, line 11
def initialize(cache_dir)
  @cache_dir = cache_dir
  FileUtils.mkdir_p cache_dir
end

Public Instance Methods

fetch(url, orig_url=nil, redirect_count=0) click to toggle source

Fetch the contents of a url and cache result on filesystem.

Calls superclass method
# File lib/plumnailer/caching_fetcher.rb, line 17
def fetch(url, orig_url=nil, redirect_count=0)
  cache_file = File.join(cache_dir, CGI.escape(url.to_s))

  if File.exists?(cache_file)
    open(cache_file) { |f| f.read }
  else
    data = super
    open(cache_file, 'w') { |f| f.write(data) }
    data
  end
end