Class: Passifier::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/passifier/storage.rb

Overview

Disk storage for a pass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scratch_directory, assets) ⇒ Storage

Returns a new instance of Storage.

Parameters:

  • scratch (String)

    directory The directory to use for file storage

  • assets (Array<Object>)

    The file assets to store



12
13
14
15
# File 'lib/passifier/storage.rb', line 12

def initialize(scratch_directory, assets)
  @assets = [assets].flatten
  @scratch_directory = scratch_directory
end

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



8
9
10
# File 'lib/passifier/storage.rb', line 8

def assets
  @assets
end

#scratch_directoryObject (readonly)

Returns the value of attribute scratch_directory.



8
9
10
# File 'lib/passifier/storage.rb', line 8

def scratch_directory
  @scratch_directory
end

Instance Method Details

#cleanupObject

Clean up temp files



45
46
47
48
# File 'lib/passifier/storage.rb', line 45

def cleanup
  remove_temp_files
  remove_directory
end

#path(filename) ⇒ String

Path to the stored version

Parameters:

  • filename (String)

    The filename to return the path for

Returns:

  • (String)

    The full path to the file with the passed-in filename



20
21
22
# File 'lib/passifier/storage.rb', line 20

def path(filename)
  "#{@scratch_directory}/#{filename}"
end

#remove_zip(zip_path) ⇒ Object

Remove a zip archive

Parameters:

  • zip_path (String)

    The path of the archive to delete



52
53
54
# File 'lib/passifier/storage.rb', line 52

def remove_zip(zip_path)
  File.delete(zip_path) if File.exists?(zip_path)
end

#storeObject

Store the files for a group of pass assets

Parameters:

  • The (Array<Object>)

    pass asset objects to store files for



26
27
28
29
# File 'lib/passifier/storage.rb', line 26

def store
  ensure_directory_exists
  @assets.each { |asset| write_file(asset) }
end

#zip(zip_path) ⇒ String

Create a zip archive given a filename for the archive and a set of pass assets

Parameters:

  • zip_path (String)

    The desired output path

Returns:

  • (String)

    The full path of the resulting zip archive



34
35
36
37
38
39
40
41
42
# File 'lib/passifier/storage.rb', line 34

def zip(zip_path)
  remove_zip(zip_path) # ensure that older version is deleted if it exists
  Zip::ZipFile.open(zip_path, Zip::ZipFile::CREATE) do |zipfile|
    @assets.each do |asset| 
      zipfile.add(asset.filename, path(asset.filename))
    end
  end
  zip_path
end