Skip to content

Instantly share code, notes, and snippets.

@pete-otaqui
Created February 5, 2010 15:55
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 pete-otaqui/295892 to your computer and use it in GitHub Desktop.
Save pete-otaqui/295892 to your computer and use it in GitHub Desktop.
Rakefile for capturing HTTP responses and serializing as YAML
# 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