Class: Vara::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/vara/downloader.rb

Class Method Summary collapse

Class Method Details

.download(metadata, target) ⇒ Object

Parameters:

  • source (String)

    The URL from which to download

  • target (String)

    The path on disk for where to save the downloaded file



9
10
11
12
13
14
15
16
# File 'lib/vara/downloader.rb', line 9

def self.download(, target)
  raise 'URL or AWS Config required to download!' unless .url || .aws
  if .aws
    download_from_aws(.aws, target)
  elsif .url
    download_from_url(.url, target)
  end
end

.download_from_aws(aws_config, target) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# 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