Method: Fetchers::Url.download_archive

Defined in:
lib/fetchers/url.rb

.download_archive(url, opts = {}) ⇒ Object

download url into archive using opts, returns File object and content-type from HTTP headers



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fetchers/url.rb', line 68

def self.download_archive(url, opts = {})
  http_opts = {}
  # http_opts['http_basic_authentication'] = [opts['user'] || '', opts['password'] || ''] if opts['user']
  http_opts['ssl_verify_mode'.to_sym] = OpenSSL::SSL::VERIFY_NONE if opts['insecure']
  http_opts['Authorization'] = "Bearer #{opts['token']}" if opts['token']

  remote = open(
    url,
    http_opts,
  )

  content_type = remote.meta['content-type']
  file_type = MIME_TYPES[content_type] ||
              throw(RuntimeError, 'Failed to resolve URL target, its '\
              "metadata did not match ZIP or TAR: #{content_type}")

  # fall back to tar
  if file_type.nil?
    fail "Could not determine file type for content type #{content_type}."
  end

  # download content
  archive = Tempfile.new(['inspec-dl-', file_type])
  archive.binmode
  archive.write(remote.read)
  archive.rewind
  archive.close
  archive
end