Class: PushPackage
- Inherits:
-
Object
- Object
- PushPackage
- Defined in:
- lib/push_package.rb,
lib/push_package/version.rb
Defined Under Namespace
Classes: InvalidIconsetError, InvalidParameterError
Constant Summary collapse
- REQUIRED_WEBSITE_PARAMS =
["websiteName", "websitePushID", "allowedDomains", "urlFormatString", "authenticationToken", "webServiceURL"]
- REQUIRED_ICONSET_FILES =
["icon_16x16.png", "[email protected]", "icon_32x32.png", "[email protected]", "icon_128x128.png", "[email protected]" ]
- VERSION =
'0.0.2'.freeze
Instance Attribute Summary collapse
-
#p12 ⇒ Object
readonly
Returns the value of attribute p12.
Instance Method Summary collapse
-
#initialize(website_params, iconset_path, certificate, password = nil) ⇒ PushPackage
constructor
A new instance of PushPackage.
- #save(output_path) ⇒ Object
Constructor Details
#initialize(website_params, iconset_path, certificate, password = nil) ⇒ PushPackage
Returns a new instance of PushPackage.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/push_package.rb', line 16 def initialize(website_params, iconset_path, certificate, password = nil) raise InvalidParameterError unless valid_website_params?(website_params) raise InvalidIconsetError unless valid_iconset?(iconset_path) raise ArgumentError unless certificate @website_params = website_params @iconset_path = iconset_path if certificate.respond_to?(:read) cert_data = certificate.read certificate.rewind if certificate.respond_to?(:rewind) else cert_data = File.read(certificate) end @p12 = OpenSSL::PKCS12.new(cert_data, password) end |
Instance Attribute Details
#p12 ⇒ Object (readonly)
Returns the value of attribute p12.
14 15 16 |
# File 'lib/push_package.rb', line 14 def p12 @p12 end |
Instance Method Details
#save(output_path) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/push_package.rb', line 33 def save(output_path) Dir.mktmpdir('pushPackage') do |dir| json = File.open(dir + '/website.json', 'w+') json.write(JSON.dump(@website_params)) json.close Dir.mkdir(File.join(dir,'icon.iconset')) Dir.glob(@iconset_path + '/*.png').each do |icon| FileUtils.cp(icon, dir + '/icon.iconset/') end manifest_keys = REQUIRED_ICONSET_FILES.map{|f| 'icon.iconset/' + f } manifest_keys << 'website.json' manifest_values = manifest_keys.map {|file| Digest::SHA1.file(File.join(dir, file)).hexdigest } manifest_data = Hash[manifest_keys.zip(manifest_values)].to_json File.open(dir + '/manifest.json', 'w+') do |manifest| manifest.write(manifest_data) end #use the certificate to create a pkcs7 detached signature signature = OpenSSL::PKCS7::sign(@p12.certificate, @p12.key, manifest_data, [], OpenSSL::PKCS7::BINARY | OpenSSL::PKCS7::DETACHED) File.open(dir + '/signature', 'wb+') do |file| file << signature.to_der end `pushd #{dir}; zip -r #{output_path} ./; popd` end File.open(output_path, 'r') end |