Module: Eaternet::Util

Defined in:
lib/eaternet/util.rb

Class Method Summary collapse

Class Method Details

.download(source:, dest:) ⇒ Object


26
27
28
# File 'lib/eaternet/util.rb', line 26

def self.download(source:, dest:)
  open(dest, 'wb') { |file| file << open(source).read }
end

.download_and_extract_zipfile(url) ⇒ String

A utility function to download a zip file, extract it into a temp directory.

Returns:

  • (String)

    the directory path


10
11
12
13
14
15
16
# File 'lib/eaternet/util.rb', line 10

def self.download_and_extract_zipfile(url)
  dir = make_temp_dir
  zip_path = File.join(dir, 'zip-file.zip')
  download(source: url, dest: zip_path)
  extract_zipfile(path: zip_path, dest_dir: dir)
  dir
end

.extract_zipfile(path:, dest_dir:) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/eaternet/util.rb', line 30

def self.extract_zipfile(path:, dest_dir:)
  open(path) do |zip_file|
    Zip::File.open(zip_file, 'rb') do |zip_data|
      zip_data.each { |entry| entry.extract(File.join(dest_dir, entry.name)) }
    end
  end
end

.make_temp_dirObject

Create a temporary directory which is automatically deleted when the program exits.


20
21
22
23
24
# File 'lib/eaternet/util.rb', line 20

def self.make_temp_dir
  dir = Dir.mktmpdir
  at_exit { FileUtils.remove_entry dir }
  dir
end