Class: Vagrant::Downloaders::HTTP

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant/downloaders/http.rb

Overview

Downloads a file from an HTTP URL to a temporary file. This downloader reports its progress to stdout while downloading.

Constant Summary collapse

CL_RESET =

ANSI escape code to clear lines from cursor to end of line

"\r\e[0K"

Instance Method Summary collapse

Methods inherited from Base

#prepare

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Instance Method Details

#complete_progressObject



41
42
43
44
# File 'lib/vagrant/downloaders/http.rb', line 41

def complete_progress
  # Just clear the line back out
  print "#{CL_RESET}"
end

#download!(source_url, destination_file) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant/downloaders/http.rb', line 9

def download!(source_url, destination_file)
  Net::HTTP.get_response(URI.parse(source_url)) 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

  complete_progress
end

#report_progress(progress, total) ⇒ Object



35
36
37
38
39
# File 'lib/vagrant/downloaders/http.rb', line 35

def report_progress(progress, total)
  percent = (progress.to_f / total.to_f) * 100
  print "#{CL_RESET}Download Progress: #{percent.to_i}% (#{progress} / #{total})"
  $stdout.flush
end