Class: HelpScout::API::AccessToken

Inherits:
Object
  • Object
show all
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

Classes: Cache, Request

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_atObject (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_inObject (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

#invalidObject

Returns the value of attribute invalid.



25
26
27
# File 'lib/help_scout/api/access_token.rb', line 25

def invalid
  @invalid
end

#valueObject (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

.createObject



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_jsonObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


64
65
66
# File 'lib/help_scout/api/access_token.rb', line 64

def stale?
  invalid? || expired?
end