Skip to content

Instantly share code, notes, and snippets.

@isaksky
Created February 18, 2012 21:14
Show Gist options
  • Save isaksky/1861043 to your computer and use it in GitHub Desktop.
Save isaksky/1861043 to your computer and use it in GitHub Desktop.
HTTP fetch breaks
require 'uri'
require 'net/http'
# From http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html#method-c-get_response
# Does not fully work!
def fetch(uri_str, limit = 10)
# You should choose a better exception.
raise ArgumentError, 'too many HTTP redirects' if limit == 0
response = Net::HTTP.get_response(URI(uri_str))
case response
when Net::HTTPSuccess then
response
when Net::HTTPRedirection then
location = response['location']
puts "redirected to #{location}"
fetch(location, limit - 1)
else
response.value
end
end
response_1 = fetch "http://www.facebook.com" #Works fine
puts response_1.inspect
response_2 = fetch "http://apps.facebook.com/pickoffgame" #Explodes
puts response_2.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment