diff --git a/README.md b/README.md index 9b23833..0de9b05 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Then go to a directory that you want to generate your pass under and use the "pk pk generate your_pass_name ``` -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) +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) Assuming that you have put your certificate files etc. in your working directory. @@ -40,7 +40,7 @@ If you are not building your passes on a mac or just prefer to use the pass cert ``` pk build passbook_gem_name -w ./wwdc.pem -c ./your_pass_name_certificate.pem -k your_pass_name_key.pem -p '12345' -``` +``` Now you can drag the file over to a simulator or send it to your iPhone via e-mail to view your pass. @@ -79,6 +79,7 @@ If You are doing push notifications then you will need to add some extra configu Passbook.configure do |passbook| .....other settings..... passbook.notification_gateway = 'gateway.push.apple.com' + passbook.notification_passphrase = 'my_hard_password' (optional) passbook.notification_cert = 'lib/assets/my_notification_cert.pem' end ``` @@ -232,7 +233,7 @@ module Passbook # you will want to return my_pass = PkPass.new 'your pass json' # you will want to return the string from the stream of your PkPass object. - mypass.stream.string + {:status => 200, :latest_pass => mypass.stream.string, :last_modified => '1442120893'} end # This is called whenever there is something from the update process that is a warning @@ -247,10 +248,10 @@ end ``` -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 +To send a push notification for a updated pass simply call Passbook::PushNotification.send_notification with the push token for the device you are updating ``` - Passbook::PassbookPushNotification.send_notifications_for_promotion the_pass_push_token + Passbook::PushNotification.send_notification the_device_push_token ``` @@ -260,7 +261,7 @@ Apple will send out a notification to your phone (usually within 15 minutes or l To launch tests : ``` - bundle exec rake spec + bundle exec rake spec ``` ## Contributing diff --git a/lib/passbook.rb b/lib/passbook.rb index c345264..b5936f6 100644 --- a/lib/passbook.rb +++ b/lib/passbook.rb @@ -7,7 +7,7 @@ require 'rack/passbook_rack' module Passbook - mattr_accessor :p12_certificate, :p12_password, :wwdc_cert, :p12_key, :notification_cert, :notification_gateway + mattr_accessor :p12_certificate, :p12_password, :wwdc_cert, :p12_key, :notification_cert, :notification_gateway, :notification_passphrase def self.configure yield self diff --git a/lib/passbook/push_notification.rb b/lib/passbook/push_notification.rb index 4a3cd19..79a2c68 100644 --- a/lib/passbook/push_notification.rb +++ b/lib/passbook/push_notification.rb @@ -1,7 +1,7 @@ module Passbook class PushNotification def self.send_notification(device_token) - pusher = Grocer.pusher({:certificate => Passbook.notification_cert, :gateway => Passbook.notification_gateway}) + pusher = Grocer.pusher({:certificate => Passbook.notification_cert, :passphrase => Passbook.notification_passphrase || "", :gateway => Passbook.notification_gateway}) notification = Grocer::PassbookNotification.new(:device_token => device_token) pusher.push notification diff --git a/lib/rack/passbook_rack.rb b/lib/rack/passbook_rack.rb index 4536571..3c8b622 100644 --- a/lib/rack/passbook_rack.rb +++ b/lib/rack/passbook_rack.rb @@ -15,7 +15,7 @@ def call(env) when 'device_register_delete' if env['REQUEST_METHOD'] == 'POST' [Passbook::PassbookNotification. - register_pass(method_and_params[:params].merge! JSON.parse(env['rack.input'].read 1000))[:status], + register_pass(method_and_params[:params].merge! JSON.parse(env['rack.input'].read 1000))[:status], {}, ['']] elsif env['REQUEST_METHOD'] == 'DELETE' [Passbook::PassbookNotification.unregister_pass(method_and_params[:params])[:status], {}, {}] @@ -25,12 +25,12 @@ def call(env) [response ? 200 : 204, {}, [response.to_json]] when 'latest_pass' response = Passbook::PassbookNotification.latest_pass(method_and_params[:params]) - if response - [200, {'Content-Type' => 'application/vnd.apple.pkpass', - 'Content-Disposition' => 'attachment', - 'filename' => "#{method_and_params[:params]['serialNumber']}.pkpass"}, [response]] + if response[:status] == 200 + [200, {'Content-Type' => 'application/vnd.apple.pkpass', + 'Content-Disposition' => 'attachment', + 'filename' => "#{method_and_params[:params]['serialNumber']}.pkpass","last-modified" => response[:last_modified]}, [response[:latest_pass]]] else - [204, {}, {}] + [response[:status], {}, {}] end when 'log' Passbook::PassbookNotification.passbook_log JSON.parse(env['rack.input'].read 10000) @@ -69,10 +69,10 @@ def find_method(path) end end - return nil + return nil end - private + private def method_and_params_hash(method, path) parsed_path = path.split '/' diff --git a/spec/lib/passbook/push_notification_spec.rb b/spec/lib/passbook/push_notification_spec.rb index 37b3a7e..6c56282 100644 --- a/spec/lib/passbook/push_notification_spec.rb +++ b/spec/lib/passbook/push_notification_spec.rb @@ -5,15 +5,16 @@ context 'send notification' do let(:grocer_pusher) {double 'Grocer'} - let(:notification) {double 'Grocer::Notification'} - let(:notification_settings) {{:certificate => './notification_cert.pem', :gateway => 'honeybadger.apple.com'}} + let(:notification) {double 'Grocer::Notification'} + let(:notification_settings) {{:certificate => './notification_cert.pem', :gateway => 'honeybadger.apple.com', :passphrase => 'ah@rdvintAge'}} before :each do Passbook.should_receive(:notification_cert).and_return './notification_cert.pem' - Grocer::PassbookNotification.should_receive(:new).with(:device_token => 'my token').and_return notification + Grocer::PassbookNotification.should_receive(:new).with(:device_token => 'my token').and_return notification grocer_pusher.should_receive(:push).with(notification).and_return 55 Grocer.should_receive(:pusher).with(notification_settings).and_return grocer_pusher Passbook.should_receive(:notification_gateway).and_return 'honeybadger.apple.com' + Passbook.should_receive(:notification_passphrase).and_return 'ah@rdvintAge' end subject {Passbook::PushNotification.send_notification('my token')} diff --git a/spec/lib/rack/passbook_rack_spec.rb b/spec/lib/rack/passbook_rack_spec.rb index 4a01fb4..d6e07f5 100644 --- a/spec/lib/rack/passbook_rack_spec.rb +++ b/spec/lib/rack/passbook_rack_spec.rb @@ -48,7 +48,7 @@ its([:params]) {should eq(register_delete_params) } end - it_behaves_like 'a method that can handle non passbook urls' + it_behaves_like 'a method that can handle non passbook urls' end @@ -144,7 +144,7 @@ context 'get latest pass' do context 'valid pass' do - let(:raw_pass) {'some url encoded text'} + let(:raw_pass) {{:status => 200, :latest_pass => 'some url encoded text', :last_modified => '1442401010'}} before do Passbook::PassbookNotification.should_receive(:latest_pass).with(latest_pass_params). @@ -154,15 +154,15 @@ subject {last_response} its(:status) {should eq 200} - its(:header) {should eq({'Content-Type' => 'application/vnd.apple.pkpass', - 'Content-Disposition' => 'attachment', 'filename' => '27-1.pkpass', 'Content-Length' => '21'})} - its(:body) {should eq raw_pass} + its(:header) {should eq({'Content-Type' => 'application/vnd.apple.pkpass', + 'Content-Disposition' => 'attachment', 'filename' => '27-1.pkpass', 'last-modified' => '1442401010', 'Content-Length' => '21'})} + its(:body) {should eq raw_pass[:latest_pass]} end context 'no pass' do before do Passbook::PassbookNotification.should_receive(:latest_pass).with(latest_pass_params). - and_return(nil) + and_return({:status => 204, :latest_pass => nil}) get latest_pass_path end @@ -213,7 +213,7 @@ subject {last_response} its(:status) {should eq 200} its(:body) {should eq 'test app'} - end + end end end @@ -224,7 +224,7 @@ def app test_app = lambda do |env| [200, {}, 'test app'] - end + end Rack::PassbookRack.new test_app end