Module: PEBuild::Transfer::OpenURI Private
- Extended by:
- Idempotent
- Defined in:
- lib/pe_build/transfer/open_uri.rb
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Defined Under Namespace
Classes: DownloadFailed
Constant Summary collapse
- HEADERS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
{'User-Agent' => "Vagrant/PEBuild (v#{PEBuild::VERSION})"}
Class Method Summary collapse
- .copy(uri, dst) ⇒ Object private
-
.download_file(uri) ⇒ IO
private
Open a open-uri file handle for the given URL.
-
.read(uri) ⇒ String
private
The contents of the file with leading and trailing whitespace removed.
Methods included from Idempotent
Class Method Details
.copy(uri, dst) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pe_build/transfer/open_uri.rb', line 19 def self.copy(uri, dst) idempotent(dst) do tmpfile = download_file(uri) # Ensure the file is closed after the download. On Windows, leaving the # file open will prevent it from being moved. tmpfile.close() FileUtils.mv(tmpfile, dst) end rescue StandardError => e raise DownloadFailed, :uri => uri, :msg => e. end |
.download_file(uri) ⇒ IO
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Open a open-uri file handle for the given URL
46 47 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 |
# File 'lib/pe_build/transfer/open_uri.rb', line 46 def self.download_file(uri) progress = nil downloaded = 0 content_length_proc = lambda do |length| if length and length > 0 STDERR.puts "Fetching: #{uri}" progress = ProgressBar.create( :title => "Fetching file", :total => length, :output => STDERR, :format => '%t: %p%% |%b>%i| %e') end end progress_proc = lambda do |size| unless progress.nil? progress.progress += (size - downloaded) downloaded = size end end = HEADERS.merge({ :content_length_proc => content_length_proc, :progress_proc => progress_proc, }) uri.open() ensure progress.stop unless progress.nil? end |
.read(uri) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The contents of the file with leading and trailing whitespace removed.
36 37 38 39 40 |
# File 'lib/pe_build/transfer/open_uri.rb', line 36 def self.read(uri) uri.read(HEADERS.merge({'Accept' => 'text/plain'})).strip rescue StandardError => e raise DownloadFailed, :uri => uri, :msg => e. end |