Created
February 5, 2010 15:55
-
-
Save pete-otaqui/295892 to your computer and use it in GitHub Desktop.
Rakefile for capturing HTTP responses and serializing as YAML
This file contains 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
# should be called 'Rakefile' | |
# | |
# Usage: | |
# rake response:capture url=http://google.com/ filename=google_200 | |
# rake response:capture url=http://google.com/404 filename=google_404 | |
require 'rubygems' | |
require 'net/http' | |
require 'uri' | |
namespace :response do | |
desc "Response management" | |
task :capture, :url, :filename do |t,args| | |
filepath = '../responses/' | |
filename = args.filename | |
filename += '.http' unless filename =~ /\.http$/ | |
puts "Capturing '#{args.url}'" | |
puts "Will save as '#{filepath}#{filename}'" | |
uri = URI.parse(args.url) | |
proxy = Net::HTTP::Proxy('www-cache.reith.bbc.co.uk',80) | |
proxy.start(uri.host) do |h| | |
req = Net::HTTP::Get.new(uri.path) | |
response = h.request(req) | |
serial = response.to_yaml | |
file = File.new("#{filepath}#{filename}","w") | |
file.write(serial) | |
file.close | |
puts "Saved #{filepath}#{filename}" | |
response | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment