Skip to content

Instantly share code, notes, and snippets.

@ivey
Created October 29, 2008 02:53
Show Gist options
  • Save ivey/20598 to your computer and use it in GitHub Desktop.
Save ivey/20598 to your computer and use it in GitHub Desktop.
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