class Fig::Protocol::HTTP

File transfers via HTTP.

Public Instance Methods

download(uri, path, prompt_for_login) click to toggle source

Returns whether the file was not downloaded because the file already exists and is already up-to-date.

# File lib/fig/protocol/http.rb, line 20
def download(uri, path, prompt_for_login)
  log_download(uri, path)
  ::File.open(path, 'wb') do |file|
    file.binmode

    begin
      download_via_http_get(uri, file)
    rescue SystemCallError => error
      Fig::Logging.debug error.message
      raise Fig::FileNotFoundError.new error.message, uri
    rescue SocketError => error
      Fig::Logging.debug error.message
      raise Fig::FileNotFoundError.new error.message, uri
    end
  end
end

Private Instance Methods

download_via_http_get(uri_string, file, redirection_limit = 10) click to toggle source
# File lib/fig/protocol/http.rb, line 39
def download_via_http_get(uri_string, file, redirection_limit = 10)
  if redirection_limit < 1
    Fig::Logging.debug 'Too many HTTP redirects.'
    raise Fig::FileNotFoundError.new 'Too many HTTP redirects.', uri_string
  end

  response = Net::HTTP.get_response(URI(uri_string))

  case response
  when Net::HTTPSuccess then
    file.write(response.body)
  when Net::HTTPRedirection then
    location = response['location']
    Fig::Logging.debug "Redirecting to #{location}."
    download_via_http_get(location, file, redirection_limit - 1)
  else
    Fig::Logging.debug "Download failed: #{response.code} #{response.message}."
    raise Fig::FileNotFoundError.new(
      "Download failed: #{response.code} #{response.message}.", uri_string
    )
  end

  return
end