Skip to content

Instantly share code, notes, and snippets.

@stephencelis
Created January 7, 2011 15:06
Show Gist options
  • Save stephencelis/769559 to your computer and use it in GitHub Desktop.
Save stephencelis/769559 to your computer and use it in GitHub Desktop.
À la VCR.
require 'open-uri'
require 'digest/md5'
class << OpenURI
CACHE_DIR = Rails.root.join 'tmp', 'cache', 'open-uri'
CACHE_DIR.mkdir unless CACHE_DIR.exist?
alias cache_original_open_uri open_uri
def open_uri name, *rest, &block
snap = Digest::MD5.hexdigest name.to_s
path = CACHE_DIR.join snap
return path.read if path.exist?
io = cache_original_open_uri name, *rest, &block
path.open('wb') { |f| f.write io.read }
io.rewind
io
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment