Class: Suj::Pusher::GCMConnection

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/suj/pusher/gcm_connection.rb

Constant Summary collapse

GATEWAY =
"https://android.googleapis.com/gcm/send"

Instance Method Summary collapse

Constructor Details

#initialize(pool, key, options = {}) ⇒ GCMConnection

Returns a new instance of GCMConnection.



10
11
12
13
14
15
16
17
18
# File 'lib/suj/pusher/gcm_connection.rb', line 10

def initialize(pool, key, options = {})
  @pool = pool
  @options = options
  @key = key
  @headers =  {
    'Content-Type' => 'application/json',
    'Authorization' => "key=#{options[:api_key]}"
  }
end

Instance Method Details

#deliver(msg) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/suj/pusher/gcm_connection.rb', line 20

def deliver(msg)

  return if msg[:gcm_ids].empty?

  body = MultiJson.dump({
    registration_ids: msg[:gcm_ids],
    time_to_live: msg[:time_to_live] || 0,
    data: msg[:data] || {}
  })

  http = EventMachine::HttpRequest.new(GATEWAY).post( head: @headers, body: body )

  http.errback do
    error "GCM network error"
    @pool.remove_connection(@key)
  end
  http.callback do
    if http.response_header.status != 200
      error "GCM push error #{http.response_header.status}"
      error http.response
    else
      info "GCM push notification send"
      info http.response
    end
    @pool.remove_connection(@key)
  end
end