Class: Rpush::Daemon::GoogleCredentialCache

Inherits:
Object
  • Object
show all
Includes:
Loggable, Singleton
Defined in:
lib/rpush/daemon/google_credential_cache.rb

Constant Summary collapse

TOKEN_VALID_FOR_SEC =

Assuming tokens are valid for 1 hour

60 * 59

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_info, #log_warn

Constructor Details

#initializeGoogleCredentialCache

Returns a new instance of GoogleCredentialCache.



11
12
13
# File 'lib/rpush/daemon/google_credential_cache.rb', line 11

def initialize
  @credentials_cache = {}
end

Instance Method Details

#access_token(scope, json_key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rpush/daemon/google_credential_cache.rb', line 15

def access_token(scope, json_key)
  key = hash_key(scope, json_key)

  if @credentials_cache[key].nil? || Time.now > @credentials_cache[key][:expires_at]
    token = fetch_fresh_token(scope, json_key)
    expires_at = Time.now + TOKEN_VALID_FOR_SEC
    @credentials_cache[key] = { token: token, expires_at: expires_at }
  end

  @credentials_cache[key][:token]
end