Class: EventMachine::APN::Client

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(options = {})
  @key  = options[:key]  || ENV["APN_KEY"]
  @cert = options[:cert] || ENV["APN_CERT"]
  @port = options[:port] || PORT

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

  @connection = nil
end

Instance Attribute Details

#certObject (readonly)

Returns the value of attribute cert.



10
11
12
# File 'lib/em-apn/client.rb', line 10

def cert
  @cert
end

#close_callbackObject (readonly)

Returns the value of attribute close_callback.



10
11
12
# File 'lib/em-apn/client.rb', line 10

def close_callback
  @close_callback
end

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/em-apn/client.rb', line 10

def connection
  @connection
end

#error_callbackObject (readonly)

Returns the value of attribute error_callback.



10
11
12
# File 'lib/em-apn/client.rb', line 10

def error_callback
  @error_callback
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



10
11
12
# File 'lib/em-apn/client.rb', line 10

def gateway
  @gateway
end

#keyObject (readonly)

Returns the value of attribute key.



10
11
12
# File 'lib/em-apn/client.rb', line 10

def key
  @key
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/em-apn/client.rb', line 10

def port
  @port
end

Class Method Details

.connect(options = {}) ⇒ Object

A convenience method for creating and connecting.



13
14
15
16
17
# File 'lib/em-apn/client.rb', line 13

def self.connect(options = {})
  new(options).tap do |client|
    client.connect
  end
end

Instance Method Details

#connectObject



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

def connect
  @connection = EM.connect(gateway, port, Connection, self)
end

#deliver(notification) ⇒ Object



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

def deliver(notification)
  connect if connection.nil? || connection.disconnected?
  log(notification)
  connection.send_data(notification.data)
end

#log(notification) ⇒ Object



48
49
50
# File 'lib/em-apn/client.rb', line 48

def log(notification)
  EM::APN.logger.info("TOKEN=#{notification.token} ALERT=#{notification.alert}")
end

#on_close(&block) ⇒ Object



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

def on_close(&block)
  @close_callback = block
end

#on_error(&block) ⇒ Object



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

def on_error(&block)
  @error_callback = block
end