Class: HelpScout::API::AccessToken
- Inherits:
-
Object
- Object
- HelpScout::API::AccessToken
- Defined in:
- lib/help_scout/api/access_token.rb,
lib/help_scout/api/access_token/cache.rb,
lib/help_scout/api/access_token/request.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#invalid ⇒ Object
Returns the value of attribute invalid.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #expired? ⇒ Boolean
-
#initialize(params) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #invalid? ⇒ Boolean
- #invalidate! ⇒ Object
- #stale? ⇒ Boolean
Constructor Details
#initialize(params) ⇒ AccessToken
Returns a new instance of AccessToken.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/help_scout/api/access_token.rb', line 28 def initialize(params) @value = params[:access_token] if params[:expires_at] @expires_at = DateTime.parse(params[:expires_at].to_s).to_time.utc elsif params[:expires_in] @expires_in = params[:expires_in].to_i @expires_at = (Time.now.utc + @expires_in) end end |
Instance Attribute Details
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
26 27 28 |
# File 'lib/help_scout/api/access_token.rb', line 26 def expires_at @expires_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
26 27 28 |
# File 'lib/help_scout/api/access_token.rb', line 26 def expires_in @expires_in end |
#invalid ⇒ Object
Returns the value of attribute invalid.
25 26 27 |
# File 'lib/help_scout/api/access_token.rb', line 25 def invalid @invalid end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
26 27 28 |
# File 'lib/help_scout/api/access_token.rb', line 26 def value @value end |
Class Method Details
.create ⇒ Object
11 12 13 14 15 16 |
# File 'lib/help_scout/api/access_token.rb', line 11 def create cache = HelpScout::API::AccessToken::Cache.new request = HelpScout::API::AccessToken::Request.new cache.configured? ? cache.fetch_token { request.execute } : request.execute end |
.refresh! ⇒ Object
18 19 20 21 22 |
# File 'lib/help_scout/api/access_token.rb', line 18 def refresh! return HelpScout.access_token unless HelpScout.access_token.nil? || HelpScout.access_token.stale? HelpScout.api.access_token = create end |
Instance Method Details
#as_json ⇒ Object
39 40 41 42 43 44 |
# File 'lib/help_scout/api/access_token.rb', line 39 def as_json(*) { access_token: value, expires_at: expires_at } end |
#expired? ⇒ Boolean
46 47 48 49 50 |
# File 'lib/help_scout/api/access_token.rb', line 46 def expired? return false unless expires_at Time.now.utc > expires_at end |
#invalid? ⇒ Boolean
52 53 54 |
# File 'lib/help_scout/api/access_token.rb', line 52 def invalid? !!invalid # rubocop:disable Style/DoubleNegation end |
#invalidate! ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/help_scout/api/access_token.rb', line 56 def invalidate! cache = HelpScout::API::AccessToken::Cache.new cache.delete if cache.configured? self.invalid = true end |
#stale? ⇒ Boolean
64 65 66 |
# File 'lib/help_scout/api/access_token.rb', line 64 def stale? invalid? || expired? end |