Class: Traquitana::Packager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir = '') ⇒ Packager



9
10
11
12
13
# File 'lib/packager.rb', line 9

def initialize(dir = '')
  @dir     = dir
  @id      = Time.now.strftime('%Y%m%d%H%M%S%L')
  @verbose = verbose
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/packager.rb', line 6

def id
  @id
end

#verboseObject

Returns the value of attribute verbose.



7
8
9
# File 'lib/packager.rb', line 7

def verbose
  @verbose
end

Instance Method Details

#list_fileObject



15
16
17
# File 'lib/packager.rb', line 15

def list_file
  "#{@id}.list"
end

#packObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/packager.rb', line 23

def pack
  list_path = "#{Dir.tmpdir}/#{self.list_file}"
  zip_path  = "#{Dir.tmpdir}/#{self.zip_file}"
  list      = Traquitana::Selector.new(@dir).files
  regex     = @dir.to_s.size < 1 ? '' : Regexp.new("^#{@dir}")

  # write list file
  STDOUT.puts "Creating the list file: #{list_path}" if @verbose

  File.open(list_path, 'w') do |file|
    file << list.map do |f|
      f.sub(regex, '')
    end.join("\n")
  end

  # write zip file
  STDOUT.puts "Creating the zip file : #{zip_path}" if @verbose

  Zip::File.open(zip_path, 'w') do |zip_file|
    for file in list
      strip = file.sub(regex, '')
      zip_file.add(strip, file)
    end
  end
  [list_path, zip_path]
end

#zip_fileObject



19
20
21
# File 'lib/packager.rb', line 19

def zip_file
  "#{@id}.zip"
end