Class: EventMachine::ApnManager::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/em_apn_manager/client.rb

Constant Summary collapse

SANDBOX_GATEWAY =
"gateway.sandbox.push.apple.com"
PRODUCTION_GATEWAY =
"gateway.push.apple.com"
TEST_GATEWAY =
"127.0.0.1"
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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/em_apn_manager/client.rb', line 22

def initialize(options = {})
  @cert = options[:cert]
  @port = options[:port] || PORT
  @environment = options[:env] || "development"
  @gateway = options[:gateway]
  @gateway ||=  case @environment
                when "test"
                  TEST_GATEWAY
                when "development"
                  SANDBOX_GATEWAY
                when "production"
                  PRODUCTION_GATEWAY
                else
                  TEST_GATEWAY
                end
  @connection = nil
end

Instance Attribute Details

#certObject (readonly)

Returns the value of attribute cert.



13
14
15
# File 'lib/em_apn_manager/client.rb', line 13

def cert
  @cert
end

#close_callbackObject (readonly)

Returns the value of attribute close_callback.



13
14
15
# File 'lib/em_apn_manager/client.rb', line 13

def close_callback
  @close_callback
end

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/em_apn_manager/client.rb', line 13

def connection
  @connection
end

#environmentObject (readonly)

Returns the value of attribute environment.



13
14
15
# File 'lib/em_apn_manager/client.rb', line 13

def environment
  @environment
end

#error_callbackObject (readonly)

Returns the value of attribute error_callback.



13
14
15
# File 'lib/em_apn_manager/client.rb', line 13

def error_callback
  @error_callback
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



13
14
15
# File 'lib/em_apn_manager/client.rb', line 13

def gateway
  @gateway
end

#open_callbackObject (readonly)

Returns the value of attribute open_callback.



13
14
15
# File 'lib/em_apn_manager/client.rb', line 13

def open_callback
  @open_callback
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/em_apn_manager/client.rb', line 13

def port
  @port
end

Class Method Details

.connect(options = {}) ⇒ Object

A convenience method for creating and connecting.



16
17
18
19
20
# File 'lib/em_apn_manager/client.rb', line 16

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

Instance Method Details

#connectObject



40
41
42
# File 'lib/em_apn_manager/client.rb', line 40

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

#connected?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/em_apn_manager/client.rb', line 44

def connected?
  !connection.nil? && !connection.disconnected?
end

#deliver(notification) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/em_apn_manager/client.rb', line 48

def deliver(notification)
  if !connected?
    puts "No connection."
    return
  end
  notification.validate!
  log(notification)
  connection.send_data(notification.data)
end

#log(notification) ⇒ Object



70
71
72
# File 'lib/em_apn_manager/client.rb', line 70

def log(notification)
  EM::ApnManager.logger.info("TOKEN=#{notification.token} PAYLOAD=#{notification.payload.inspect}")
end

#on_close(&block) ⇒ Object



62
63
64
# File 'lib/em_apn_manager/client.rb', line 62

def on_close(&block)
  @close_callback = block
end

#on_error(&block) ⇒ Object



58
59
60
# File 'lib/em_apn_manager/client.rb', line 58

def on_error(&block)
  @error_callback = block
end

#on_open(&block) ⇒ Object



66
67
68
# File 'lib/em_apn_manager/client.rb', line 66

def on_open(&block)
  @open_callback = block
end