Class: AccessTokenWrapper::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/access_token_wrapper/base.rb

Constant Summary collapse

NON_ERROR_CODES =
[402, 404, 422, 414, 429, 500, 503]
EXPIRY_GRACE_SEC =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_token, &callback) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/access_token_wrapper/base.rb', line 7

def initialize(raw_token, &callback)
  @raw_token = raw_token
  @callback  = callback
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/access_token_wrapper/base.rb', line 12

def method_missing(method_name, *args, &block)
  refresh_token! if token_expiring?
  @raw_token.send(method_name, *args, &block)
rescue OAuth2::Error => exception
  if NON_ERROR_CODES.include?(exception.response.status)
    raise exception
  else
    refresh_token!(exception)
    @raw_token.send(method_name, *args, &block)
  end
end

Instance Attribute Details

#raw_tokenObject (readonly)

Returns the value of attribute raw_token.



5
6
7
# File 'lib/access_token_wrapper/base.rb', line 5

def raw_token
  @raw_token
end

Instance Method Details

#refresh_token!(exception = nil) ⇒ Object



24
25
26
27
# File 'lib/access_token_wrapper/base.rb', line 24

def refresh_token!(exception = nil)
  @raw_token = @raw_token.refresh!
  @callback.call(@raw_token, exception)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/access_token_wrapper/base.rb', line 33

def respond_to_missing?(method_name, include_private = false)
  @raw_token.respond_to?(method_name, include_private) || super
end

#token_expiring?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/access_token_wrapper/base.rb', line 29

def token_expiring?
  @raw_token.expires_at < (Time.now.to_i + EXPIRY_GRACE_SEC)
end