Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A refactoring PR of send_v1 method #104

Merged
merged 7 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 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,20 @@ 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|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! 🙇

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 +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
Expand Down
68 changes: 67 additions & 1 deletion spec/fcm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,73 @@
end

describe "#send_v1" do
pending "should send message"
let(:project_name) { "project_name" }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

let(:send_v1_url) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" }
let(:access_token) { "access_token" }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

let(:valid_request_v1_headers) do
{
"Content-Type" => "application/json",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

"Authorization" => "Bearer #{access_token}",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.

}
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: "{}",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

headers: {},
status: 200,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call.

)
end

let(:authorizer_double) { double("token_fetcher") }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

let(:json_key_path) { double("file alike object") }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.


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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [121/80]

expect(authorizer_double).to receive(:fetch_access_token!).and_return({ "access_token" => access_token })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/BracesAroundHashParameters: Redundant curly braces around a hash parameter.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Metrics/LineLength: Line is too long. [111/80]

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