diff --git a/README.md b/README.md index ccb6930..bf15f32 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 cerificates 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 cerificates 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 cerificate 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 i-phone via e-mail to view your pass. @@ -156,6 +156,22 @@ Sometime you might want to be able to use different certificates for different p .... ``` +### Using Different Compressor + +Sometime you might want to be able to another compressor from rubyzip. + +For the moment, only rubyzip is supported, but feel free to add your own into `lib/passbook/compressors/` and add it to the list of available compressors into `lib/passbook/compressors.rb` + +In order to change the default compressor use the configuration like that + +``` +Passbook.configure do |passbook| + ... + passbook.compressor = :rubyzip # or wathever you want and that is implemented + ... +end +``` + ### Push Notifications If you want to support passbook push notification updates you will need to configure the appropriate bits above. @@ -260,7 +276,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..63ec64d 100644 --- a/lib/passbook.rb +++ b/lib/passbook.rb @@ -1,13 +1,14 @@ require "passbook/version" require "passbook/pkpass" require "passbook/signer" +require 'passbook/compressor' require 'active_support/core_ext/module/attribute_accessors' require 'passbook/push_notification' require 'grocer/passbook_notification' 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, :compressor def self.configure yield self diff --git a/lib/passbook/compressor.rb b/lib/passbook/compressor.rb new file mode 100644 index 0000000..211f8f6 --- /dev/null +++ b/lib/passbook/compressor.rb @@ -0,0 +1,35 @@ +module Passbook + module Compressor + extend self + + def all_compressors + [:rubyzip] + end + + def get name + @compressors = {} unless defined?(@compressors) + @compressors[name] = spawn(name) unless @compressors.include?(name) + @compressors[name] + end + + private + + ## + # Spawn a Lookup of the given name. + # + def spawn name + Passbook::Compressor.const_get(classify_name(name)).new + end + + ## + # Convert an "underscore" version of a name into a "class" version. + # + def classify_name filename + filename.to_s.split("_").map{ |i| i[0...1].upcase + i[1..-1] }.join + end + end +end + +Passbook::Compressor.all_compressors.each do |name| + require "passbook/compressors/#{name}" +end \ No newline at end of file diff --git a/lib/passbook/compressors/base.rb b/lib/passbook/compressors/base.rb new file mode 100644 index 0000000..d237117 --- /dev/null +++ b/lib/passbook/compressors/base.rb @@ -0,0 +1,24 @@ +module Passbook + module Compressor + class Base + + def initialize + end + + ### + # Human-readable name of the compressors + # + def name + fail + end + + ### + # Output zip string + # + def outputZip pass, files, manifest, signature + fail + end + + end + end +end diff --git a/lib/passbook/compressors/rubyzip.rb b/lib/passbook/compressors/rubyzip.rb new file mode 100644 index 0000000..189f17a --- /dev/null +++ b/lib/passbook/compressors/rubyzip.rb @@ -0,0 +1,43 @@ +require 'passbook/compressors/base' +require 'zip' + +module Passbook + module Compressor + + class Rubyzip < Base + + ### + # Human-readable name of the compressors + # + def name + "RubyZip" + end + + ### + # Output zip string + # + def outputZip pass, files, manifest, signature + Zip::OutputStream.write_buffer do |zip| + zip.put_next_entry 'pass.json' + zip.write pass + zip.put_next_entry 'manifest.json' + zip.write manifest + zip.put_next_entry 'signature' + zip.write signature + + files.each do |file| + if file.class == Hash + zip.put_next_entry file[:name] + zip.print file[:content] + else + zip.put_next_entry File.basename(file) + zip.print IO.read(file) + end + end + end + end + + end + + end +end \ No newline at end of file diff --git a/lib/passbook/pkpass.rb b/lib/passbook/pkpass.rb index c8e51fd..fcc9446 100644 --- a/lib/passbook/pkpass.rb +++ b/lib/passbook/pkpass.rb @@ -60,7 +60,9 @@ def file(options = {}) def stream manifest, signature = build - outputZip manifest, signature + compressor = Passbook::Compressor.get Passbook.compressor || :rubyzip + + compressor.outputZip @pass, @manifest_files, manifest, signature end private @@ -94,27 +96,5 @@ def createManifest return sha1s.to_json end - - def outputZip manifest, signature - - Zip::OutputStream.write_buffer do |zip| - zip.put_next_entry 'pass.json' - zip.write @pass - zip.put_next_entry 'manifest.json' - zip.write manifest - zip.put_next_entry 'signature' - zip.write signature - - @manifest_files.each do |file| - if file.class == Hash - zip.put_next_entry file[:name] - zip.print file[:content] - else - zip.put_next_entry File.basename(file) - zip.print IO.read(file) - end - end - end - end end end