lib/apns_simple/client.rb in apns_simple-1.1.1 vs lib/apns_simple/client.rb in apns_simple-2.0.0

- old
+ new

@@ -44,32 +44,35 @@ gateway_uri = options[:gateway_uri] || DEFAULT_GATEWAY_URI @host, @port = parse_gateway_uri(gateway_uri) end def push(notification) - sock = TCPSocket.new(host, port) - ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context) - ssl.sync = true - ssl.connect - ssl.write(notification.payload) + unless notification.error + begin + sock = TCPSocket.new(host, port) + ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context) + ssl.sync = true + ssl.connect + ssl.write(notification.payload) - if (ready = IO.select([ssl], [], [], TIMEOUT)) - readable_ssl_socket = ready.first.first - if (error = readable_ssl_socket.read(ERROR_BYTES_COUNT)) - command, code, _index = error.unpack('ccN') - notification.error = true - 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}" + if (ready = IO.select([ssl], [], [], TIMEOUT)) + readable_ssl_socket = ready.first.first + if (error = readable_ssl_socket.read(ERROR_BYTES_COUNT)) + command, code, _index = error.unpack('ccN') + notification.error = true + 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 + end end + ensure + ssl.close if ssl + sock.close if sock end end - - ensure - ssl.close if ssl - sock.close if sock end private def parse_gateway_uri(uri) \ No newline at end of file