Created
December 11, 2012 13:52
-
-
Save flagbug/4258731 to your computer and use it in GitHub Desktop.
Ruby redirect
This file contains hidden or 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
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 |
This file contains hidden or 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
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