passbook-iid 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.travis.yml +7 -0
  2. data/Gemfile +17 -0
  3. data/Gemfile.lock +65 -0
  4. data/LICENSE +22 -0
  5. data/README.md +290 -0
  6. data/Rakefile +36 -0
  7. data/VERSION +1 -0
  8. data/bin/pk +22 -0
  9. data/lib/commands/build.rb +62 -0
  10. data/lib/commands/commands.rb +31 -0
  11. data/lib/commands/generate.rb +44 -0
  12. data/lib/commands/templates/boarding-pass.json +56 -0
  13. data/lib/commands/templates/coupon.json +33 -0
  14. data/lib/commands/templates/event-ticket.json +33 -0
  15. data/lib/commands/templates/generic.json +33 -0
  16. data/lib/commands/templates/store-card.json +33 -0
  17. data/lib/passbook.rb +15 -0
  18. data/lib/passbook/pkpass.rb +120 -0
  19. data/lib/passbook/push_notification.rb +10 -0
  20. data/lib/passbook/signer.rb +40 -0
  21. data/lib/passbook/version.rb +3 -0
  22. data/lib/rack/passbook_rack.rb +98 -0
  23. data/lib/rails/generators/passbook/config/config_generator.rb +16 -0
  24. data/lib/rails/generators/passbook/config/templates/initializer.rb +13 -0
  25. data/lib/utils/command_utils.rb +12 -0
  26. data/passbook.gemspec +110 -0
  27. data/spec/data/icon.png +0 -0
  28. data/spec/data/icon@2x.png +0 -0
  29. data/spec/data/logo.png +0 -0
  30. data/spec/data/logo@2x.png +0 -0
  31. data/spec/lib/commands/build_spec.rb +92 -0
  32. data/spec/lib/commands/commands_spec.rb +102 -0
  33. data/spec/lib/commands/commands_spec_helper.rb +69 -0
  34. data/spec/lib/commands/generate_spec.rb +72 -0
  35. data/spec/lib/passbook/pkpass_spec.rb +108 -0
  36. data/spec/lib/passbook/push_notification_spec.rb +22 -0
  37. data/spec/lib/passbook/signer_spec.rb +84 -0
  38. data/spec/lib/rack/passbook_rack_spec.rb +233 -0
  39. data/spec/spec_helper.rb +9 -0
  40. metadata +216 -0
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - ruby-head
6
+ - 2.0.0
7
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in passbook.gemspec
4
+ gem 'rubyzip', '~> 1.0.0'
5
+ gem 'grocer'
6
+ gem 'commander'
7
+ gem 'terminal-table'
8
+
9
+ group :test, :development do
10
+ gem 'rack-test'
11
+ gem 'activesupport'
12
+ gem 'jeweler', :git => 'git://github.com/foxnewsnetwork/jeweler.git', :branch => 'ruby-2.0.0-ifying'
13
+ gem 'simplecov'
14
+ gem 'rspec'
15
+ gem 'rake'
16
+ gem 'yard'
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ GIT
2
+ remote: git://github.com/foxnewsnetwork/jeweler.git
3
+ revision: f05c62e168cfc29bd82cebe06df8fd11e1ef09ee
4
+ branch: ruby-2.0.0-ifying
5
+ specs:
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.3.0.pre)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ activesupport (3.2.14)
16
+ i18n (~> 0.6, >= 0.6.4)
17
+ multi_json (~> 1.0)
18
+ commander (4.1.5)
19
+ highline (~> 1.6.11)
20
+ diff-lcs (1.2.4)
21
+ git (1.2.6)
22
+ grocer (0.4.1)
23
+ highline (1.6.19)
24
+ i18n (0.6.5)
25
+ json (1.8.0)
26
+ json (1.8.0-java)
27
+ multi_json (1.8.0)
28
+ rack (1.5.2)
29
+ rack-test (0.6.2)
30
+ rack (>= 1.0)
31
+ rake (10.1.0)
32
+ rdoc (4.0.1)
33
+ json (~> 1.4)
34
+ rspec (2.14.1)
35
+ rspec-core (~> 2.14.0)
36
+ rspec-expectations (~> 2.14.0)
37
+ rspec-mocks (~> 2.14.0)
38
+ rspec-core (2.14.5)
39
+ rspec-expectations (2.14.3)
40
+ diff-lcs (>= 1.1.3, < 2.0)
41
+ rspec-mocks (2.14.3)
42
+ rubyzip (1.0.0)
43
+ simplecov (0.7.1)
44
+ multi_json (~> 1.0)
45
+ simplecov-html (~> 0.7.1)
46
+ simplecov-html (0.7.1)
47
+ terminal-table (1.4.5)
48
+ yard (0.8.7.2)
49
+
50
+ PLATFORMS
51
+ java
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ activesupport
56
+ commander
57
+ grocer
58
+ jeweler!
59
+ rack-test
60
+ rake
61
+ rspec
62
+ rubyzip (~> 1.0.0)
63
+ simplecov
64
+ terminal-table
65
+ yard
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Thomas Lauro
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,290 @@
1
+ [![Build Status](https://travis-ci.org/lgleasain/passbook.png)](https://travis-ci.org/lgleasain/passbook)
2
+
3
+ # passbook
4
+
5
+ The passbook gem let's you create a pkpass for passbook in iOS 6+
6
+
7
+ ## Installation
8
+
9
+ Include the passbook gem in your project.
10
+
11
+ IE: In your Gemfile
12
+ ```
13
+ gem 'passbook'
14
+ ```
15
+
16
+ ## Quick Start
17
+
18
+ If you want to jump in without having to integrate this into your code you can use the command line options to get started. Start by installing the gem
19
+
20
+ ```
21
+ gem install passbook
22
+ ```
23
+
24
+ Then go to a directory that you want to generate your pass under and use the "pk generate command". (note: do not use spaces in your passname or else strange things will happen.)
25
+
26
+ ```
27
+ pk generate your_pass_name
28
+ ```
29
+
30
+ This will generate a directory called your_pass_name. Edit your pass.json file in the your_pass_directory to have a valid team identifier and passTypeIdentifier and create your certificates if you haven't yet. [See this article for information on how to do this.](http://www.raywenderlich.com/20734/beginning-passbook-part-1#more-20734)
31
+
32
+ Assuming that you have put your certificate files etc. in your working directory.
33
+
34
+ ```
35
+ pk build your_pass_name -w ./wwdc.pem -c ./your_pass_name.p12 -p ''
36
+ ```
37
+ The wwdc.pem file is the exported Apple World Wide Developer Relations Certification Authority certificate file from your key manager and the your_pass_name.p12 is the exported p12 file from your pass certificate.
38
+
39
+ If you are not building your passes on a mac or just prefer to use the pass certificate and key pem file.
40
+
41
+ ```
42
+ pk build passbook_gem_name -w ./wwdc.pem -c ./your_pass_name_certificate.pem -k your_pass_name_key.pem -p '12345'
43
+ ```
44
+
45
+ Now you can drag the file over to a simulator or send it to your iPhone via e-mail to view your pass.
46
+
47
+
48
+ ## Configuration
49
+
50
+ Create initializer
51
+ ```
52
+ rails g passbook:config
53
+ or with params
54
+ rails g passbook:config [Absolute path to the wwdc cert file] [Absolute path to your cert.p12 file] [Password for your certificate]
55
+ ```
56
+
57
+ Configure your config/initializers/passbook.rb
58
+ ```
59
+ Passbook.configure do |passbook|
60
+ passbook.wwdc_cert = Rails.root.join('wwdc_cert.pem')
61
+ passbook.p12_certificate = Rails.root.join('cert.p12')
62
+ passbook.p12_password = 'cert password'
63
+ end
64
+ ```
65
+
66
+ If you are running this on a different machine from what you used to create your WWDC keys
67
+ ```
68
+ Passbook.configure do |passbook|
69
+ passbook.wwdc_cert = Rails.root.join('wwdc_cert.pem')
70
+ passbook.p12_key = Rails.root.join('key.pem')
71
+ passbook.p12_certificate = Rails.root.join('certificate.pem')
72
+ passbook.p12_password = 'cert password'
73
+ end
74
+ ```
75
+ If you are using Sinatra you can place this in the file you are executing or in a file that you do a require on. You would also not reference Rails.root when specifying your file path.
76
+
77
+ If You are doing push notifications then you will need to add some extra configuration options, namely a push notification certificate and a notification gateway certificate. Look at the Grocer gem documentation to find information on how to create this certificate. Settings you will want to use for the notification gateway are either 'gateway.push.apple.com' for production, 'gateway.sandbox.push.apple.com' for development and 'localhost' for unit tests.
78
+ ```
79
+ Passbook.configure do |passbook|
80
+ .....other settings.....
81
+ passbook.notification_gateway = 'gateway.push.apple.com'
82
+ passbook.notification_cert = 'lib/assets/my_notification_cert.pem'
83
+ end
84
+ ```
85
+
86
+ If you want to also support the push notification endpoints you will also need to include the Rack::PassbookRack middleware. In rails your config will look something like this.
87
+ ```
88
+ config.middleware.use Rack::PassbookRack
89
+ ```
90
+
91
+ ## Usage
92
+
93
+ Please refer to apple iOS dev center for how to build cert and json. [This article is also helpful.](http://www.raywenderlich.com/20734/beginning-passbook-part-1#more-20734)
94
+ ```
95
+ pass = Passbook::PKPass.new 'your json data'
96
+
97
+ # Add file from disk
98
+ pass.addFile 'file_path'
99
+
100
+ # Add file from memory
101
+ file[:name] = 'file name'
102
+ file[:content] = 'whatever you want'
103
+ pass.addFile file
104
+
105
+ # Add multiple files
106
+ pass.addFiles [file_path_1, file_path_2, file_path_3]
107
+
108
+ # Add multiple files from memory
109
+ pass.addFiles [{name: 'file1', content: 'content1'}, {name: 'file2', content: 'content2'}, {name: 'file3', content: 'content3'}]
110
+
111
+ # Output a Tempfile
112
+
113
+ pkpass = pass.file
114
+ send_file pkpass.path, type: 'application/vnd.apple.pkpass', disposition: 'attachment', filename: "pass.pkpass"
115
+
116
+ # Or a stream
117
+
118
+ pkpass = pass.stream
119
+ send_data pkpass.string, type: 'application/vnd.apple.pkpass', disposition: 'attachment', filename: "pass.pkpass"
120
+
121
+ ```
122
+ If you are using Sinatra you will need to include the 'active_support' gem and will need to require 'active_support/json/encoding'. Here is an example using the streaming mechanism.
123
+
124
+ ```
125
+ require 'sinatra'
126
+ require 'passbook'
127
+ require 'active_support/json/encoding'
128
+
129
+ Passbook.configure do |passbook|
130
+ passbook.p12_password = '12345'
131
+ passbook.p12_key = 'passkey.pem'
132
+ passbook.p12_certificate = 'passcertificate.pem'
133
+ passbook.wwdc_cert = 'WWDR.pem'
134
+ end
135
+
136
+ get '/passbook' do
137
+ pass = # valid passbook json. refer to the wwdc documentation.
138
+ passbook = Passbook::PKPass.new pass
139
+ passbook.addFiles ['logo.png', 'logo@2x.png', 'icon.png', 'icon@2x.png']
140
+ response['Content-Type'] = 'application/vnd.apple.pkpass'
141
+ attachment 'mypass.pkpass'
142
+ passbook.stream.string
143
+ end
144
+ ```
145
+
146
+ We will try to make this cleaner in subsequent releases.
147
+
148
+ ### Using Different Certificates For Different Passes
149
+
150
+ Sometime you might want to be able to use different certificates for different passes. This can be done by passing in a Signer class into your PKPass initializer like so
151
+
152
+ ```
153
+ signer = Passbook::Signer.new {certificate: some_cert, password: some_password, key: some_key, wwdc_cert: some_wwdc_cert}
154
+ pk_pass = Passbook::PKPass.new your_json_data, signer
155
+
156
+ ....
157
+ ```
158
+
159
+ ### Push Notifications
160
+
161
+ If you want to support passbook push notification updates you will need to configure the appropriate bits above.
162
+
163
+ In order to support push notifications you will need to have a basic understanding of the way that push notifications work and how the data is passed back and forth. See [this](http://developer.apple.com/library/ios/#Documentation/UserExperience/Conceptual/PassKit_PG/Chapters/Creating.html) for basic information about passes and [this](http://developer.apple.com/library/ios/#Documentation/UserExperience/Conceptual/PassKit_PG/Chapters/Updating.html#//apple_ref/doc/uid/TP40012195-CH5-SW1) to understand the information that needs to be exchanged between each device and your application to support the update service.
164
+
165
+ Your pass will need to have a field called 'webServiceURL' with the base url to your site and a field called 'authenticationToken'. The json snippet should look like this. Note that your url needs to be a valid signed https endpoint for production. You can put your phone in dev mode to test updates against a insecure http endpoint (under settings => developer => passkit testing).
166
+
167
+ ```
168
+ ...
169
+ "webserviceURL" : "https://www.honeybadgers.com/",
170
+ "authenticationToken" : "yummycobras"
171
+ ...
172
+ ```
173
+
174
+ Passbook includes rack middleware to make the job of supporting the passbook endpoints easier. You will need to configure the middleware as outlined above and then implement a class called Passbook::PassbookNotification. Below is an annotated implementation.
175
+
176
+ ```
177
+ module Passbook
178
+ class PassbookNotification
179
+
180
+ # This is called whenever a new pass is saved to a users passbook or the
181
+ # notifications are re-enabled. You will want to persist these values to
182
+ # allow for updates on subsequent calls in the call chain. You can have
183
+ # multiple push tokens and serial numbers for a specific
184
+ # deviceLibraryIdentifier.
185
+
186
+ def self.register_pass(options)
187
+ the_passes_serial_number = options['serialNumber']
188
+ the_devices_device_library_identifier = options['deviceLibraryIdentifier']
189
+ the_devices_push_token = options['pushToken']
190
+ the_pass_type_identifier = options["passTypeIdentifier"]
191
+ the_authentication_token = options['authToken']
192
+
193
+ # this is if the pass registered successfully
194
+ # change the code to 200 if the pass has already been registered
195
+ # 404 if pass not found for serialNubmer and passTypeIdentifier
196
+ # 401 if authorization failed
197
+ # or another appropriate code if something went wrong.
198
+ {:status => 201}
199
+ end
200
+
201
+ # This is called when the device receives a push notification from apple.
202
+ # You will need to return the serial number of all passes associated with
203
+ # that deviceLibraryIdentifier.
204
+
205
+ def self.passes_for_device(options)
206
+ device_library_identifier = options['deviceLibraryIdentifier']
207
+ passes_updated_since = options['passesUpdatedSince']
208
+
209
+ # the 'lastUpdated' uses integers values to tell passbook if the pass is
210
+ # more recent than the current one. If you just set it is the same value
211
+ # every time the pass will update and you will get a warning in the log files.
212
+ # you can use the time in milliseconds, a counter or any other numbering scheme.
213
+ # you then also need to return an array of serial numbers.
214
+ {'lastUpdated' => '1', 'serialNumbers' => ['various', 'serial', 'numbers']}
215
+ end
216
+
217
+ # this is called when a pass is deleted or the user selects the option to disable pass updates.
218
+ def self.unregister_pass(options)
219
+ # a solid unique pair of identifiers to identify the pass are
220
+ serial_number = options['serialNumber']
221
+ device_library_identifier = options['deviceLibraryIdentifier']
222
+ the_pass_type_identifier = options["passTypeIdentifier"]
223
+ the_authentication_token = options['authToken']
224
+ # return a status 200 to indicate that the pass was successfully unregistered.
225
+ {:status => 200}
226
+ end
227
+
228
+ # this returns your updated pass
229
+ def self.latest_pass(options)
230
+ the_pass_serial_number = options['serialNumber']
231
+ # create your PkPass the way you did when your first created the pass.
232
+ # you will want to return
233
+ my_pass = PkPass.new 'your pass json'
234
+ # you will want to return the string from the stream of your PkPass object.
235
+ mypass.stream.string
236
+ end
237
+
238
+ # This is called whenever there is something from the update process that is a warning
239
+ # or error
240
+ def self.passbook_log(log)
241
+ # this is a VERY crude logging example. use the logger of your choice here.
242
+ p "#{Time.now} #{log}"
243
+ end
244
+
245
+ end
246
+ end
247
+
248
+ ```
249
+
250
+ To send a push notification for a updated pass simply call Passbook::PassbookPushNotification.send_notifications_for_promotion with the push token for the pass you are updating
251
+
252
+ ```
253
+ Passbook::PassbookPushNotification.send_notifications_for_promotion the_pass_push_token
254
+
255
+ ```
256
+
257
+ Apple will send out a notification to your phone (usually within 15 minutes or less), which will cause the phone that this push notification is associated with to make a call to your server to get pass serial numbers and to then get the updated pass. Each phone/pass combination has it's own push token whch will require a separate call for every phone that has push notifications enabled for a pass (this is an Apple thing). In the future we may look into offering background process support for this as part of this gem. For now, if you have a lot of passes to update you will need to do this yourself.
258
+
259
+ ## Tests
260
+
261
+ To launch tests :
262
+ ```
263
+ bundle exec rake spec
264
+ ```
265
+
266
+ ## Contributing
267
+
268
+ 1. Fork it
269
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
270
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
271
+ 4. Push to the branch (`git push origin my-new-feature`)
272
+ 5. Create new Pull Request
273
+
274
+ ## Changelog
275
+
276
+ ### 0.0.4
277
+ Allow passbook gem to return a ZipOutputStream (needed when garbage collector delete tempfile before beeing able to use it) [Thx to applidget]
278
+
279
+ ### 0.2.0
280
+ Adding support for push notification updates for passes.
281
+
282
+ ### 0.4.0
283
+ Adding support for using multiple signatures per gem configuration and introducing command line helpers. More in [this](http://www.polyglotprogramminginc.com/allowing-for-more-signature-flexibility-in-the-ruby-passbook-gem/) blog post.
284
+
285
+ License
286
+ -------
287
+
288
+ passbook is released under the MIT license:
289
+
290
+ * http://www.opensource.org/licenses/MIT
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env rake
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "passbook"
16
+ gem.homepage = "http://github.com/frozon/passbook"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A IOS Passbook generator.}
19
+ gem.description = %Q{This gem allows you to create IOS Passbooks. Unlike some, this works with Rails but does not require it.}
20
+ gem.email = ['thomas@lauro.fr', 'lgleason@polyglotprogramminginc.com']
21
+ gem.authors = ['Thomas Lauro', 'Lance Gleason']
22
+ gem.executables = ['pk']
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rspec/core'
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new(:spec) do |spec|
30
+ spec.pattern = FileList['spec/**/*_spec.rb']
31
+ end
32
+
33
+ task :default => :spec
34
+
35
+ require 'yard'
36
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.0