Class: EventMachine::APN::Client

Inherits:
Connection
  • Object
show all
Defined in:
lib/em-apn/client.rb

Constant Summary

SANDBOX_GATEWAY =
"gateway.sandbox.push.apple.com"
PRODUCTION_GATEWAY =
"gateway.push.apple.com"
PORT =
2195

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(options = {})

A new instance of Client



29
30
31
32
33
# File 'lib/em-apn/client.rb', line 29

def initialize(options = {})
  @key    = options[:key]
  @cert   = options[:cert]
  @closed = false
end

Class Method Details

+ (Object) connect(options = {})

Create a connection to Apple's push notification gateway

This convenience method will adopt environment variables and defaults for the necessary parameters -- gateway host, port, key, and cert.

You can use EM.connect yourself, but in that case, be sure to pass along the key and cert for the SSL connection.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/em-apn/client.rb', line 17

def self.connect(options = {})
  options = options.dup
  options[:key]  ||= ENV["APN_KEY"]
  options[:cert] ||= ENV["APN_CERT"]

  gateway = options.delete(:gateway)
  gateway ||= ENV["APN_GATEWAY"]
  gateway ||= (ENV["APN_ENV"] == "production") ? PRODUCTION_GATEWAY : SANDBOX_GATEWAY

  EM.connect(gateway, PORT, self, options)
end

Instance Method Details

- (Boolean) closed?

Returns:

  • (Boolean)


44
45
46
# File 'lib/em-apn/client.rb', line 44

def closed?
  @closed
end

- (Object) deliver(notification)



35
36
37
38
# File 'lib/em-apn/client.rb', line 35

def deliver(notification)
  LogMessage.new(Response.new(notification)).log
  send_data(notification.data)
end

- (Object) on_receipt(&block)



40
41
42
# File 'lib/em-apn/client.rb', line 40

def on_receipt(&block)
  @on_receipt_callback = block
end

- (Object) post_init

EM callbacks



52
53
54
55
56
57
58
# File 'lib/em-apn/client.rb', line 52

def post_init
  start_tls(
    :private_key_file => @key,
    :cert_chain_file  => @cert,
    :verify_peer      => false
  )
end

- (Object) receive_data(data)



60
61
62
63
64
65
66
67
# File 'lib/em-apn/client.rb', line 60

def receive_data(data)
  data_array = data.unpack("ccN")
  LogMessage.new(ErrorResponse.new(*data_array)).log

  if @on_receipt_callback
    @on_receipt_callback.call(data_array)
  end
end

- (Object) unbind

The caller should attempt to detect closed connections by calling Client#closed? and re-connecting.



71
72
73
74
# File 'lib/em-apn/client.rb', line 71

def unbind
  @closed = true
  EM::APN.logger.info("Connection closed")
end