lib/apns_simple/client.rb in apns_simple-0.7.0 vs lib/apns_simple/client.rb in apns_simple-0.8.0
- old
+ new
@@ -2,12 +2,14 @@
require 'socket'
module ApnsSimple
class Client
- attr_reader :certificate, :ssl_context, :host, :port
+ class CertificateActivenessTimeError < StandardError; end
+ attr_reader :ssl_context, :host, :port
+
COMMAND = 8
CODES = {
0 => 'No errors encountered',
1 => 'Processing error',
2 => 'Missing device token',
@@ -20,26 +22,28 @@
10 => 'Shutdown',
255 => 'Unknown error'
}
def initialize(options)
- @certificate = options.fetch(:certificate)
- passphrase = options[:passphrase] || ''
+ certificate = options.fetch(:certificate)
+ current_time = Time.now.utc
+ cert = OpenSSL::X509::Certificate.new(certificate)
+ if current_time < cert.not_before || current_time > cert.not_after
+ raise CertificateActivenessTimeError, "CURRENT_TIME: #{current_time}, NOT_BEFORE: #{cert.not_before}, NOT_AFTER: #{cert.not_after}"
+ end
+
@ssl_context = OpenSSL::SSL::SSLContext.new
- @ssl_context.key = OpenSSL::PKey::RSA.new(certificate, passphrase)
+ ssl_context.cert = cert
+
+ passphrase = options[:passphrase] || ''
+ ssl_context.key = OpenSSL::PKey::RSA.new(certificate, passphrase)
gateway_uri = options[:gateway_uri] || 'apn://gateway.push.apple.com:2195'
@host, @port = parse_gateway_uri(gateway_uri)
end
def push(notification)
begin
- current_time = Time.now.utc
- cert = OpenSSL::X509::Certificate.new(certificate)
- if current_time < cert.not_before || current_time > cert.not_after
- raise CertificateActivenessTimeError, "CURRENT_TIME: #{current_time}, NOT_BEFORE: #{cert.not_before}, NOT_AFTER: #{cert.not_after}"
- end
- ssl_context.cert = cert
sock = TCPSocket.new(host, port)
ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
ssl.connect
ssl.write(notification.payload)
ssl.flush
\ No newline at end of file