Class: Miteru::Downloader

Inherits:
Service
  • Object
show all
Includes:
MemoWise
Defined in:
lib/miteru/downloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Service

call, #result, result

Constructor Details

#initialize(kit, base_dir: Miteru.config.download_to) ⇒ Downloader

<Description>

Parameters:

  • kit (Miteru::Kit)
  • base_dir (String) (defaults to: Miteru.config.download_to)


24
25
26
27
28
# File 'lib/miteru/downloader.rb', line 24

def initialize(kit, base_dir: Miteru.config.download_to)
  super()
  @kit = kit
  @base_dir = base_dir
end

Instance Attribute Details

#base_dirString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/miteru/downloader.rb', line 13

def base_dir
  @base_dir
end

#kitMiteru::Kit (readonly)

Returns:



16
17
18
# File 'lib/miteru/downloader.rb', line 16

def kit
  @kit
end

Instance Method Details

#callString

Returns:

  • (String)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/miteru/downloader.rb', line 33

def call
  destination = kit.filepath_to_download

  # downloader.download(kit.url, destination:, max_size:)
  downloader.download(kit.url, destination:, max_size:)

  unless Record.unique_sha256?(sha256(destination))
    FileUtils.rm destination
    raise UniquenessError, "Kit:#{sha256(destination)} is registered already."
  end

  # Record a kit in DB
  Record.create_by_kit_and_hash(kit, sha256: sha256(destination))
  logger.info "Download #{kit.url} as #{destination}"

  destination
end