class Downloadr::HTTP

Attributes

path[R]
uri[R]

Public Class Methods

download(uri, download_path = nil) click to toggle source
# File lib/downloadr/http.rb, line 35
def self.download(uri, download_path = nil)
  downloader = Downloadr::HTTP.new(uri, download_path)
  downloader.download
end
new(uri, download_path = nil) click to toggle source

@param [String] uri @param [String] download_path

# File lib/downloadr/http.rb, line 12
def initialize(uri, download_path = nil)
  @uri = ::Addressable::URI.parse(uri)
  @path = normalize_path(download_path)
end

Public Instance Methods

download() click to toggle source
# File lib/downloadr/http.rb, line 17
def download
  begin
    response = ::RestClient::Request.execute(
                 :method => :get,
                 :url => @uri.to_s,
                 :timeout => 100,
                 :open_timeout => 10
               )

  rescue ::SocketError
    raise Downloadr::SocketError
  rescue RestClient::ResourceNotFound
    raise Downloadr::ResourceNotFound
  end

  File.write(@path, response.to_str)
end

Private Instance Methods

normalize_path(download_path) click to toggle source
# File lib/downloadr/http.rb, line 41
def normalize_path(download_path)
  if download_path.nil?
    if @uri.basename.empty? ||
       @uri.basename.nil?
      raise Downloadr::UnknownDownloadPath
    else
      path = @uri.basename
    end
  else
    path = download_path
  end

  return path
end