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
# File 'lib/pe_build/transfer/open_uri.rb', line 19

def self.copy(uri, dst)
  idempotent(dst) do
    tmpfile = download_file(uri)
    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)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pe_build/transfer/open_uri.rb', line 43

def self.download_file(uri)
  progress = nil

  content_length_proc = lambda do |length|
    if length and length > 0
      STDERR.puts "Fetching: #{uri}"
      progress = ProgressBar.new("Fetching file", length, STDERR)
      progress.file_transfer_mode
    end
  end

  progress_proc = lambda do |size|
    progress.set(size) if progress
  end

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

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



33
34
35
36
37
# File 'lib/pe_build/transfer/open_uri.rb', line 33

def self.read(uri)
  uri.read(HEADERS.merge({'Accept' => 'text/plain'})).strip
rescue StandardError => e
  raise DownloadFailed, :uri => uri, :msg => e.message
end