class ChefApply::FileFetcher

Public Class Methods

download_file(url, local_path) click to toggle source
# File lib/chef_apply/file_fetcher.rb, line 42
def download_file(url, local_path)
  temp_path = "#{local_path}.downloading"
  file = open(temp_path, "wb")
  ChefApply::Log.debug "Downloading: #{temp_path}"
  Net::HTTP.start(url.host) do |http|

    http.request_get(url.path) do |resp|
      resp.read_body do |segment|
        file.write(segment)
      end
    end
  rescue e
    @error = true
    raise
  ensure
    file.close
    # If any failures occurred, don't risk keeping
    # an incomplete download that we'll see as 'cached'
    if @error
      FileUtils.rm_f(temp_path)
    else
      FileUtils.mv(temp_path, local_path)
    end

  end
end
fetch(path) click to toggle source

Simple fetcher of an http(s) url. Returns the local path of the downloaded file.

# File lib/chef_apply/file_fetcher.rb, line 28
def fetch(path)
  cache_path = ChefApply::Config.cache.path
  FileUtils.mkdir_p(cache_path)
  url = URI.parse(path)
  name = File.basename(url.path)
  local_path = File.join(cache_path, name)

  # TODO header check for size or checksum?
  return local_path if File.exist?(local_path)

  download_file(url, local_path)
  local_path
end