Class: Tenderloin::Actions::Box::Download

Inherits:
Tenderloin::Actions::Base show all
Defined in:
lib/tenderloin/actions/box/download.rb

Overview

An action which acts on a box by downloading the box file from the given URI into a temporary location. This action parses a given URI and handles downloading it via one of the many Tenderloin downloads (such as Downloaders::File).

This action cleans itself up by removing the downloaded box file.

Constant Summary collapse

BASENAME =
"box"
BUFFERSIZE =

1 MB

1048576

Instance Attribute Summary collapse

Attributes inherited from Tenderloin::Actions::Base

#runner

Instance Method Summary collapse

Methods inherited from Tenderloin::Actions::Base

#initialize

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

This class inherits a constructor from Tenderloin::Actions::Base

Instance Attribute Details

#downloaderObject (readonly)

Returns the value of attribute downloader.



14
15
16
# File 'lib/tenderloin/actions/box/download.rb', line 14

def downloader
  @downloader
end

Instance Method Details

#cleanupObject



38
39
40
41
42
43
# File 'lib/tenderloin/actions/box/download.rb', line 38

def cleanup
  if @runner.temp_path && File.exist?(@runner.temp_path)
    logger.info "Cleaning up downloaded box..."
    File.unlink(@runner.temp_path)
  end
end

#download_to(f) ⇒ Object



56
57
58
59
# File 'lib/tenderloin/actions/box/download.rb', line 56

def download_to(f)
  logger.info "Copying box to temporary location..."
  downloader.download!(@runner.uri, f)
end

#execute!Object



31
32
33
34
35
36
# File 'lib/tenderloin/actions/box/download.rb', line 31

def execute!
  with_tempfile do |tempfile|
    download_to(tempfile)
    @runner.temp_path = tempfile.path
  end
end

#prepareObject

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tenderloin/actions/box/download.rb', line 16

def prepare
  # Parse the URI given and prepare a downloader
  uri = URI.parse(@runner.uri)
  uri_map = [[URI::HTTP, Downloaders::HTTP], [URI::Generic, Downloaders::File]]

  uri_map.find do |uri_type, downloader_klass|
    if uri.is_a?(uri_type)
      logger.info "#{uri_type} for URI, downloading via #{downloader_klass}..."
      @downloader = downloader_klass.new
    end
  end

  raise ActionException.new("Unknown URI type for box download.") unless @downloader
end

#rescue(exception) ⇒ Object



45
46
47
# File 'lib/tenderloin/actions/box/download.rb', line 45

def rescue(exception)
  cleanup
end

#with_tempfileObject



49
50
51
52
53
54
# File 'lib/tenderloin/actions/box/download.rb', line 49

def with_tempfile
  logger.info "Creating tempfile for storing box file..."
  Tempfile.open(BASENAME, Env.tmp_path) do |tempfile|
    yield tempfile
  end
end