Class: HelpScout::API::AccessToken::Cache
- Inherits:
-
Object
- Object
- HelpScout::API::AccessToken::Cache
- Defined in:
- lib/help_scout/api/access_token/cache.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #configured? ⇒ Boolean
- #delete ⇒ Object
- #fetch_token(&token_request) ⇒ Object
-
#initialize(backend: nil, key: nil) ⇒ Cache
constructor
A new instance of Cache.
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
#backend ⇒ Object (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 |
#key ⇒ Object (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
14 15 16 |
# File 'lib/help_scout/api/access_token/cache.rb', line 14 def configured? backend.present? && key.present? end |
#delete ⇒ Object
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
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 |