This repository was archived by the owner on Mar 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathgcm.rb
More file actions
196 lines (170 loc) · 6.24 KB
/
Copy pathgcm.rb
File metadata and controls
196 lines (170 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require 'httparty'
require 'cgi'
require 'json'
class GCM
include HTTParty
base_uri 'https://gcm-http.googleapis.com/gcm'
default_timeout 30
format :json
attr_accessor :timeout, :api_key
def initialize(api_key, client_options = {})
@api_key = api_key
@client_options = client_options
end
# {
# "collapse_key": "score_update",
# "time_to_live": 108,
# "delay_while_idle": true,
# "registration_ids": ["4", "8", "15", "16", "23", "42"],
# "data" : {
# "score": "5x1",
# "time": "15:10"
# }
# }
# gcm = GCM.new("API_KEY")
# gcm.send(registration_ids: ["4sdsx", "8sdsd"], {data: {score: "5x1"}})
def send_notification(registration_ids, options = {})
post_body = build_post_body(registration_ids, options)
params = {
body: post_body.to_json,
headers: {
'Authorization' => "key=#{@api_key}",
'Content-Type' => 'application/json'
}
}
response = self.class.post('/send', params.merge(@client_options))
build_response(response, registration_ids)
end
alias send send_notification
def create_notification_key(key_name, project_id, registration_ids = [])
post_body = build_post_body(registration_ids, operation: 'create',
notification_key_name: key_name)
params = {
body: post_body.to_json,
headers: {
'Content-Type' => 'application/json',
'project_id' => project_id,
'Authorization' => "key=#{@api_key}"
}
}
response = self.class.post('/notification', params.merge(@client_options))
build_response(response)
end
alias create create_notification_key
def add_registration_ids(key_name, project_id, notification_key, registration_ids)
post_body = build_post_body(registration_ids, operation: 'add',
notification_key_name: key_name,
notification_key: notification_key)
params = {
body: post_body.to_json,
headers: {
'Content-Type' => 'application/json',
'project_id' => project_id,
'Authorization' => "key=#{@api_key}"
}
}
response = self.class.post('/notification', params.merge(@client_options))
build_response(response)
end
alias add add_registration_ids
def remove_registration_ids(key_name, project_id, notification_key, registration_ids)
post_body = build_post_body(registration_ids, operation: 'remove',
notification_key_name: key_name,
notification_key: notification_key)
params = {
body: post_body.to_json,
headers: {
'Content-Type' => 'application/json',
'project_id' => project_id,
'Authorization' => "key=#{@api_key}"
}
}
response = self.class.post('/notification', params.merge(@client_options))
build_response(response)
end
alias remove remove_registration_ids
def send_with_notification_key(notification_key, options = {})
body = { to: notification_key }.merge(options)
params = {
body: body.to_json,
headers: {
'Authorization' => "key=#{@api_key}",
'Content-Type' => 'application/json'
}
}
response = self.class.post('/send', params.merge(@client_options))
build_response(response)
end
def send_to_topic(topic, options = {})
if topic =~ /[a-zA-Z0-9\-_.~%]+/
send_with_notification_key('/topics/' + topic, options)
end
end
private
def build_post_body(registration_ids, options = {})
{ registration_ids: registration_ids }.merge(options)
end
def build_response(response, registration_ids = [])
body = response.body || {}
response_hash = { body: body, headers: response.headers, status_code: response.code }
case response.code
when 200
response_hash[:response] = 'success'
body = JSON.parse(body) unless body.empty?
response_hash[:canonical_ids] = build_canonical_ids(body, registration_ids) unless registration_ids.empty?
response_hash[:not_registered_ids] = build_not_registered_ids(body, registration_ids) unless registration_ids.empty?
response_hash[:invalid_registration_ids] = build_invalid_registration_ids(body, registration_ids) unless registration_ids.empty?
when 400
response_hash[:response] = 'Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.'
when 401
response_hash[:response] = 'There was an error authenticating the sender account.'
when 503
response_hash[:response] = 'Server is temporarily unavailable.'
when 500..599
response_hash[:response] = 'There was an internal error in the GCM server while trying to process the request.'
end
response_hash
end
def build_canonical_ids(body, registration_ids)
canonical_ids = []
unless body.empty?
if body['canonical_ids'] > 0
body['results'].each_with_index do |result, index|
canonical_ids << { old: registration_ids[index], new: result['registration_id'] } if has_canonical_id?(result)
end
end
end
canonical_ids
end
def build_not_registered_ids(body, registration_id)
not_registered_ids = []
unless body.empty?
if body['failure'] > 0
body['results'].each_with_index do |result, index|
not_registered_ids << registration_id[index] if is_not_registered?(result)
end
end
end
not_registered_ids
end
def build_invalid_registration_ids(body, registration_id)
invalid_registration_ids = []
unless body.empty?
if body['failure'] > 0
body['results'].each_with_index do |result, index|
invalid_registration_ids << registration_id[index] if is_invalid_registration?(result)
end
end
end
invalid_registration_ids
end
def has_canonical_id?(result)
!result['registration_id'].nil?
end
def is_not_registered?(result)
result['error'] == 'NotRegistered'
end
def is_invalid_registration?(result)
result['error'] == 'InvalidRegistration'
end
end