lib/apns_simple/client.rb in apns_simple-1.0.0 vs lib/apns_simple/client.rb in apns_simple-1.0.1
- old
+ new
@@ -6,10 +6,13 @@
class CertificateActivenessTimeError < StandardError; end
attr_reader :ssl_context, :host, :port
+ DEFAULT_CERTIFICATE_PASSWORD = ''
+ DEFAULT_GATEWAY_URI = 'apn://gateway.push.apple.com:2195'
+ ERROR_BYTES_COUNT = 6
COMMAND = 8
CODES = {
0 => 'No errors encountered',
1 => 'Processing error',
2 => 'Missing device token',
@@ -33,43 +36,44 @@
end
@ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.cert = cert
- passphrase = options[:passphrase] || ''
+ passphrase = options[:passphrase] || DEFAULT_CERTIFICATE_PASSWORD
ssl_context.key = OpenSSL::PKey::RSA.new(certificate, passphrase)
- gateway_uri = options[:gateway_uri] || 'apn://gateway.push.apple.com:2195'
+ gateway_uri = options[:gateway_uri] || DEFAULT_GATEWAY_URI
@host, @port = parse_gateway_uri(gateway_uri)
end
def push(notification)
begin
+ notification.error = true
sock = TCPSocket.new(host, port)
ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
ssl.sync = true
ssl.connect
ssl.write(notification.payload)
ready = IO.select([ssl], [], [], TIMEOUT)
unless ready
- notification.error = true
notification.error_message = "No response from APNS server received in #{TIMEOUT} seconds. Exit by timeout."
return
end
readable_ssl_socket = ready.first.first
- if (error = readable_ssl_socket.read(6))
- notification.error = true
+ if (error = readable_ssl_socket.read(ERROR_BYTES_COUNT))
command, code, _index = error.unpack('ccN')
if command == COMMAND
notification.error_code = code
notification.error_message = "CODE: #{code}, DESCRIPTION: #{CODES[code]}"
else
notification.error_message = "Unknown command received from APNS server: #{command}"
end
+ else
+ notification.error = false
end
ensure
ssl.close if ssl
sock.close if sock
end
\ No newline at end of file