Skip to content

Commit 46629ec

Browse files
committed
create fcm http v1 protocol client
1 parent 95dca01 commit 46629ec

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

lib/fcm/client_v1.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'fcm/connection'
4+
require 'fcm/response'
5+
require 'fcm/client_v1/notification_delivery'
6+
7+
module Fcm
8+
# Fcm Client class for http v1 protocol API connections
9+
#
10+
# @see https://firebase.google.com/docs/cloud-messaging/migrate-v1
11+
class ClientV1
12+
include Fcm::Connection
13+
include Fcm::Response
14+
include Fcm::ClientV1::NotificationDilivery
15+
16+
TOKEN_URI = 'https://www.googleapis.com/auth/firebase.messaging'
17+
18+
# @see https://firebase.google.com/docs/projects/provisioning/configure-oauth#auth
19+
# @param json_key_path [String] file path to service_account_key.json
20+
# @return [ClientV1] client_v1 instance
21+
def initialize(json_key_path)
22+
@json_key_path = json_key_path
23+
end
24+
25+
private
26+
27+
def authorization_headers
28+
{
29+
'Content-Type' => 'application/json',
30+
'Authorization' => "Bearer #{jwt_token}"
31+
}
32+
end
33+
34+
def jwt_token
35+
@authorizer ||= Google::Auth::ServiceAccountCredentials.make_creds(
36+
json_key_io: json_key,
37+
scope: TOKEN_URI
38+
)
39+
token = @authorizer.fetch_access_token!
40+
token['access_token']
41+
end
42+
43+
def json_key
44+
@json_key ||= if @json_key_path.respond_to?(:read)
45+
@json_key_path
46+
else
47+
File.open(@json_key_path)
48+
end
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)