module Net

Public Class Methods

download(url, limit = nil) click to toggle source

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

# 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
download_and_save(url, path = nil, options = {}) click to toggle source

Downloads a given URL and saves it to disk at the specified path. If path is not provided or nil, then the last segment in the path of the URL is used as the file name.

Takes a third parameters as a hash of options:

CREDIT: Daniel Huckstep

# File lib/standard/facets/net/http.rb, line 43
def self.download_and_save(url, path = nil, options = {})
  opts = options.dup
  path = File.expand_path(path || url.split('/').last)
  raise ArgumentError.new('Save path is a directory') if File.directory?(path)
  resp = download(url, opts.delete(:limit))
  File.write(path, resp.body, opts) if resp.is_a?(Net::HTTPSuccess)
end