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

Methods included from Idempotent

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.

Parameters:

  • uri (URI)

    The http(s) URI to the file to copy

  • dst (String)

    The path to destination of the copied file



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.message
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

Parameters:

  • uri (URI)

Returns:

  • (IO)


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

  options = HEADERS.merge({
    :content_length_proc => content_length_proc,
    :progress_proc       => progress_proc,
  })

  uri.open(options)
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.

Parameters:

  • uri (URI)

    The http(s) URI to the file to copy

Returns:

  • (String)

    The contents of the file with leading and trailing whitespace removed.

Since:

  • 0.9.0



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.message
end