Class: RubyPushNotifications::APNS::APNSConnection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruby-push-notifications/apns/apns_connection.rb

Overview

This class encapsulates a connection with APNS.

Author:

  • Carlos Alonso

Constant Summary collapse

APNS_SANDBOX_URL =
'gateway.sandbox.push.apple.com'
APNS_PRODUCTION_URL =
'gateway.push.apple.com'
APNS_PORT =
2195

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tcpsock, sslsock) ⇒ APNSConnection

Initializes the APNSConnection

Parameters:

  • tcpsock (TCPSocket)

    . The used TCP Socket.

  • sslsock (SSLSocket)

    . The connected SSL Socket.



58
59
60
61
# File 'lib/ruby-push-notifications/apns/apns_connection.rb', line 58

def initialize(tcpsock, sslsock)
  @tcpsock = tcpsock
  @sslsock = sslsock
end

Class Method Details

.host(sandbox) ⇒ String

Returns the URL to connect to.

Parameters:

  • sandbox (Boolean)

    . Whether it is the sandbox or the production environment we’re looking for.

Returns:

  • (String)

    . The URL for the APNS service.



50
51
52
# File 'lib/ruby-push-notifications/apns/apns_connection.rb', line 50

def self.host(sandbox)
  sandbox ? APNS_SANDBOX_URL : APNS_PRODUCTION_URL
end

.open(cert, sandbox, pass = nil, options = {}) ⇒ APNSConnection

Opens a connection with APNS

Parameters:

  • cert (String)

    . Contents of the PEM encoded certificate

  • sandbox (Boolean)

    . Whether to use the sandbox environment or not.

  • pass (String) (defaults to: nil)

    optional. Passphrase for the certificate.

  • options (Hash) (defaults to: {})

    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.

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Method Details

#closeObject

Closes the APNSConnection



64
65
66
67
# File 'lib/ruby-push-notifications/apns/apns_connection.rb', line 64

def close
  @sslsock.close
  @tcpsock.close
end