Skip to content

Commit 9a4d889

Browse files
committed
more refactor
1 parent 39ed5b6 commit 9a4d889

File tree

6 files changed

+125
-100
lines changed

6 files changed

+125
-100
lines changed

lib/fcm/client.rb

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,102 +3,27 @@
33
require 'fcm/connection'
44
require 'fcm/client/notification_delivery'
55
require 'fcm/client/notification_setting'
6+
require 'fcm/client/instance_topic_management'
67

78
module Fcm
89
# A Fcm Client class to handle legacy protocol API connections
910
class Client
10-
include Connection
11+
include Fcm::Connection
1112
include Fcm::Client::NotificationDilivery
1213
include Fcm::Client::NotificationSetting
13-
14-
INSTANCE_ID_API = 'https://iid.googleapis.com'
15-
TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/
14+
include Fcm::Client::InstanceTopicManagement
1615

1716
def initialize(api_key)
1817
@api_key = api_key
1918
end
2019

21-
def topic_subscription(topic, registration_id)
22-
end_point = "/iid/v1/#{registration_id}/rel/topics/#{topic}"
23-
res = make_request(:post, INSTANCE_ID_API, end_point, nil, client_headers)
24-
build_response(res)
25-
end
26-
27-
def batch_topic_subscription(topic, registration_ids)
28-
manage_topics_relationship(topic, registration_ids, 'Add')
29-
end
30-
31-
def batch_topic_unsubscription(topic, registration_ids)
32-
manage_topics_relationship(topic, registration_ids, 'Remove')
33-
end
34-
35-
def batch_subscribe_instance_ids_to_topic(instance_ids, topic_name)
36-
manage_topics_relationship(topic_name, instance_ids, 'Add')
37-
end
38-
39-
def batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name)
40-
manage_topics_relationship(topic_name, instance_ids, 'Remove')
41-
end
42-
43-
def subscribe_instance_id_to_topic(iid_token, topic_name)
44-
batch_subscribe_instance_ids_to_topic([iid_token], topic_name)
45-
end
46-
47-
def unsubscribe_instance_id_from_topic(iid_token, topic_name)
48-
batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name)
49-
end
50-
51-
def get_instance_id_info(iid_token, options = {})
52-
params = options
53-
end_point = "/iid/info/#{iid_token}"
54-
res = make_request(
55-
:get, INSTANCE_ID_API, end_point, params, client_headers
56-
)
57-
build_response(res)
58-
end
59-
60-
def send_to_topic(topic, options = {})
61-
return unless topic.gsub(TOPIC_REGEX, '').length.zero?
62-
send_with_notification_key('/topics/' + topic, options)
63-
end
64-
6520
private
6621

