Class: AmpkWriter

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

Instance Method Summary collapse

Constructor Details

#initialize(path, private_key, &block) ⇒ AmpkWriter

Returns a new instance of AmpkWriter.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/ampk/writer.rb', line 4

def initialize(path, private_key, &block)
  @fp = File.open(path,'wb')
  write("AMPK")
  @private_key = private_key
  if block_given?
    begin
      yield(self)
    ensure
      close()
    end
  end
end

Instance Method Details

#add_entity(name, data, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ampk/writer.rb', line 16

def add_entity(name, data, options = {})
  write_entry("NAME", name)
  write_entry("DLEN", size(data).to_s)
  
  if data.respond_to?(:encoding)
    write_entry("ENCD", data.encoding.name)
    data = data.force_encoding("ASCII-8BIT")
  end
  
  write_entry("TYPE", options[:type] || 'application/octet-stream')
  if options[:sign] || options[:signature]
    write_entry("SIGN", options[:signature] || @private_key.sign(data))
  end
  filter = ""
  if options[:encrypt] || options[:crypt]
    filter << "C"
    data = @private_key.encrypt(data)
  end
  orig_size = size(data)
  if options[:compress] and size(data) > 256
    filter << "Z"
    data = Zlib::Deflate.deflate(data)
  end
  unless filter.empty?
    write_entry('FILT', filter)
  end
  write_size = size(data)
  write_entry("DATA", data)
end

#closeObject



46
47
48
49
# File 'lib/ampk/writer.rb', line 46

def close
  write('ENDS')
  @fp.close()
end