Method: Net.download

Defined in:
lib/standard/facets/net/http.rb

.download(url, limit = nil) ⇒ Object

Returns a response object for the given URL or raises an exception with the appropriate error status code if unable to complete the request

Net.download('http://example.com/')
Net.download('http://example.com/', 2) # fail after 2 redirects

CREDIT: Daniel Huckstep

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
# File 'lib/standard/facets/net/http.rb', line 13

def self.download(url, limit = nil)
  limit ||= 10
  raise ArgumentError, 'HTTP redirect too deep' if limit.zero?
  resp = Net::HTTP.get_response(URI.parse(url))
  case resp
  when Net::HTTPSuccess     then resp
  when Net::HTTPRedirection then download(resp['location'], limit - 1)
  else resp.error!
  end
end