Skip to content

Instantly share code, notes, and snippets.

@robotarmy
Created July 9, 2010 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotarmy/469715 to your computer and use it in GitHub Desktop.
Save robotarmy/469715 to your computer and use it in GitHub Desktop.
Cache Fake Web Content to Disk and Register a URI
# Example
# fan = Factory(:fan)
# FakeWeb.register(:get,"http://www.google.com/",fan.class)
# fan.visit_google #successive calls read cache
#
# recommended to put into spec/support
#
require 'fakeweb'
require 'open-uri'
require 'fileutils'
module FakeWeb
class << self
def register(method,uri,klass)
file_path = File.join(File.dirname(__FILE__),to_s,"#{klass.to_s}.#{uri.hash}.fakeweb")
content = nil
unless File.exists?(file_path)
FileUtils.mkdir_p(File.dirname(file_path))
content = open(uri).read
File.open(file_path,File::CREAT|File::TRUNC|File::RDWR) do |f|
f << content
end
else
content = File.open(file_path).readlines.join
end
self.register_uri(method,uri,:body => content)
end
end
end
@myronmarston
Copy link

If you want something that helps you record and replay HTTP requests, check out VCR.

@robotarmy
Copy link
Author

Is better.

@sandro
Copy link

sandro commented Jul 9, 2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment