class RubyPushNotifications::APNS::APNSConnection

This class encapsulates a connection with APNS.

@author Carlos Alonso

Constants

APNS_PORT

@private Port to connect to

APNS_PRODUCTION_URL

@private URL of APNS production environment

APNS_SANDBOX_URL

@private URL of the APNS Sandbox environment

Public Class Methods

host(sandbox) click to toggle source

Returns the URL to connect to.

@param sandbox [Boolean]. Whether it is the sandbox or the production

environment we're looking for.

@return [String]. The URL for the APNS service.

# File lib/ruby-push-notifications/apns/apns_connection.rb, line 50
def self.host(sandbox)
  sandbox ? APNS_SANDBOX_URL : APNS_PRODUCTION_URL
end
new(tcpsock, sslsock) click to toggle source

Initializes the APNSConnection

@param tcpsock [TCPSocket]. The used TCP Socket. @param sslsock [SSLSocket]. The connected SSL Socket.

# File lib/ruby-push-notifications/apns/apns_connection.rb, line 58
def initialize(tcpsock, sslsock)
  @tcpsock = tcpsock
  @sslsock = sslsock
end
open(cert, sandbox, pass = nil, options = {}) click to toggle source

Opens a connection with APNS

@param cert [String]. Contents of the PEM encoded certificate @param sandbox [Boolean]. Whether to use the sandbox environment or not. @param pass [String] optional. Passphrase for the certificate. @param options [Hash] optional. Options for open. Currently supports:

* host [String]: Hostname of the APNS environment. Defaults to the official APNS hostname.
* connect_timeout [Integer]: how long the socket will wait for when opening the APNS socket. Defaults to 30.

@return [APNSConnection]. The recently stablished connection.

# File lib/ruby-push-notifications/apns/apns_connection.rb, line 32
def self.open(cert, sandbox, pass = nil, options = {})
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.key = OpenSSL::PKey::RSA.new cert, pass
  ctx.cert = OpenSSL::X509::Certificate.new cert

  h = options.fetch(:host, host(sandbox))
  socket = Socket.tcp h, APNS_PORT, nil, nil, connect_timeout: options.fetch(:connect_timeout, 30)
  ssl = OpenSSL::SSL::SSLSocket.new socket, ctx
  ssl.connect

  new socket, ssl
end

Public Instance Methods

close() click to toggle source

Closes the APNSConnection

# File lib/ruby-push-notifications/apns/apns_connection.rb, line 64
def close
  @sslsock.close
  @tcpsock.close
end