Class: StellaGCM

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

Constant Summary collapse

URL =
'https://android.googleapis.com/gcm/send'

Instance Method Summary collapse

Constructor Details

#initialize(private_key, option = nil) ⇒ StellaGCM

Returns a new instance of StellaGCM.



6
7
8
9
10
11
12
13
# File 'lib/stella_gcm.rb', line 6

def initialize(private_key, option = nil)
  @http = Net::HTTP::Persistent.new 'StellaGcm'
  @http.idle_timeout = option.nil? ? 1 :option[:idle_timeout]
  @http.open_timeout = option.nil? ? 30 :option[:open_timeout]
  @http.read_timeout = option.nil? ? 60 :option[:read_timeout]
  @http.headers['Authorization'] = "key=#{private_key}"
  @http.headers['Content-Type'] = "application/json"
end

Instance Method Details

#body(ids, option) ⇒ Object



39
40
41
# File 'lib/stella_gcm.rb', line 39

def body(ids, option)
  {:registration_ids => ids}.merge!(option)
end

#send_notification(ids, option) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/stella_gcm.rb', line 15

def send_notification(ids, option)
  gcm_server_uri = URI URL
  request = Net::HTTP::Post.new gcm_server_uri.path
  request.body = self.body(ids, option).to_json

  begin
    response = @http.request gcm_server_uri, request
  rescue
    return {:code => 404,
            :response => nil,
            :msg => 'GCM Server Not Connected.'}
  end

  begin
    body = ActiveSupport::JSON.decode(response.body).with_indifferent_access
  rescue
    return {:code => 500,
            :response => response,
            :msg => 'Json Parse failed Return Value.'}
  end
  {:code => 0,
   :response => body}
end