class Vara::Downloader

Public Class Methods

download(metadata, target) click to toggle source

@param source [String] The URL from which to download @param target [String] The path on disk for where to save the downloaded file

# File lib/vara/downloader.rb, line 9
def self.download(metadata, target)
  raise 'URL or AWS Config required to download!' unless metadata.url || metadata.aws
  if metadata.aws
    download_from_aws(metadata.aws, target)
  elsif metadata.url
    download_from_url(metadata.url, target)
  end
end
download_from_aws(aws_config, target) click to toggle source
# File lib/vara/downloader.rb, line 18
def self.download_from_aws(aws_config, target)
  s3 = Aws::S3::Client.new(
    access_key_id: aws_config.access_key_id,
    secret_access_key: aws_config.secret_access_key,
    force_path_style: true,
    region: aws_config.region
  )

  s3.get_object(
    response_target: target,
    bucket: aws_config.bucket_name,
    key: aws_config.filename
  )
end
download_from_url(url, target) click to toggle source
# File lib/vara/downloader.rb, line 33
def self.download_from_url(url, target)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.start { |h| stream_target(h, target, uri) }
end
stream_target(http, target, uri) click to toggle source
# File lib/vara/downloader.rb, line 40
def self.stream_target(http, target, uri)
  request = Net::HTTP::Get.new uri.request_uri

  http.request request do |response|
    return download_from_url(response['location'], target) if response.is_a?(Net::HTTPRedirection)

    open target, 'w' do |io|
      response.read_body { |chunk| io.write chunk }
    end
  end
end