Skip to content

Commit

Permalink
Merge pull request #104 from Roy-Mao/refactor_branch
Browse files Browse the repository at this point in the history
A refactoring PR of send_v1 method
  • Loading branch information
sabman authored Apr 28, 2022
2 parents 9c17861 + c1bda3d commit 07941b3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
26 changes: 12 additions & 14 deletions lib/fcm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@ 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"
INSTANCE_ID_API = "https://iid.googleapis.com"
TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/

attr_accessor :timeout, :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
Expand Down Expand Up @@ -48,20 +44,22 @@ 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)
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
Expand Down Expand Up @@ -228,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
Expand Down
70 changes: 69 additions & 1 deletion spec/fcm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,75 @@
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
Expand Down

0 comments on commit 07941b3

Please sign in to comment.