Class: Passbook::Pkpass

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pass_type_id, serial_number) ⇒ Pkpass

Returns a new instance of Pkpass.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/passbook/pkpass.rb', line 29

def initialize(pass_type_id, serial_number)
  self.pass_type_id = pass_type_id
  self.serial_number = serial_number
  self.translations = Hash.new
  raise(ArgumentError, "Don't forget to run the generator to create the initializer") unless Config.instance.pass_config
  self.config = Config.instance.pass_config[self.pass_type_id]
  raise(ArgumentError, "Could not find configuration for #{self.pass_type_id}") unless self.config

  if self.config.include? :files
    self.files = self.config['files'].dup
  else
    self.files = Config.instance.load_files self.config['template_path']
  end

  if self.files.include? 'pass.json'
    self.json = JSON.parse(self.files['pass.json'])
  else
    self.json = {}
    puts "Warning: your template_path does not contain pass.json"
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



27
28
29
# File 'lib/passbook/pkpass.rb', line 27

def config
  @config
end

#filesObject

Returns the value of attribute files.



27
28
29
# File 'lib/passbook/pkpass.rb', line 27

def files
  @files
end

#jsonObject

Returns the value of attribute json.



27
28
29
# File 'lib/passbook/pkpass.rb', line 27

def json
  @json
end

#pass_type_idObject

Returns the value of attribute pass_type_id.



27
28
29
# File 'lib/passbook/pkpass.rb', line 27

def pass_type_id
  @pass_type_id
end

#serial_numberObject

Returns the value of attribute serial_number.



27
28
29
# File 'lib/passbook/pkpass.rb', line 27

def serial_number
  @serial_number
end

#translationsObject

Returns the value of attribute translations.



27
28
29
# File 'lib/passbook/pkpass.rb', line 27

def translations
  @translations
end

Instance Method Details

#add_file(filename, content) ⇒ Object



51
52
53
# File 'lib/passbook/pkpass.rb', line 51

def add_file filename, content
  self.files[filename] = content
end

#add_translation_string(source, destination, language) ⇒ Object



55
56
57
58
# File 'lib/passbook/pkpass.rb', line 55

def add_translation_string source, destination, language
  self.translations[language] = Hash.new unless self.translations.include?(language)
  self.translations[language][source] = destination
end

#compress_pass_fileObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/passbook/pkpass.rb', line 98

def compress_pass_file
  stringio = Zip::ZipOutputStream::write_buffer do |z|
    self.files.each do |filename, content|
      z.put_next_entry filename
      z.print content
    end
  end
  stringio.set_encoding "binary"
  stringio.rewind
  stringio
  # stringio.sysread
end

#generate_json_manifestObject



84
85
86
87
88
89
90
# File 'lib/passbook/pkpass.rb', line 84

def generate_json_manifest
  manifest = {}
  self.files.each do |filename, content|
    manifest[filename] = Digest::SHA1.hexdigest(content)
  end
  self.files['manifest.json'] = JSON.pretty_generate(manifest)
end

#packageObject



60
61
62
63
64
65
66
67
68
# File 'lib/passbook/pkpass.rb', line 60

def package
  #TODO: write a library that checks that all the right files are included in the package
  #those requirements are going to be different depending on pass_type_id
  self.write_json
  self.write_translation_strings
  self.generate_json_manifest
  self.sign_manifest
  self.compress_pass_file
end

#sign_manifestObject



92
93
94
95
96
# File 'lib/passbook/pkpass.rb', line 92

def sign_manifest
  flag = OpenSSL::PKCS7::BINARY|OpenSSL::PKCS7::DETACHED
  signed = OpenSSL::PKCS7::sign(config['p12_certificate'].certificate, config['p12_certificate'].key, self.files['manifest.json'], [Config.instance.wwdr_certificate], flag)
  self.files['signature'] = signed.to_der.force_encoding('UTF-8')
end

#write_jsonObject



70
71
72
# File 'lib/passbook/pkpass.rb', line 70

def write_json
  self.files['pass.json'] = JSON.pretty_generate(self.json)
end

#write_translation_stringsObject



74
75
76
77
78
79
80
81
82
# File 'lib/passbook/pkpass.rb', line 74

def write_translation_strings
  self.translations.each do |language, trans|
    self.files["#{language}.lproj/pass.strings"] ||= ""
    trans.each do |key, value|
      #TODO: escape key and value
      self.files["#{language}.lproj/pass.strings"] << "\n\"#{key}\" = \"#{value}\";"
    end
  end
end