67-
def client_headers
22+
def authorization_headers
6823
{
6924
'Content-Type' => 'application/json',
7025
'Authorization' => "key=#{@api_key}"
7126
}
7227
end
73-
74-
def manage_topics_relationship(topic, registration_ids, action)
75-
body = { to: "/topics/#{topic}", registration_tokens: registration_ids }
76-
end_point = "/iid/v1:batch#{action}"
77-
res = make_request(
78-
:post, INSTANCE_ID_API, end_point, body.to_json, client_headers
79-
)
80-
build_response(res, registration_ids)
81-
end
82-
83-
def validate_condition?(condition)
84-
validate_condition_format?(
85-
condition
86-
) && validate_condition_topics?(
87-
condition
88-
)
89-
end
90-
91-
def validate_condition_format?(condition)
92-
bad_characters = condition.gsub(
93-
/(topics|in|\s|\(|\)|(&&)|[!]|(\|\|)|'([a-zA-Z0-9\-_.~%]+)')/,
94-
''
95-
)
96-
bad_characters.length.zero?
97-
end
98-
99-
def validate_condition_topics?(condition)
100-
topics = condition.scan(/(?:^|\S|\s)'([^']*?)'(?:$|\S|\s)/).flatten
101-
topics.all? { |topic| topic.gsub(TOPIC_REGEX, '').length.zero? }
102-
end
10328
end
10429
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
module Fcm
4+
class Client
5+
# A Fcm Client class to handle notification setting methods
6+
module InstanceTopicManagement
7+
INSTANCE_ID_API = 'https://iid.googleapis.com'
8+
9+
def manage_topics_relationship(topic, registration_ids, action)
10+
body = { to: "/topics/#{topic}", registration_tokens: registration_ids }
11+
end_point = "/iid/v1:batch#{action}"
12+
res = make_request(
13+
:post, INSTANCE_ID_API, end_point, body.to_json, authorization_headers
14+
)
15+
build_response(res, registration_ids)
16+
end
17+
18+
def topic_subscription(topic, registration_id)
19+
end_point = "/iid/v1/#{registration_id}/rel/topics/#{topic}"
20+
res = make_request(:post, INSTANCE_ID_API, end_point, nil, authorization_headers)
21+
build_response(res)
22+
end
23+
24+
def get_instance_id_info(iid_token, options = {})
25+
params = options
26+
end_point = "/iid/info/#{iid_token}"
27+
res = make_request(
28+
:get, INSTANCE_ID_API, end_point, params, authorization_headers
29+
)
30+
build_response(res)
31+
end
32+
33+
def batch_topic_subscription(topic, registration_ids)
34+
manage_topics_relationship(topic, registration_ids, 'Add')
35+
end
36+
37+
def batch_topic_unsubscription(topic, registration_ids)
38+
manage_topics_relationship(topic, registration_ids, 'Remove')
39+
end
40+
41+
def batch_subscribe_instance_ids_to_topic(instance_ids, topic_name)
42+
manage_topics_relationship(topic_name, instance_ids, 'Add')
43+
end
44+
45+
def batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name)
46+
manage_topics_relationship(topic_name, instance_ids, 'Remove')
47+
end
48+
49+
def subscribe_instance_id_to_topic(iid_token, topic_name)
50+
batch_subscribe_instance_ids_to_topic([iid_token], topic_name)
51+
end
52+
53+
def unsubscribe_instance_id_from_topic(iid_token, topic_name)
54+
batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name)
55+
end
56+
end
57+
end
58+
end

lib/fcm/client/notification_delivery.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ class Client
66
module NotificationDilivery
77
BASE_URI = 'https://fcm.googleapis.com'
88
END_POINT = '/fcm/send'
9+
TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/
910

1011
def send_notification(registration_ids, options = {})
1112
post_body = build_post_body(registration_ids, options)
1213
res = make_request(
13-
:post, BASE_URI, END_POINT, post_body, client_headers
14+
:post, BASE_URI, END_POINT, post_body, authorization_headers
1415
)
1516
build_response(res, registration_ids)
1617
end
1718

1819
def send_with_notification_key(notification_key, options = {})
1920
body = { to: notification_key }.merge(options)
2021
res = make_request(
21-
:post, BASE_URI, END_POINT, body.to_json, client_headers
22+
:post, BASE_URI, END_POINT, body.to_json, authorization_headers
2223
)
2324
build_response(res)
2425
end
@@ -28,10 +29,38 @@ def send_to_topic_condition(condition, options = {})
2829
body = { condition: condition }.merge(options)
2930

3031
res = make_request(
31-
:post, BASE_URI, END_POINT, body.to_json, client_headers
32+
:post, BASE_URI, END_POINT, body.to_json, authorization_headers
3233
)
3334
build_response(res)
3435
end
36+
37+
def send_to_topic(topic, options = {})
38+
return unless topic.gsub(TOPIC_REGEX, '').length.zero?
39+
send_with_notification_key('/topics/' + topic, options)
40+
end
41+
42+
private
43+
44+
def validate_condition?(condition)
45+
validate_condition_format?(
46+
condition
47+
) && validate_condition_topics?(
48+
condition
49+
)
50+
end
51+
52+
def validate_condition_format?(condition)
53+
bad_characters = condition.gsub(
54+
/(topics|in|\s|\(|\)|(&&)|[!]|(\|\|)|'([a-zA-Z0-9\-_.~%]+)')/,
55+
''
56+
)
57+
bad_characters.length.zero?
58+
end
59+
60+
def validate_condition_topics?(condition)
61+
topics = condition.scan(/(?:^|\S|\s)'([^']*?)'(?:$|\S|\s)/).flatten
62+
topics.all? { |topic| topic.gsub(TOPIC_REGEX, '').length.zero? }
63+
end
3564
end
3665
end
3766
end

