Skip to content

Commit

Permalink
more refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy-Mao committed May 16, 2022
1 parent 39ed5b6 commit 9a4d889
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 100 deletions.
83 changes: 4 additions & 79 deletions lib/fcm/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,27 @@
require 'fcm/connection'
require 'fcm/client/notification_delivery'
require 'fcm/client/notification_setting'
require 'fcm/client/instance_topic_management'

module Fcm
# A Fcm Client class to handle legacy protocol API connections
class Client
include Connection
include Fcm::Connection
include Fcm::Client::NotificationDilivery
include Fcm::Client::NotificationSetting

INSTANCE_ID_API = 'https://iid.googleapis.com'
TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/
include Fcm::Client::InstanceTopicManagement

def initialize(api_key)
@api_key = api_key
end

def topic_subscription(topic, registration_id)
end_point = "/iid/v1/#{registration_id}/rel/topics/#{topic}"
res = make_request(:post, INSTANCE_ID_API, end_point, nil, client_headers)
build_response(res)
end

def batch_topic_subscription(topic, registration_ids)
manage_topics_relationship(topic, registration_ids, 'Add')
end

def batch_topic_unsubscription(topic, registration_ids)
manage_topics_relationship(topic, registration_ids, 'Remove')
end

def batch_subscribe_instance_ids_to_topic(instance_ids, topic_name)
manage_topics_relationship(topic_name, instance_ids, 'Add')
end

def batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name)
manage_topics_relationship(topic_name, instance_ids, 'Remove')
end

def subscribe_instance_id_to_topic(iid_token, topic_name)
batch_subscribe_instance_ids_to_topic([iid_token], topic_name)
end

def unsubscribe_instance_id_from_topic(iid_token, topic_name)
batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name)
end

def get_instance_id_info(iid_token, options = {})
params = options
end_point = "/iid/info/#{iid_token}"
res = make_request(
:get, INSTANCE_ID_API, end_point, params, client_headers
)
build_response(res)
end

def send_to_topic(topic, options = {})
return unless topic.gsub(TOPIC_REGEX, '').length.zero?
send_with_notification_key('/topics/' + topic, options)
end

private

def client_headers
def authorization_headers
{
'Content-Type' => 'application/json',
'Authorization' => "key=#{@api_key}"
}
end

def manage_topics_relationship(topic, registration_ids, action)
body = { to: "/topics/#{topic}", registration_tokens: registration_ids }
end_point = "/iid/v1:batch#{action}"
res = make_request(
:post, INSTANCE_ID_API, end_point, body.to_json, client_headers
)
build_response(res, registration_ids)
end

def validate_condition?(condition)
validate_condition_format?(
condition
) && validate_condition_topics?(
condition
)
end

def validate_condition_format?(condition)
bad_characters = condition.gsub(
/(topics|in|\s|\(|\)|(&&)|[!]|(\|\|)|'([a-zA-Z0-9\-_.~%]+)')/,
''
)
bad_characters.length.zero?
end

def validate_condition_topics?(condition)
topics = condition.scan(/(?:^|\S|\s)'([^']*?)'(?:$|\S|\s)/).flatten
topics.all? { |topic| topic.gsub(TOPIC_REGEX, '').length.zero? }
end
end
end
58 changes: 58 additions & 0 deletions lib/fcm/client/instance_topic_management.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

module Fcm
class Client
# A Fcm Client class to handle notification setting methods
module InstanceTopicManagement
INSTANCE_ID_API = 'https://iid.googleapis.com'

def manage_topics_relationship(topic, registration_ids, action)
body = { to: "/topics/#{topic}", registration_tokens: registration_ids }
end_point = "/iid/v1:batch#{action}"
res = make_request(
:post, INSTANCE_ID_API, end_point, body.to_json, authorization_headers
)
build_response(res, registration_ids)
end

def topic_subscription(topic, registration_id)
end_point = "/iid/v1/#{registration_id}/rel/topics/#{topic}"
res = make_request(:post, INSTANCE_ID_API, end_point, nil, authorization_headers)
build_response(res)
end

def get_instance_id_info(iid_token, options = {})
params = options
end_point = "/iid/info/#{iid_token}"
res = make_request(
:get, INSTANCE_ID_API, end_point, params, authorization_headers
)
build_response(res)
end

def batch_topic_subscription(topic, registration_ids)
manage_topics_relationship(topic, registration_ids, 'Add')
end

def batch_topic_unsubscription(topic, registration_ids)
manage_topics_relationship(topic, registration_ids, 'Remove')
end

def batch_subscribe_instance_ids_to_topic(instance_ids, topic_name)
manage_topics_relationship(topic_name, instance_ids, 'Add')
end

def batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name)
manage_topics_relationship(topic_name, instance_ids, 'Remove')
end

