Module: Kosmos::PackageDownloads

Defined in:
lib/kosmos/package_downloads.rb

Class Method Summary collapse

Class Method Details

.download_and_unzip_package(package, opts = {}) ⇒ Object

Downloads and unzips a package. This will call #download! on its own, and will return the location where the package was downloaded to as a Pathname.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kosmos/package_downloads.rb', line 6

def self.download_and_unzip_package(package, opts = {})
  download_file = download_package(package, opts)

  Util.log "Unzipping ..."

  output_path = Pathname.new(download_file.path).parent.to_s

  Zip::File.open(download_file.path) do |zip_file|
    zip_file.each do |entry|
      destination = File.join(output_path, entry.name)
      parent_dir = File.expand_path('..', destination)

      FileUtils.mkdir_p(parent_dir) unless File.exists?(parent_dir)

      entry.extract(destination)
    end
  end

  File.delete(File.absolute_path(download_file))

  output_path
end

.download_package(package, opts = {}) ⇒ Object

Downloads the zipfile for a package using its URL, unless a cached version is found first. Uses DownloadUrl to intelligently resolve download URLs.

Returns the file downloaded, which is created in a temp directory.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kosmos/package_downloads.rb', line 33

def self.download_package(package, opts = {})
  cached_download = download_from_cache(package)
  downloaded_file = if cached_download
    Util.log "Use a cached version of #{package.title} ..."
    cached_download
  else
    Util.log "The package is found at #{package.url}. "\
      "Finding the download URL ..."
    download_url = DownloadUrl.new(package.url).resolve_download_url

    Util.log "Found it. Downloading from #{download_url} ..."
    HTTParty.get(download_url)
  end

  save_to_cache(package, downloaded_file) if opts[:cache_after_download]

  tmpdir = Dir.mktmpdir

  download_file = File.new(File.join(tmpdir, 'download'), 'wb+')
  download_file.write(downloaded_file)
  download_file.close

  download_file
end