Skip to content

Commit 07941b3

Browse files
authored
Merge pull request #104 from Roy-Mao/refactor_branch
A refactoring PR of send_v1 method
2 parents 9c17861 + c1bda3d commit 07941b3

File tree

2 files changed

+81
-15
lines changed

2 files changed

+81
-15
lines changed

lib/fcm.rb

+12-14
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@ class FCM
77
BASE_URI = "https://fcm.googleapis.com"
88
BASE_URI_V1 = "https://fcm.googleapis.com/v1/projects/"
99
DEFAULT_TIMEOUT = 30
10-
FORMAT = :json
1110

12-
# constants
1311
GROUP_NOTIFICATION_BASE_URI = "https://android.googleapis.com"
1412
INSTANCE_ID_API = "https://iid.googleapis.com"
1513
TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/
1614

17-
attr_accessor :timeout, :api_key, :json_key_path, :project_base_uri
18-
1915
def initialize(api_key, json_key_path = "", project_name = "", client_options = {})
2016
@api_key = api_key
2117
@client_options = client_options
2218
@json_key_path = json_key_path
23-
@project_base_uri = BASE_URI_V1 + project_name.to_s
19+
@project_name = project_name
2420
end
2521

2622
# See https://firebase.google.com/docs/cloud-messaging/send-message
@@ -48,20 +44,22 @@ def initialize(api_key, json_key_path = "", project_name = "", client_options =
4844
# }
4945
# }
5046
# fcm = FCM.new(api_key, json_key_path, project_name)
51-
# fcm.send(
47+
# fcm.send_v1(
5248
# { "token": "4sdsx",, "to" : "notification": {}.. }
5349
# )
5450
def send_notification_v1(message)
55-
return if @project_base_uri.empty?
51+
return if @project_name.empty?
5652

5753
post_body = { 'message': message }
58-
59-
response = Faraday.post("#{@project_base_uri}/messages:send") do |req|
60-
req.headers["Content-Type"] = "application/json"
61-
req.headers["Authorization"] = "Bearer #{jwt_token}"
62-
req.body = post_body.to_json
54+
extra_headers = {
55+
'Authorization' => "Bearer #{jwt_token}"
56+
}
57+
for_uri(BASE_URI_V1, extra_headers) do |connection|
58+
response = connection.post(
59+
"#{@project_name}/messages:send", post_body.to_json
60+
)
61+
build_response(response)
6362
end
64-
build_response(response)
6563
end
6664

6765
alias send_v1 send_notification_v1
@@ -228,7 +226,7 @@ def for_uri(uri, extra_headers = {})
228226
) do |faraday|
229227
faraday.adapter Faraday.default_adapter
230228
faraday.headers["Content-Type"] = "application/json"
231-
faraday.headers["Authorization"] = "key=#{api_key}"
229+
faraday.headers['Authorization'] = "key=#{@api_key}"
232230
extra_headers.each do |key, value|
233231
faraday.headers[key] = value
234232
end

spec/fcm_spec.rb

+69-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,75 @@
2424
end
2525

2626
describe "#send_v1" do
27-
pending "should send message"
27+
let(:project_name) { "project_name" }
28+
let(:send_v1_url) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" }
29+
let(:access_token) { "access_token" }
30+
let(:valid_request_v1_headers) do
31+
{
32+
"Content-Type" => "application/json",
33+
"Authorization" => "Bearer #{access_token}",
34+
}
35+
end
36+
37+
let(:send_v1_params) do
38+
{
39+
'token' => '4sdsx',
40+
'notification' => {
41+
'title' => 'Breaking News',
42+
'body' => 'New news story available.'
43+
},
44+
'data' => {
45+
'story_id' => 'story_12345'
46+
},
47+
'android' => {
48+
'notification' => {
49+
'click_action' => 'TOP_STORY_ACTIVITY',
50+
'body' => 'Check out the Top Story'
51+
}
52+
},
53+
'apns' => {
54+
'payload' => {
55+
'aps' => {
56+
'category' => 'NEW_MESSAGE_CATEGORY'
57+
}
58+
}
59+
}
60+
}
61+
end
62+
63+
let(:valid_request_v1_body) do
64+
{ 'message' => send_v1_params }
65+
end
66+
67+
let(:stub_fcm_send_v1_request) do
68+
stub_request(:post, send_v1_url).with(
69+
body: valid_request_v1_body.to_json,
70+
headers: valid_request_v1_headers
71+
).to_return(
72+
# ref: https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream
73+
body: "{}",
74+
headers: {},
75+
status: 200,
76+
)
77+
end
78+
79+
let(:authorizer_double) { double("token_fetcher") }
80+
let(:json_key_path) { double("file alike object") }
81+
82+
before do
83+
expect(json_key_path).to receive(:respond_to?).and_return(true)
84+
expect(Google::Auth::ServiceAccountCredentials).to receive_message_chain(:make_creds).and_return(authorizer_double)
85+
expect(authorizer_double).to receive(:fetch_access_token!).and_return({ "access_token" => access_token })
86+
stub_fcm_send_v1_request
87+
end
88+
89+
it 'should send notification of HTTP V1 using POST to FCM server' do
90+
fcm = FCM.new(api_key, json_key_path, project_name)
91+
fcm.send_v1(send_v1_params).should eq(
92+
response: 'success', body: '{}', headers: {}, status_code: 200
93+
)
94+
stub_fcm_send_v1_request.should have_been_made.times(1)
95+
end
2896
end
2997

3098
describe "sending notification" do

0 commit comments

Comments
 (0)