def subscribe_instance_id_to_topic(iid_token, topic_name)
batch_subscribe_instance_ids_to_topic([iid_token], topic_name)
end

def unsubscribe_instance_id_from_topic(iid_token, topic_name)
batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name)
end
end
end
end
35 changes: 32 additions & 3 deletions lib/fcm/client/notification_delivery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ class Client
module NotificationDilivery
BASE_URI = 'https://fcm.googleapis.com'
END_POINT = '/fcm/send'
TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/

def send_notification(registration_ids, options = {})
post_body = build_post_body(registration_ids, options)
res = make_request(
:post, BASE_URI, END_POINT, post_body, client_headers
:post, BASE_URI, END_POINT, post_body, authorization_headers
)
build_response(res, registration_ids)
end

def send_with_notification_key(notification_key, options = {})
body = { to: notification_key }.merge(options)
res = make_request(
:post, BASE_URI, END_POINT, body.to_json, client_headers
:post, BASE_URI, END_POINT, body.to_json, authorization_headers
)
build_response(res)
end
Expand All @@ -28,10 +29,38 @@ def send_to_topic_condition(condition, options = {})
body = { condition: condition }.merge(options)

res = make_request(
:post, BASE_URI, END_POINT, body.to_json, client_headers
:post, BASE_URI, END_POINT, body.to_json, authorization_headers
)
build_response(res)
end

def send_to_topic(topic, options = {})
return unless topic.gsub(TOPIC_REGEX, '').length.zero?
send_with_notification_key('/topics/' + topic, options)
end

private

def validate_condition?(condition)
validate_condition_format?(
condition
) && validate_condition_topics?(
condition
)
end

def validate_condition_format?(condition)
bad_characters = condition.gsub(
/(topics|in|\s|\(|\)|(&&)|[!]|(\|\|)|'([a-zA-Z0-9\-_.~%]+)')/,
''
)
bad_characters.length.zero?
end

def validate_condition_topics?(condition)
topics = condition.scan(/(?:^|\S|\s)'([^']*?)'(?:$|\S|\s)/).flatten
topics.all? { |topic| topic.gsub(TOPIC_REGEX, '').length.zero? }
end
end
end
end
8 changes: 4 additions & 4 deletions lib/fcm/client/notification_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def create_notification_key(key_name, project_id, registration_ids = [])
GROUP_NOTIFICATION_BASE_URI,
END_POINT,
post_body,
client_headers.merge('project_id' => project_id)
authorization_headers.merge('project_id' => project_id)
)
build_response(res)
end
Expand All @@ -35,7 +35,7 @@ def add_registration_ids(key_name, project_id, notification_key, register_ids)
GROUP_NOTIFICATION_BASE_URI,
END_POINT,
post_body,
client_headers.merge('project_id' => project_id)
authorization_headers.merge('project_id' => project_id)
)
build_response(res)
end
Expand All @@ -52,7 +52,7 @@ def remove_registration_ids(key_name, project_id, notif_key, register_ids)
GROUP_NOTIFICATION_BASE_URI,
END_POINT,
post_body,
client_headers.merge('project_id' => project_id)
authorization_headers.merge('project_id' => project_id)
)
build_response(res)
end
Expand All @@ -64,7 +64,7 @@ def recover_notification_key(key_name, project_id)
GROUP_NOTIFICATION_BASE_URI,
END_POINT,
params,
client_headers.merge('project_id' => project_id)
authorization_headers.merge('project_id' => project_id)
)
build_response(res)
end
Expand Down
23 changes: 23 additions & 0 deletions lib/fcm/client_v1/notification_delivery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Fcm
class ClientV1
# Handle notification delivery methods
module NotificationDilivery
BASE_URI_V1 = 'https://fcm.googleapis.com/v1/projects/'
TOKEN_URI = 'https://www.googleapis.com/auth/firebase.messaging'

def send_notification_v1(message, project_name)
return if project_name.empty?

post_body = { 'message': message }
end_point = "#{project_name}/messages:send"

res = make_request(
:post, BASE_URI_V1, end_point, post_body.to_json, authorization_headers
)
build_response(res)
end
end
end
end
18 changes: 4 additions & 14 deletions lib/fcm/v1_client.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

require 'fcm/connection'
require 'fcm/client_v1/notification_delivery'

module Fcm
# A Fcm Client class to handle http v1 protocol API connections
class V1Client
include Connection
include Fcm::Connection
include Fcm::ClientV1::NotificationDilivery

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

def send_notification_v1(message, project_name)
return if project_name.empty?

post_body = { 'message': message }
end_point = "#{project_name}/messages:send"

res = make_request(
:post, BASE_URI_V1, end_point, post_body.to_json, client_headers
)
build_response(res)
end

private

def client_headers
def authorization_headers
{
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{jwt_token}"
Expand Down

0 comments on commit 9a4d889

Please sign in to comment.