Class: HTTPDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/abicli/commands/instant-deploy.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.match?(uri) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/abicli/commands/instant-deploy.rb', line 32

def self.match?(uri)
  # URI.parse barfs on '<drive letter>:\\files \on\ windows'
  extracted = URI.extract(uri).first
  extracted && extracted.include?(uri)
end

Instance Method Details

#download!(source_url, destination_file) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/abicli/commands/instant-deploy.rb', line 48

def download!(source_url, destination_file)
  proxy_uri = URI.parse(ENV["http_proxy"] || "")
  uri = URI.parse(source_url)
  http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)

  if uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  http.start do |h|
    h.request_get(uri.request_uri) do |response|
      total = response.content_length
      progress = 0
      segment_count = 0

      response.read_body do |segment|
        # Report the progress out
        progress += segment.length
        segment_count += 1

        # Progress reporting is limited to every 25 segments just so
        # we're not constantly updating
        if segment_count % 25 == 0
          report_progress(progress, total)
          segment_count = 0
        end


        # Store the segment
        destination_file.write(segment)
      end
    end
  end
rescue SocketError
  raise Errors::DownloaderHTTPSocketError.new
end

#report_progress(progress, total, show_parts = true) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/abicli/commands/instant-deploy.rb', line 38

def report_progress(progress, total, show_parts=true)
  line_reset = "\r\e[0K" 
  percent = (progress.to_f / total.to_f) * 100
  line = "Progress: #{percent.to_i}%"
  line << " (#{progress} / #{total})" if show_parts
  line = "#{line_reset}#{line}"
  $stdout.sync = true
  $stdout.print line
end