Class: Passbook::Pkpass

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

Overview

Pkpass is the class responsible for managing the contect of a pkpass and also signing the package

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)


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

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.



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

def config
  @config
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

#jsonObject

Returns the value of attribute json.



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

def json
  @json
end

#pass_type_idObject

Returns the value of attribute pass_type_id.



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

def pass_type_id
  @pass_type_id
end

#serial_numberObject

Returns the value of attribute serial_number.



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

def serial_number
  @serial_number
end

#translationsObject

Returns the value of attribute translations.



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

def translations
  @translations
end

Instance Method Details

#add_file(filename, content) ⇒ Object

Add a file to your pkpass

example:

pass.add_file "stripe.png", image_content

Parameters:

filename

A String for the name of the file. It can contain a folder, so it is really a relative path within the pkpass

content

Binary content for what will be inside that file



62
63
64
# File 'lib/passbook/pkpass.rb', line 62

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

#add_translation_string(source, destination, language) ⇒ Object



66
67
68
69
# File 'lib/passbook/pkpass.rb', line 66

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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/passbook/pkpass.rb', line 114

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



98
99
100
101
102
103
104
# File 'lib/passbook/pkpass.rb', line 98

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



71
72
73
74
75
76
77
78
79
# File 'lib/passbook/pkpass.rb', line 71

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



107
108
109
110
111
# File 'lib/passbook/pkpass.rb', line 107

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



82
83
84
# File 'lib/passbook/pkpass.rb', line 82

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

#write_translation_stringsObject



87
88
89
90
91
92
93
94
95
# File 'lib/passbook/pkpass.rb', line 87

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