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.


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

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.


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

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

.open(cert, sandbox) ⇒ 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.

Returns:


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby-push-notifications/apns/apns_connection.rb', line 30

def self.open(cert, sandbox)
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.key = OpenSSL::PKey::RSA.new cert
  ctx.cert = OpenSSL::X509::Certificate.new cert

  h = host sandbox
  socket = TCPSocket.new h, APNS_PORT
  ssl = OpenSSL::SSL::SSLSocket.new socket, ctx
  ssl.connect

  new socket, ssl
end

Instance Method Details

#closeObject

Closes the APNSConnection


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

def close
  @sslsock.close
  @tcpsock.close
end