Class: Downager::Archiver

Inherits:
Object
  • Object
show all
Defined in:
lib/downager/archiver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace: nil, input_file_array: nil, switcher: nil, root_path: nil) ⇒ Archiver

input_file_array is expected in the format [“full file path”, “directory file to be stored in”, “file_name”]

For example ["home/somewhere/file_1.txt", "1.1.1", "file_1.txt"]
The swticher is an object that allow you to swtich to a difference namespace, such as with the Apartment gem, it's optional


10
11
12
13
14
15
16
# File 'lib/downager/archiver.rb', line 10

def initialize(namespace: nil, input_file_array: nil, switcher: nil, root_path: nil)
  @namespace = namespace
  @input_file_array = input_file_array
  @switcher = switcher
  @root_path = root_path
  @switcher.switch(@namespace) unless @switcher.nil? || @namespace.nil?
end

Instance Attribute Details

#input_file_arrayObject

Returns the value of attribute input_file_array.



4
5
6
# File 'lib/downager/archiver.rb', line 4

def input_file_array
  @input_file_array
end

#namespaceObject

Returns the value of attribute namespace.



4
5
6
# File 'lib/downager/archiver.rb', line 4

def namespace
  @namespace
end

#root_pathObject

Returns the value of attribute root_path.



4
5
6
# File 'lib/downager/archiver.rb', line 4

def root_path
  @root_path
end

#switcherObject

Returns the value of attribute switcher.



4
5
6
# File 'lib/downager/archiver.rb', line 4

def switcher
  @switcher
end

Instance Method Details

#create_zipObject



19
20
21
22
23
24
25
26
# File 'lib/downager/archiver.rb', line 19

def create_zip
  create_output_directory
  Zip::File.open(root_path + "/#{namespace}/#{namespace}-#{Time.now.strftime('%Y-%m-%d-%H%M')}.zip" , Zip::File::CREATE) do |zipfile|
    input_file_array.each do |file|
      zipfile.add("#{file[0]}/#{file[1]}", file[2]) unless file.empty? || file.nil?
    end
  end
end