Created
January 7, 2011 15:06
-
-
Save stephencelis/769559 to your computer and use it in GitHub Desktop.
À la VCR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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