Skip to content

Instantly share code, notes, and snippets.

@flagbug
Created December 11, 2012 13:52
Show Gist options
  • Save flagbug/4258731 to your computer and use it in GitHub Desktop.
Save flagbug/4258731 to your computer and use it in GitHub Desktop.
Ruby redirect
require 'net/http'
require 'uri'
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']
warn "redirected to #{location}"
fetch(location, limit - 1)
else
response.value
end
end
require 'httpclient'
client = HTTPClient.new
content = client.get_content("http://example.com")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment