Class: IMW::Tools::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/imw/tools/downloader.rb

Overview

A class to download a collection of resources to a shared directory.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, *inputs) ⇒ Downloader

Returns a new instance of Downloader.



8
9
10
11
# File 'lib/imw/tools/downloader.rb', line 8

def initialize dir, *inputs
  self.dir    = dir
  self.inputs = inputs unless inputs.blank?
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



18
19
20
# File 'lib/imw/tools/downloader.rb', line 18

def dir
  @dir
end

#inputsObject

Returns the value of attribute inputs.



23
24
25
# File 'lib/imw/tools/downloader.rb', line 23

def inputs
  @inputs
end

Class Method Details

.dir=(new_dir) ⇒ Object

Raises:



13
14
15
16
17
# File 'lib/imw/tools/downloader.rb', line 13

def self.dir= new_dir
  @dir = IMW.open(new_dir)
  raise IMW::PathError.new("#{@dir} must be a local directory") unless @dir.is_local? && @dir.is_directory?
  @dir
end

Instance Method Details

#after_downloadObject



57
58
# File 'lib/imw/tools/downloader.rb', line 57

def after_download
end

#before_downloadObject



54
55
# File 'lib/imw/tools/downloader.rb', line 54

def before_download
end

#clean!Object



49
50
51
52
# File 'lib/imw/tools/downloader.rb', line 49

def clean!
  IMW.log_if_verbose("Deleting downloader directory #{dir}")
  dir.rm_rf!
end

#download!Object



29
30
31
32
33
34
35
36
37
# File 'lib/imw/tools/downloader.rb', line 29

def download!
  before_download
  inputs.each do |input|
    downloaded_path = downloaded_path_for(input)
    IMW.log_if_verbose "Downloading #{input} to #{downloaded_path}"
    input.cp(downloaded_path)
  end
  after_download
end

#downloaded?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/imw/tools/downloader.rb', line 39

def downloaded?
  downloaded_resources.all? { |resource| resource.exist? }
end

#downloaded_path_for(input) ⇒ Object



25
26
27
# File 'lib/imw/tools/downloader.rb', line 25

def downloaded_path_for input
  dir.join(input.respond_to?(:effective_basename) ? input.effective_basename : input.basename)
end

#downloaded_resourcesObject



43
44
45
46
47
# File 'lib/imw/tools/downloader.rb', line 43

def downloaded_resources
  inputs.map do |input|
    IMW.open(downloaded_path_for(input))
  end
end