diff --git a/README.md b/README.md index c48cd3c..c84ec3c 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ and install it with APNS.pass = '' # Just in case your pem need a password + Alternatively, If you don't have the certificate stored in a file, you can pass any object that responds to ``read``. + + APNS.pem = StringIO.new(pem_string) + ### Usage #### Sending a single notification: diff --git a/lib/pushmeup/apns/core.rb b/lib/pushmeup/apns/core.rb index 5d74778..57016b6 100644 --- a/lib/pushmeup/apns/core.rb +++ b/lib/pushmeup/apns/core.rb @@ -46,16 +46,25 @@ def self.feedback return apns_feedback end - + protected + def self.pem_data + raise "Your pem file is not set. (APNS.pem = /path/to/cert.pem or object that responds to read)" unless pem + if pem.respond_to? :read + data = pem.read + pem.rewind if pem.respond_to?(:rewind) + else + raise "The path to your pem file does not exist!" unless File.exist?(pem) + data = File.read(pem) + end + data + end + def self.open_connection - raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem - raise "The path to your pem file does not exist!" unless File.exist?(self.pem) - context = OpenSSL::SSL::SSLContext.new - context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem)) - context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass) + context.cert = OpenSSL::X509::Certificate.new(self.pem_data) + context.key = OpenSSL::PKey::RSA.new(self.pem_data, self.pass) sock = TCPSocket.new(self.host, self.port) ssl = OpenSSL::SSL::SSLSocket.new(sock,context) @@ -65,12 +74,9 @@ def self.open_connection end def self.feedback_connection - raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem - raise "The path to your pem file does not exist!" unless File.exist?(self.pem) - context = OpenSSL::SSL::SSLContext.new - context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem)) - context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass) + context.cert = OpenSSL::X509::Certificate.new(self.pem_data) + context.key = OpenSSL::PKey::RSA.new(self.pem_data, self.pass) fhost = self.host.gsub('gateway','feedback') puts fhost