Class: SudachiInstaller::Downloader

Inherits:
Object
  • Object
show all
Extended by:
Dry::Configurable
Defined in:
lib/sudachi-installer/downloader.rb

Overview

download and expand archive if it is zip archive

Instance Method Summary collapse

Instance Method Details

#download(url, filename, dest_dir) ⇒ Object

Parameters:

  • url (string)
  • filename (string)
  • dest_dir (string)


18
19
20
21
22
23
# File 'lib/sudachi-installer/downloader.rb', line 18

def download(url, filename, dest_dir)
  FileUtils.mkdir_p(dest_dir)

  Down.download(url, destination: File.join(dest_dir, filename))
  expand(File.join(dest_dir, filename), force: true) if filename.match?(/\.zip\z/i)
end

#expand(path, force: false) ⇒ Object

Parameters:

  • path (String)
  • force (boolean) (defaults to: false)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sudachi-installer/downloader.rb', line 29

def expand(path, force: false)
  dir = File.dirname(path)

  Zip::File.open(path) { |zip|
    zip.each { |entry|
      dest = File.join(dir, entry.name)
      FileUtils.rm_rf dest if File.exist?(dest) && force
      entry.extract(File.join(dir, entry.name))
    }
  }
end