Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
À 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