lib/fcm/client/notification_setting.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def create_notification_key(key_name, project_id, registration_ids = [])
1818
GROUP_NOTIFICATION_BASE_URI,
1919
END_POINT,
2020
post_body,
21-
client_headers.merge('project_id' => project_id)
21+
authorization_headers.merge('project_id' => project_id)
2222
)
2323
build_response(res)
2424
end
@@ -35,7 +35,7 @@ def add_registration_ids(key_name, project_id, notification_key, register_ids)
3535
GROUP_NOTIFICATION_BASE_URI,
3636
END_POINT,
3737
post_body,
38-
client_headers.merge('project_id' => project_id)
38+
authorization_headers.merge('project_id' => project_id)
3939
)
4040
build_response(res)
4141
end
@@ -52,7 +52,7 @@ def remove_registration_ids(key_name, project_id, notif_key, register_ids)
5252
GROUP_NOTIFICATION_BASE_URI,
5353
END_POINT,
5454
post_body,
55-
client_headers.merge('project_id' => project_id)
55+
authorization_headers.merge('project_id' => project_id)
5656
)
5757
build_response(res)
5858
end
@@ -64,7 +64,7 @@ def recover_notification_key(key_name, project_id)
6464
GROUP_NOTIFICATION_BASE_URI,
6565
END_POINT,
6666
params,
67-
client_headers.merge('project_id' => project_id)
67+
authorization_headers.merge('project_id' => project_id)
6868
)
6969
build_response(res)
7070
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module Fcm
4+
class ClientV1
5+
# Handle notification delivery methods
6+
module NotificationDilivery
7+
BASE_URI_V1 = 'https://fcm.googleapis.com/v1/projects/'
8+
TOKEN_URI = 'https://www.googleapis.com/auth/firebase.messaging'
9+
10+
def send_notification_v1(message, project_name)
11+
return if project_name.empty?
12+
13+
post_body = { 'message': message }
14+
end_point = "#{project_name}/messages:send"
15+
16+
res = make_request(
17+
:post, BASE_URI_V1, end_point, post_body.to_json, authorization_headers
18+
)
19+
build_response(res)
20+
end
21+
end
22+
end
23+
end

lib/fcm/v1_client.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# frozen_string_literal: true
22

33
require 'fcm/connection'
4+
require 'fcm/client_v1/notification_delivery'
45

56
module Fcm
67
# A Fcm Client class to handle http v1 protocol API connections
78
class V1Client
8-
include Connection
9+
include Fcm::Connection
10+
include Fcm::ClientV1::NotificationDilivery
911

1012
TOKEN_URI = 'https://www.googleapis.com/auth/firebase.messaging'
1113
BASE_URI_V1 = 'https://fcm.googleapis.com/v1/projects/'
@@ -14,21 +16,9 @@ def initialize(json_key_path)
1416
@json_key_path = json_key_path
1517
end
1618

17-
def send_notification_v1(message, project_name)
18-
return if project_name.empty?
19-
20-
post_body = { 'message': message }
21-
end_point = "#{project_name}/messages:send"
22-
23-
res = make_request(
24-
:post, BASE_URI_V1, end_point, post_body.to_json, client_headers
25-
)
26-
build_response(res)
27-
end
28-
2919
private
3020

31-
def client_headers
21+
def authorization_headers
3222
{
3323
'Content-Type' => 'application/json',
3424
'Authorization' => "Bearer #{jwt_token}"

0 commit comments

Comments
 (0)