fcm 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +12 -7
  3. data/fcm.gemspec +2 -2
  4. data/lib/fcm.rb +81 -108
  5. data/spec/fcm_spec.rb +3 -3
  6. metadata +8 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0d92dc29d8a781ef45a9e3cd113fe834a3a41b63
4
- data.tar.gz: e3413daa3b418bfbc0631ffdf79f653950599f25
2
+ SHA256:
3
+ metadata.gz: 4200d68170fc0c8cee514824e77244e51fc68802dc8ad6a34a1c592c11f36a71
4
+ data.tar.gz: a70490fb7e92cf2ea66e53ba1cb58a5c8caad45b8d6bbbf1335bc13c3e49c774
5
5
  SHA512:
6
- metadata.gz: 994687542f81b3d11b403d71dde8097a1990445c66249922215da103cff7737d44ee30d99537676cf2695f5606d2837cbb45cfdf59aed2b8c7649290450bf9a1
7
- data.tar.gz: 1516e659f3c1d7180a77ac177e0f79e4f45d9d471d0f22434533657de07d9de4c29855baf66dcbee003cf9c3b1ac6976ac8a543ca9e75a07b1d9fd04297dfb59
6
+ metadata.gz: a5d276a776ddd9eed746f16e129c1820e27361e99fa3e6c6f8be14af04561a8f4dd520d4296bf632d31bb1269b4a8817e18bb3938d8992d736503e310dfbd8cb
7
+ data.tar.gz: 7049ded384b5df8a097addbd96cce01ca559ddb6a61b8ec27e2f5fe06d7d4d70d0009b4d7bfa240dfe84598acfb917ce735ec4b8c6a71e072108d4ed1d6cc629
data/README.md CHANGED
@@ -22,9 +22,11 @@ For Android you will need a device running 2.3 (or newer) that also have the Goo
22
22
  One of the following, tested Ruby versions:
23
23
 
24
24
  - `2.0.0`
25
- - `2.1.9`
26
- - `2.2.5`
27
- - `2.3.1`
25
+ - `2.1.10`
26
+ - `2.2.10`
27
+ - `2.3.8`
28
+ - `2.4.5`
29
+ - `2.5.3`
28
30
 
29
31
  ## Usage
30
32
 
@@ -43,12 +45,11 @@ fcm = FCM.new("my_server_key")
43
45
 
44
46
  registration_ids= ["12", "13"] # an array of one or more client registration tokens
45
47
 
46
- # See https://developers.google.com/cloud-messaging/http for all available options.
48
+ # See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages for all available options.
47
49
  options = { "notification": {
48
50
  "title": "Portugal vs. Denmark",
49
- "text": "5 to 1"
50
- },
51
- "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
51
+ "body": "5 to 1"
52
+ }
52
53
  }
53
54
  response = fcm.send(registration_ids, options)
