Class: PushPackage

Inherits:
Object
  • Object
show all
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 =
'1.0.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(website_params, iconset_path, certificate, password = nil, intermediate_cert = nil) ⇒ PushPackage

Returns a new instance of PushPackage.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/push_package.rb', line 18

def initialize(website_params, iconset_path, certificate, password = nil, intermediate_cert = 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.to_s
  @extra_certs = nil

  if certificate.respond_to?(:read)
    cert_data = certificate.read
    certificate.rewind if certificate.respond_to?(:rewind)
  else
    cert_data = File.binread(certificate)
  end

  if defined?(JRUBY_VERSION)
    #ensure binary data for jruby.
    cert_data.force_encoding(Encoding::ASCII_8BIT)
  end
  if pem?(cert_data)
    @certificate = OpenSSL::X509::Certificate.new(cert_data)
    @pkey = OpenSSL::PKey::RSA.new(cert_data)
  else
    p12 = OpenSSL::PKCS12.new(cert_data, password)
    @certificate = p12.certificate
    @pkey = p12.key
  end

  if intermediate_cert
    intermediate_cert_data = File.binread(intermediate_cert)
    @extra_certs = [OpenSSL::X509::Certificate.new(intermediate_cert_data)]
  end
end

Instance Attribute Details

#certificateObject (readonly)

Returns the value of attribute certificate.



16
17
18
# File 'lib/push_package.rb', line 16

def certificate
  @certificate
end

#pkeyObject (readonly)

Returns the value of attribute pkey.



16
17
18
# File 'lib/push_package.rb', line 16

def pkey
  @pkey
end

Instance Method Details

#save(output_path = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/push_package.rb', line 53

def save(output_path = nil)

  @working_dir = Dir.mktmpdir('pushPackage')

  if output_path
    output_path = File.expand_path(output_path)
  else
    output_path = Dir.tmpdir + '/pushPackage.zip'
  end

  ## overwrite existing push packages
  File.delete(output_path) if File.exist?(output_path)

  zip = Zip::File.open(output_path, Zip::File::CREATE)

  File.open(@working_dir + '/website.json', 'w+') do |json|
    json.write(JSON.dump(@website_params))
  end

  Dir.mkdir(File.join(@working_dir,'icon.iconset'))
  Dir.glob(@iconset_path + '/*.png').each do |icon|
    FileUtils.cp(icon, @working_dir + '/icon.iconset/')
  end

  File.open(@working_dir + '/manifest.json', 'w+') do |manifest|
    manifest.write(manifest_data)
  end

  File.open(@working_dir + '/signature', 'wb+') do |file|
    file.write(signature.to_der)
  end

  Dir.glob(@working_dir + '/**/*').each do |file|
    next if File.directory?(file)
    zip.add(file.gsub("#{@working_dir}/", ''), file)
  end

  zip.close

  #clean up the temporary directory
  FileUtils.remove_entry_secure(@working_dir)

  #re-open the file for reading
  File.open(output_path, 'r')
end