Class: Suvii::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/suvii/http.rb

Overview

Thin wrapper around OpenURI, therefore proxy can be configured via standard env variables.

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.save(url, destination, options = {}) ⇒ true

Saves a content of the url to a file.

Parameters:

  • url (String)

    URL to be fetched.

  • destination (String)

    path to a file to be created with the URL content.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :max_attempts_for_200_response (Integer) — default: 1, i.e. no retries

    number of times to fetch a remote resource until get a 200 response.

Returns:

  • (true)

Raises:

  • (OpenURI::HTTPError)

Since:

  • 0.1.0



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/suvii/http.rb', line 15

def self.save(url, destination, options = {})
  attempts ||= 1
  uri = URI.parse(url)
  Suvii.logger.info("downloading '#{url}'")
  IO.copy_stream(uri.open, destination)
  true
rescue OpenURI::HTTPError => e
  if attempts < (options[:max_attempts_for_200_response] || 1)
    Suvii.logger.warn("failed to download '#{url}': #{e.message}")
    attempts += 1
    retry
  else
    raise
  end
end