Class: Gem::Micro::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/microgem/downloader.rb

Defined Under Namespace

Classes: DownloadError

Class Method Summary collapse

Class Method Details

.curl_binaryObject



43
44
45
# File 'lib/microgem/downloader.rb', line 43

def self.curl_binary
  '/usr/bin/curl'
end

.get(remote, local) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/microgem/downloader.rb', line 35

def self.get(remote, local)
  if Config.simple_downloader?
    get_with_curl(remote, local)
  else
    get_with_net_http(remote, local)
  end
end

.get_with_curl(remote, local) ⇒ Object



8
9
10
11
12
# File 'lib/microgem/downloader.rb', line 8

def self.get_with_curl(remote, local)
  unless system("#{curl_binary} --silent --location --output '#{local}' '#{remote}'")
    raise DownloadError, "Failed to download `#{remote}' to `#{local}'"
  end
end

.get_with_net_http(remote, local) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/microgem/downloader.rb', line 14

def self.get_with_net_http(remote, local)
  require 'net/http'
  require 'uri'
  
  uri = URI.parse(remote)
  response = Net::HTTP.start(uri.host, uri.port) do |http|
    http.get(uri.path)
  end
  
  case response.code
  when '200'
    FileUtils.mkdir_p(File.dirname(local))
    File.open(local, 'w') { |file| file.write(response.body) }
    response
  when '302'
    get_with_net_http(response.header['Location'], local)
  else
    raise DownloadError, "Failed to download `#{remote}' to `#{local}' (#{response.code})"
  end
end