Class: Maia::FCM::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/maia/fcm/credentials.rb

Constant Summary collapse

SCOPE =
'https://www.googleapis.com/auth/firebase.messaging'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path = ENV['GOOGLE_APPLICATION_CREDENTIALS'], cache: Rails.cache) ⇒ Credentials

Returns a new instance of Credentials.


8
9
10
11
# File 'lib/maia/fcm/credentials.rb', line 8

def initialize(path = ENV['GOOGLE_APPLICATION_CREDENTIALS'], cache: Rails.cache)
  @path  = path
  @cache = cache
end

Instance Method Details

#projectObject


13
14
15
# File 'lib/maia/fcm/credentials.rb', line 13

def project
  @project ||= to_h['project_id']
end

#to_hObject


23
24
25
# File 'lib/maia/fcm/credentials.rb', line 23

def to_h
  @to_h ||= JSON.parse file.read
end

#tokenObject


17
18
19
20
21
# File 'lib/maia/fcm/credentials.rb', line 17

def token
  @cache.fetch('maia-fcm-token', expires_in: 1.hour) do
    credentials.fetch_access_token!['access_token']
  end
end