Class: Downloadr::HTTP

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, download_path = nil) ⇒ HTTP

Returns a new instance of HTTP.

Parameters:

  • uri (String)
  • download_path (String) (defaults to: nil)


12
13
14
15
# File 'lib/downloadr/http.rb', line 12

def initialize(uri, download_path = nil)
  @uri = ::Addressable::URI.parse(uri)
  @path = normalize_path(download_path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/downloadr/http.rb', line 8

def path
  @path
end

#uriObject (readonly)

Returns the value of attribute uri.



8
9
10
# File 'lib/downloadr/http.rb', line 8

def uri
  @uri
end

Class Method Details

.download(uri, download_path = nil) ⇒ Object



35
36
37
38
# File 'lib/downloadr/http.rb', line 35

def self.download(uri, download_path = nil)
  downloader = Downloadr::HTTP.new(uri, download_path)
  downloader.download
end

Instance Method Details

#downloadObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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