Class: EventMachine::APN::Client
- Inherits:
-
Connection
- Object
- Connection
- EventMachine::APN::Client
- 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)
-
+ (Object) connect(options = {})
Create a connection to Apple's push notification gateway.
Instance Method Summary (collapse)
- - (Boolean) closed?
- - (Object) deliver(notification)
-
- (Client) initialize(options = {})
constructor
A new instance of Client.
- - (Object) on_receipt(&block)
-
- (Object) post_init
EM callbacks.
- - (Object) receive_data(data)
-
- (Object) unbind
The caller should attempt to detect closed connections by calling Client#closed? and re-connecting.
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( = {}) @key = [:key] @cert = [: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( = {}) = .dup [:key] ||= ENV["APN_KEY"] [:cert] ||= ENV["APN_CERT"] gateway = .delete(:gateway) gateway ||= ENV["APN_GATEWAY"] gateway ||= (ENV["APN_ENV"] == "production") ? PRODUCTION_GATEWAY : SANDBOX_GATEWAY EM.connect(gateway, PORT, self, ) end |
Instance Method Details
- (Boolean) closed?
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 |