class BMO::APNS::Connection

Handle the connection state SSL or Pure TCP

Attributes

cert_pass[R]
cert_path[R]
host[R]
port[R]

Public Class Methods

new(host, port, cert_path = nil, cert_pass = nil) click to toggle source
# File lib/bmo/apns/connection.rb, line 7
def initialize(host, port, cert_path = nil, cert_pass = nil)
  @host      = host
  @port      = port
  @cert_path = cert_path
  @cert_pass = cert_pass
end

Public Instance Methods

connect() { |socket| ... } click to toggle source

Create a connection to Apple. If a cert_path exists it uses SSL else

a pure TCPSocket. It then yields the socket and handles the closing

@return The yielded return

# File lib/bmo/apns/connection.rb, line 18
def connect
  socket = cert_path ? ssl_socket : tcp_socket

  yielded = yield socket

  socket.close
  yielded
end

Private Instance Methods

ssl_context() click to toggle source

@return [SSLContext] SSLContext setted with the certificate

# File lib/bmo/apns/connection.rb, line 45
def ssl_context
  cert         = File.read(cert_path)
  context      = OpenSSL::SSL::SSLContext.new(:TLSv1)
  context.cert = OpenSSL::X509::Certificate.new(cert)
  context.key  = OpenSSL::PKey::RSA.new(cert, cert_pass)
  context
end
ssl_socket() click to toggle source

@return [SSLSocket] the SSLSocket connected and setted to sync_close

to sync tcp_socket and ssl_socket closing.
# File lib/bmo/apns/connection.rb, line 36
def ssl_socket
  ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
  ssl_socket.sync_close = true
  ssl_socket.connect
  ssl_socket
end
tcp_socket() click to toggle source

@return [TCPSocket]

# File lib/bmo/apns/connection.rb, line 30
def tcp_socket
  TCPSocket.new(host, port)
end