Class: HelpScout::API::AccessToken::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/help_scout/api/access_token/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend: nil, key: nil) ⇒ Cache

Returns a new instance of Cache.



9
10
11
12
# File 'lib/help_scout/api/access_token/cache.rb', line 9

def initialize(backend: nil, key: nil)
  @backend = backend || HelpScout.configuration.token_cache
  @key = key || HelpScout.configuration.token_cache_key
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



7
8
9
# File 'lib/help_scout/api/access_token/cache.rb', line 7

def backend
  @backend
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/help_scout/api/access_token/cache.rb', line 7

def key
  @key
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/help_scout/api/access_token/cache.rb', line 14

def configured?
  backend.present? && key.present?
end

#deleteObject



18
19
20
# File 'lib/help_scout/api/access_token/cache.rb', line 18

def delete
  backend.delete(key)
end

#fetch_token(&token_request) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
# File 'lib/help_scout/api/access_token/cache.rb', line 22

def fetch_token(&token_request)
  raise ArgumentError, 'A request fallback block is required' unless block_given?

  if (cached_token_json = backend.read(key))
    AccessToken.new(JSON.parse(cached_token_json, symbolize_names: true))
  else
    token_request.call.tap { |token| write(token) }
  end
end