54
55
  ```
@@ -174,6 +175,10 @@ The guide to set up an iOS app to get notifications is here: [Setting up a FCM C
174
175
 
175
176
  ## ChangeLog
176
177
 
178
+ ### 0.0.7
179
+
180
+ - replace `httparty` with `faraday`
181
+
177
182
  ### 0.0.2
178
183
 
179
184
  - Fixed group messaging url.
data/fcm.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fcm"
6
- s.version = "0.0.6"
6
+ s.version = "0.0.7"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Kashif Rasul", "Shoaib Burq"]
9
9
  s.email = ["kashif@spacialdb.com", "shoaib@spacialdb.com"]
@@ -21,5 +21,5 @@ Gem::Specification.new do |s|
21
21
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
22
  s.require_paths = ["lib"]
23
23
 
24
- s.add_runtime_dependency('httparty', '~> 0.10', '>= 0.10.0')
24
+ s.add_runtime_dependency('faraday','0.15.4')
25
25
  end
data/lib/fcm.rb CHANGED
@@ -1,16 +1,15 @@
1
- require 'httparty'
1
+ require 'faraday'
2
2
  require 'cgi'
3
3
  require 'json'
4
4
 
5
5
  class FCM
6
- include HTTParty
7
- base_uri 'https://fcm.googleapis.com/fcm'
8
- default_timeout 30
9
- format :json
6
+ BASE_URI = 'https://fcm.googleapis.com'
7
+ DEFAULT_TIMEOUT = 30
8
+ FORMAT = :json
10
9
 
11
10
  # constants
12
- GROUP_NOTIFICATION_BASE_URI = 'https://android.googleapis.com/gcm'
13
- INSTANCE_ID_API = 'https://iid.googleapis.com/iid/v1'
11
+ GROUP_NOTIFICATION_BASE_URI = 'https://android.googleapis.com'
12
+ INSTANCE_ID_API = 'https://iid.googleapis.com'
14
13
  TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/
15
14
 
16
15
  attr_accessor :timeout, :api_key
@@ -35,15 +34,10 @@ class FCM
35
34
  def send_notification(registration_ids, options = {})
36
35
  post_body = build_post_body(registration_ids, options)
37
36
 
38
- params = {
39
- body: post_body.to_json,
40
- headers: {
41
- 'Authorization' => "key=#{@api_key}",
42
- 'Content-Type' => 'application/json'
43
- }
44
- }
45
- response = self.class.post('/send', params.merge(@client_options))
46
- build_response(response, registration_ids)
37
+ for_uri(BASE_URI) do |connection|
38
+ response = connection.post('/fcm/send', post_body.to_json)
39
+ build_response(response, registration_ids)
40
+ end
47
41
  end
48
42
  alias send send_notification
49
43
 
@@ -51,22 +45,14 @@ class FCM
51
45
  post_body = build_post_body(registration_ids, operation: 'create',
52
46
  notification_key_name: key_name)
53
47
 
54
- params = {
55
- body: post_body.to_json,
56
- headers: {
57
- 'Content-Type' => 'application/json',
58
- 'project_id' => project_id,
59
- 'Authorization' => "key=#{@api_key}"
60
- }
48
+ extra_headers = {
49
+ 'project_id' => project_id
61
50
  }
62
51
 
63
- response = nil
64
-
65
- for_uri(GROUP_NOTIFICATION_BASE_URI) do
66
- response = self.class.post('/notification', params.merge(@client_options))
52
+ for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection|
53
+ response = connection.post('/gcm/notification', post_body.to_json)
54
+ build_response(response)
67
55
  end
68
-
69
- build_response(response)
70
56
  end
71
57
  alias create create_notification_key
72
58
 
@@ -75,21 +61,14 @@ class FCM
75
61
  notification_key_name: key_name,
76
62
  notification_key: notification_key)
77
63
 
78
- params = {
79
- body: post_body.to_json,
80
- headers: {
81
- 'Content-Type' => 'application/json',
82
- 'project_id' => project_id,
83
- 'Authorization' => "key=#{@api_key}"
84
- }
64
+ extra_headers = {
65
+ 'project_id' => project_id
85
66
  }
86
67
 
87
- response = nil
88
-
89
- for_uri(GROUP_NOTIFICATION_BASE_URI) do
90
- response = self.class.post('/notification', params.merge(@client_options))
68
+ for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection|
69
+ response = connection.post('/gcm/notification', post_body.to_json)
70
+ build_response(response)
91
71
  end
92
- build_response(response)
93
72
  end
94
73
  alias add add_registration_ids
95
74
 
@@ -98,21 +77,14 @@ class FCM
98
77
  notification_key_name: key_name,
99
78
  notification_key: notification_key)
100
79
 
101
- params = {
102
- body: post_body.to_json,
103
- headers: {
104
- 'Content-Type' => 'application/json',
105
- 'project_id' => project_id,
106
- 'Authorization' => "key=#{@api_key}"
107
- }
80
+ extra_headers = {
81
+ 'project_id' => project_id
108
82
  }
109
83
 
110
- response = nil
111
-
112
- for_uri(GROUP_NOTIFICATION_BASE_URI) do
113
- response = self.class.post('/notification', params.merge(@client_options))
84
+ for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection|
85
+ response = connection.post('/gcm/notification', post_body.to_json)
86
+ build_response(response)
114
87
  end
115
- build_response(response)
116
88
  end
117
89
  alias remove remove_registration_ids
118
90
 
@@ -120,20 +92,17 @@ class FCM
120
92
  params = {
121
93
  query: {
122
94
  notification_key_name: key_name
123
- },
124
- headers: {
125
- 'Content-Type' => 'application/json',
126
- 'project_id' => project_id,
127
- 'Authorization' => "key=#{@api_key}"
128
95
  }
129
96
  }
97
+
98
+ extra_headers = {
99
+ 'project_id' => project_id
100
+ }
130
101
 
131
- response = nil
132
-
133
- for_uri(GROUP_NOTIFICATION_BASE_URI) do
134
- response = self.class.get('/notification', params.merge(@client_options))
102
+ for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection|
103
+ response = connection.get('/gcm/notification', params)
104
+ build_response(response)
135
105
  end
136
- build_response(response)
137
106
  end
138
107
 
139
108
  def send_with_notification_key(notification_key, options = {})
@@ -142,20 +111,10 @@ class FCM
142
111
  end
143
112
 
144
113
  def topic_subscription(topic, registration_id)
145
- params = {
146
- headers: {
147
- 'Authorization' => "key=#{@api_key}",
148
- 'Content-Type' => 'application/json'
149
- }
150
- }
151
-
152
- response = nil
153
-
154
- for_uri(INSTANCE_ID_API) do
155
- response = self.class.post("/#{registration_id}/rel/topics/#{topic}", params)
114
+ for_uri(INSTANCE_ID_API) do |connection|
115
+ response = connection.post("/iid/v1/#{registration_id}/rel/topics/#{topic}")
116
+ build_response(response)
156
117
  end
157
-
158
- build_response(response)
159
118
  end
160
119
 
161
120
  def batch_topic_subscription(topic, registration_ids)
@@ -168,31 +127,46 @@ class FCM
168
127
 
169
128
  def manage_topics_relationship(topic, registration_ids, action)
170
129
  body = { to: "/topics/#{topic}", registration_tokens: registration_ids }
171
- params = {
172
- body: body.to_json,
173
- headers: {
174
- 'Authorization' => "key=#{@api_key}",
175
- 'Content-Type' => 'application/json'
176
- }
177
- }
178
130
 
179
- response = nil
180
-
181
- for_uri(INSTANCE_ID_API) do
182
- response = self.class.post("/:batch#{action}", params)
131
+ for_uri(INSTANCE_ID_API) do |connection|
132
+ response = connection.post("/iid/v1:batch#{action}", body.to_json)
133
+ build_response(response)
183
134
  end
184
-
185
- build_response(response)
186
135
  end
187
136
 
188
-
189
-
190
137
  def send_to_topic(topic, options = {})
191
138
  if topic.gsub(TOPIC_REGEX, "").length == 0
192
139
  send_with_notification_key('/topics/' + topic, options)
193
140
  end
194
141
  end
195
142
 
143
+ def get_instance_id_info iid_token, options={}
144
+ params = {
145
+ query: options
146
+ }
147
+
148
+ for_uri(INSTANCE_ID_API) do |connection|
149
+ response = connection.get('/iid/info/'+iid_token, params)
150
+ build_response(response)
151
+ end
152
+ end
153
+
154
+ def subscribe_instance_id_to_topic iid_token, topic_name
155
+ batch_subscribe_instance_ids_to_topic([iid_token], topic_name)
156
+ end
157
+
158
+ def unsubscribe_instance_id_from_topic iid_token, topic_name
159
+ batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name)
160
+ end
161
+
162
+ def batch_subscribe_instance_ids_to_topic instance_ids, topic_name
163
+ manage_topics_relationship(topic_name, instance_ids, 'Add')
164
+ end
165
+
166
+ def batch_unsubscribe_instance_ids_from_topic instance_ids, topic_name
167
+ manage_topics_relationship(topic_name, instance_ids, 'Remove')
168
+ end
169
+
196
170
  def send_to_topic_condition(condition, options = {})
197
171
  if validate_condition?(condition)
198
172
  body = { condition: condition }.merge(options)
@@ -202,11 +176,16 @@ class FCM
202
176
 
203
177
  private
204
178
 
205
- def for_uri(uri)
206
- current_uri = self.class.base_uri
207
- self.class.base_uri uri
208
- yield
209
- self.class.base_uri current_uri
179
+ def for_uri(uri, extra_headers = {})
180
+ connection = ::Faraday.new(:url => uri) do |faraday|
181
+ faraday.adapter Faraday.default_adapter
182
+ faraday.headers["Content-Type"] = "application/json"
183
+ faraday.headers["Authorization"] = "key=#{api_key}"
184
+ extra_headers.each do |key, value|
185
+ faraday.headers[key] = value
186
+ end
187
+ end
188
+ yield connection
210
189
  end
211
190
 
212
191
  def build_post_body(registration_ids, options = {})
@@ -216,8 +195,8 @@ class FCM
216
195
 
217
196
  def build_response(response, registration_ids = [])
218
197
  body = response.body || {}
219
- response_hash = { body: body, headers: response.headers, status_code: response.code }
220
- case response.code
198
+ response_hash = { body: body, headers: response.headers, status_code: response.status }
199
+ case response.status
221
200
  when 200
222
201
  response_hash[:response] = 'success'
223
202
  body = JSON.parse(body) unless body.empty?
@@ -260,16 +239,10 @@ class FCM
260
239
  end
261
240
 
262
241
  def execute_notification(body)
263
- params = {
264
- body: body.to_json,
265
- headers: {
266
- 'Authorization' => "key=#{@api_key}",
267
- 'Content-Type' => 'application/json'
268
- }
269
- }
270
-
271
- response = self.class.post('/send', params.merge(@client_options))
272
- build_response(response)
242
+ for_uri(BASE_URI) do |connection|
243
+ response = connection.post('/fcm/send', body.to_json)
244
+ build_response(response)
245
+ end
273
246
  end
274
247
 
275
248
  def has_canonical_id?(result)
data/spec/fcm_spec.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FCM do
4
- let(:send_url) { "#{FCM.base_uri}/send" }
5
- let(:group_notification_base_uri) { "https://android.googleapis.com/gcm/notification" }
4
+ let(:send_url) { "#{FCM::BASE_URI}/fcm/send" }
5
+ let(:group_notification_base_uri) { "#{FCM::GROUP_NOTIFICATION_BASE_URI}/gcm/notification" }
6
6
  let(:api_key) { 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA' }
7
7
  let(:registration_id) { '42' }
8
8
  let(:registration_ids) { ['42'] }
@@ -254,7 +254,7 @@ describe FCM do
254
254
 
255
255
  it 'should not send notification due to 599' do
256
256
  subject.send(registration_ids).should eq(body: '{"body-key" => "Body value"}',
257
- headers: { 'header-key' => ['Header value'] },
257
+ headers: { 'header-key' => 'Header value' },
258
258
  response: 'There was an internal error in the FCM server while trying to process the request.',
259
259
  status_code: 599)
260
260
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kashif Rasul
@@ -9,28 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-11-05 00:00:00.000000000 Z
12
+ date: 2020-02-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: httparty
15
+ name: faraday
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - '='
19
19
  - !ruby/object:Gem::Version
20
- version: '0.10'
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 0.10.0
20
+ version: 0.15.4
24
21
  type: :runtime
25
22
  prerelease: false
26
23
  version_requirements: !ruby/object:Gem::Requirement
27
24
  requirements:
28
- - - "~>"
29
- - !ruby/object:Gem::Version
30
- version: '0.10'
31
- - - ">="
25
+ - - '='
32
26
  - !ruby/object:Gem::Version
33
- version: 0.10.0
27
+ version: 0.15.4
34
28
  description: fcm provides ruby bindings to Firebase Cloud Messaging (FCM) a cross-platform
35
29
  messaging solution that lets you reliably deliver messages and notifications at
36
30
  no cost to Android, iOS or Web browsers.
@@ -71,8 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
65
  - !ruby/object:Gem::Version
72
66
  version: '0'
73
67
  requirements: []
74
- rubyforge_project: fcm
75
- rubygems_version: 2.6.13
68
+ rubygems_version: 3.1.2
76
69
  signing_key:
77
70
  specification_version: 4
78
71
  summary: Reliably deliver messages and notifications via FCM