Class: Underway::TokenCache

Inherits:
Object
  • Object
show all
Defined in:
lib/underway/token_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database) ⇒ TokenCache

Returns a new instance of TokenCache.



5
6
7
# File 'lib/underway/token_cache.rb', line 5

def initialize(database)
  @db = database
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



3
4
5
# File 'lib/underway/token_cache.rb', line 3

def db
  @db
end

Instance Method Details

#lookup_installation_auth_token(id:) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/underway/token_cache.rb', line 9

def lookup_installation_auth_token(id:)
  results = db[:cached_tokens].where(installation_id: id)
    .where{expires_at >= DateTime.now.new_offset(0)} # Force UTC Timezone
    .reverse(:expires_at)
  if results.any?
    results.first[:token]
  end
end

#store_installation_auth_token(id:, token:, expires_at:) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/underway/token_cache.rb', line 18

def store_installation_auth_token(id:, token:, expires_at:)
  db[:cached_tokens].insert(
    installation_id: id,
    token: token,
    expires_at: DateTime.parse(expires_at)
  )
end