From aca1ee84eda5bfa8f15fa7813433c9fccfe69969 Mon Sep 17 00:00:00 2001 From: Roy Date: Thu, 28 Apr 2022 16:52:35 +0900 Subject: [PATCH 1/7] unused FORMAT constant --- lib/fcm.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/fcm.rb b/lib/fcm.rb index 4ef6d5c..685a47a 100644 --- a/lib/fcm.rb +++ b/lib/fcm.rb @@ -7,7 +7,6 @@ class FCM BASE_URI = "https://fcm.googleapis.com" BASE_URI_V1 = "https://fcm.googleapis.com/v1/projects/" DEFAULT_TIMEOUT = 30 - FORMAT = :json # constants GROUP_NOTIFICATION_BASE_URI = "https://android.googleapis.com" From 67bcec1eec784bbe936bfdc8062ed68682a8a645 Mon Sep 17 00:00:00 2001 From: Roy Date: Thu, 28 Apr 2022 16:53:23 +0900 Subject: [PATCH 2/7] unused timeout instance var --- lib/fcm.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fcm.rb b/lib/fcm.rb index 685a47a..fd5bad0 100644 --- a/lib/fcm.rb +++ b/lib/fcm.rb @@ -13,7 +13,7 @@ class FCM INSTANCE_ID_API = "https://iid.googleapis.com" TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/ - attr_accessor :timeout, :api_key, :json_key_path, :project_base_uri + attr_accessor :api_key, :json_key_path, :project_base_uri def initialize(api_key, json_key_path = "", project_name = "", client_options = {}) @api_key = api_key From 3240bdaca3b85394f554d746380cfa978d1d3f1b Mon Sep 17 00:00:00 2001 From: Roy Date: Thu, 28 Apr 2022 16:53:51 +0900 Subject: [PATCH 3/7] minor fix comments --- lib/fcm.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/fcm.rb b/lib/fcm.rb index fd5bad0..7e799ec 100644 --- a/lib/fcm.rb +++ b/lib/fcm.rb @@ -8,7 +8,6 @@ class FCM BASE_URI_V1 = "https://fcm.googleapis.com/v1/projects/" DEFAULT_TIMEOUT = 30 - # constants GROUP_NOTIFICATION_BASE_URI = "https://android.googleapis.com" INSTANCE_ID_API = "https://iid.googleapis.com" TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/ @@ -47,7 +46,7 @@ def initialize(api_key, json_key_path = "", project_name = "", client_options = # } # } # fcm = FCM.new(api_key, json_key_path, project_name) - # fcm.send( + # fcm.send_v1( # { "token": "4sdsx",, "to" : "notification": {}.. } # ) def send_notification_v1(message) From f217a4cc856595fde99d6bde136c5058557ee1e5 Mon Sep 17 00:00:00 2001 From: Roy Date: Fri, 29 Apr 2022 00:59:39 +0900 Subject: [PATCH 4/7] add rspec for #send_v1 method --- spec/fcm_spec.rb | 68 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/spec/fcm_spec.rb b/spec/fcm_spec.rb index fb1ee9e..07d367a 100644 --- a/spec/fcm_spec.rb +++ b/spec/fcm_spec.rb @@ -24,7 +24,73 @@ end describe "#send_v1" do - pending "should send message" + let(:project_name) { "project_name" } + let(:send_v1_url) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" } + let(:access_token) { "access_token" } + let(:valid_request_v1_headers) do + { + "Content-Type" => "application/json", + "Authorization" => "Bearer #{access_token}", + } + end + + let(:send_v1_params) do + { + "token" => "4sdsx", + "notification" => { + "title" => "Breaking News", + "body" => "New news story available." + }, + "data" => { + "story_id" => "story_12345" + }, + "android" => { + "notification" => { + "click_action": "TOP_STORY_ACTIVITY", + "body" => "Check out the Top Story" + } + }, + "apns" => { + "payload" => { + "aps" => { + "category" => "NEW_MESSAGE_CATEGORY" + } + } + } + } + end + + let(:valid_request_v1_body) do + { "message" => send_v1_params } + end + + let(:stub_fcm_send_v1_request) do + stub_request(:post, send_v1_url).with( + body: valid_request_v1_body.to_json, + headers: valid_request_v1_headers, + ).to_return( + # ref: https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream + body: "{}", + headers: {}, + status: 200, + ) + end + + let(:authorizer_double) { double("token_fetcher") } + let(:json_key_path) { double("file alike object") } + + before do + expect(json_key_path).to receive(:respond_to?).and_return(true) + expect(Google::Auth::ServiceAccountCredentials).to receive_message_chain(:make_creds).and_return(authorizer_double) + expect(authorizer_double).to receive(:fetch_access_token!).and_return({ "access_token" => access_token }) + stub_fcm_send_v1_request + end + + it "should send notification of HTTP V1 using POST to FCM server" do + fcm = FCM.new(api_key, json_key_path, project_name) + fcm.send_v1(send_v1_params).should eq(response: "success", body: "{}", headers: {}, status_code: 200) + stub_fcm_send_v1_request.should have_been_made.times(1) + end end describe "sending notification" do From 1b173fad3c28fe3057db5d20ea5dae2e939d9664 Mon Sep 17 00:00:00 2001 From: Roy Date: Fri, 29 Apr 2022 01:05:32 +0900 Subject: [PATCH 5/7] refactor send_v1 method to use for_uri method --- lib/fcm.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/fcm.rb b/lib/fcm.rb index 7e799ec..1a480cb 100644 --- a/lib/fcm.rb +++ b/lib/fcm.rb @@ -50,16 +50,16 @@ def initialize(api_key, json_key_path = "", project_name = "", client_options = # { "token": "4sdsx",, "to" : "notification": {}.. } # ) def send_notification_v1(message) - return if @project_base_uri.empty? + return if @project_name.empty? post_body = { 'message': message } - - response = Faraday.post("#{@project_base_uri}/messages:send") do |req| - req.headers["Content-Type"] = "application/json" - req.headers["Authorization"] = "Bearer #{jwt_token}" - req.body = post_body.to_json + extra_headers = { + "Authorization" => "Bearer #{jwt_token}" + } + for_uri(BASE_URI_V1, extra_headers) do |connection| + response = connection.post("#{@project_name}/messages:send", post_body.to_json) + build_response(response) end - build_response(response) end alias send_v1 send_notification_v1 From 0baceca8caf4e5a31a1554e36154cb91a5d12e4e Mon Sep 17 00:00:00 2001 From: Roy Date: Fri, 29 Apr 2022 01:06:41 +0900 Subject: [PATCH 6/7] no need to make these attributes visible outside of this obj --- lib/fcm.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/fcm.rb b/lib/fcm.rb index 1a480cb..37b13e0 100644 --- a/lib/fcm.rb +++ b/lib/fcm.rb @@ -12,13 +12,11 @@ class FCM INSTANCE_ID_API = "https://iid.googleapis.com" TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/ - attr_accessor :api_key, :json_key_path, :project_base_uri - def initialize(api_key, json_key_path = "", project_name = "", client_options = {}) @api_key = api_key @client_options = client_options @json_key_path = json_key_path - @project_base_uri = BASE_URI_V1 + project_name.to_s + @project_name = project_name end # See https://firebase.google.com/docs/cloud-messaging/send-message @@ -226,7 +224,7 @@ def for_uri(uri, extra_headers = {}) ) do |faraday| faraday.adapter Faraday.default_adapter faraday.headers["Content-Type"] = "application/json" - faraday.headers["Authorization"] = "key=#{api_key}" + faraday.headers["Authorization"] = "key=#{@api_key}" extra_headers.each do |key, value| faraday.headers[key] = value end From c1bda3d71a409078b68aff2f935719edd1ed7a84 Mon Sep 17 00:00:00 2001 From: Roy Date: Fri, 29 Apr 2022 01:30:00 +0900 Subject: [PATCH 7/7] follow hound rule: prefer single-quoted strings --- lib/fcm.rb | 8 +++++--- spec/fcm_spec.rb | 38 ++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/fcm.rb b/lib/fcm.rb index 37b13e0..08980ce 100644 --- a/lib/fcm.rb +++ b/lib/fcm.rb @@ -52,10 +52,12 @@ def send_notification_v1(message) post_body = { 'message': message } extra_headers = { - "Authorization" => "Bearer #{jwt_token}" + 'Authorization' => "Bearer #{jwt_token}" } for_uri(BASE_URI_V1, extra_headers) do |connection| - response = connection.post("#{@project_name}/messages:send", post_body.to_json) + response = connection.post( + "#{@project_name}/messages:send", post_body.to_json + ) build_response(response) end end @@ -224,7 +226,7 @@ def for_uri(uri, extra_headers = {}) ) do |faraday| faraday.adapter Faraday.default_adapter faraday.headers["Content-Type"] = "application/json" - faraday.headers["Authorization"] = "key=#{@api_key}" + faraday.headers['Authorization'] = "key=#{@api_key}" extra_headers.each do |key, value| faraday.headers[key] = value end diff --git a/spec/fcm_spec.rb b/spec/fcm_spec.rb index 07d367a..e976433 100644 --- a/spec/fcm_spec.rb +++ b/spec/fcm_spec.rb @@ -36,24 +36,24 @@ let(:send_v1_params) do { - "token" => "4sdsx", - "notification" => { - "title" => "Breaking News", - "body" => "New news story available." + 'token' => '4sdsx', + 'notification' => { + 'title' => 'Breaking News', + 'body' => 'New news story available.' }, - "data" => { - "story_id" => "story_12345" + 'data' => { + 'story_id' => 'story_12345' }, - "android" => { - "notification" => { - "click_action": "TOP_STORY_ACTIVITY", - "body" => "Check out the Top Story" + 'android' => { + 'notification' => { + 'click_action' => 'TOP_STORY_ACTIVITY', + 'body' => 'Check out the Top Story' } }, - "apns" => { - "payload" => { - "aps" => { - "category" => "NEW_MESSAGE_CATEGORY" + 'apns' => { + 'payload' => { + 'aps' => { + 'category' => 'NEW_MESSAGE_CATEGORY' } } } @@ -61,13 +61,13 @@ end let(:valid_request_v1_body) do - { "message" => send_v1_params } + { 'message' => send_v1_params } end let(:stub_fcm_send_v1_request) do stub_request(:post, send_v1_url).with( body: valid_request_v1_body.to_json, - headers: valid_request_v1_headers, + headers: valid_request_v1_headers ).to_return( # ref: https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream body: "{}", @@ -86,9 +86,11 @@ stub_fcm_send_v1_request end - it "should send notification of HTTP V1 using POST to FCM server" do + it 'should send notification of HTTP V1 using POST to FCM server' do fcm = FCM.new(api_key, json_key_path, project_name) - fcm.send_v1(send_v1_params).should eq(response: "success", body: "{}", headers: {}, status_code: 200) + fcm.send_v1(send_v1_params).should eq( + response: 'success', body: '{}', headers: {}, status_code: 200 + ) stub_fcm_send_v1_request.should have_been_made.times(1) end end