Created
October 29, 2008 02:53
-
-
Save ivey/20598 to your computer and use it in GitHub Desktop.
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
def fetch_with_limits(uri_str, limit = 10) | |
raise ArgumentError, 'HTTP redirect too deep' if limit == 0 | |
url = URI.parse(uri_str) | |
req = Net::HTTP::Post.new(url.path) | |
req.range = (0..1000) | |
response = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) } | |
case response | |
when Net::HTTPSuccess then response | |
when Net::HTTPRedirection then fetch(response['location'], limit - 1) | |
else | |
response.error! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment