Sha256: 7e074c54e731dfb3b1aae3935cae5edaf5eef4fd56e0fa6b1797134c1646c1a8

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

require 'pe_build/version'

require 'open-uri'
require 'progressbar'


module PEBuild
module Transfer
class URI

  # @param src [String] The URL to the file to copy
  # @param dst [String] The path to destination of the copied file
  def initialize(src, dst)
    @src, @dst = src, dst
  end

  def copy
    tmpfile = open_uri(@src)
    FileUtils.mv(tmpfile, @dst)
  end

  HEADERS = {'User-Agent' => "Vagrant/PEBuild (v#{PEBuild::VERSION})"}

  private

  # Open a open-uri file handle for the given URL
  #
  # @return [IO]
  def open_uri(path)
    uri = ::URI.parse(path)
    progress = nil

    content_length_proc = lambda do |length|
      if length and length > 0
        progress = ProgressBar.new('Fetching', length)
        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
end
end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vagrant-pe_build-0.2.0 lib/pe_build/transfer/uri.rb
vagrant-pe_build-0.1.0 lib/pe_build/transfer/uri.rb