class AccessTokenWrapper::Base

Constants

EXPIRY_GRACE_SEC

Attributes

raw_token[R]

Public Class Methods

new(raw_token, &callback) click to toggle source

This is the core functionality

@example

AccessTokenWrapper::Base.new(token) do |new_token, exception|
  update_user_from_access_token(new_token)
end

@param [<OAuth2::AccessToken] raw_token An instance of an OAuth2::AccessToken object @param [&block] callback A callback that gets called when a token is refreshed,

the callback is provided `new_token` and optional `exception` parameters

@return <AccessTokenWrapper::Base>

@api public

# File lib/access_token_wrapper/base.rb, line 27
def initialize(raw_token, &callback)
  @raw_token = raw_token
  @callback  = callback
end

Public Instance Methods

config() click to toggle source
# File lib/access_token_wrapper/base.rb, line 7
def config
  AccessTokenWrapper.configuration
end

Private Instance Methods

method_missing(method_name, *args, &block) click to toggle source
# File lib/access_token_wrapper/base.rb, line 34
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_refreshable_exception?(exception)
    raise
  else
    refresh_token!(exception)
    @raw_token.send(method_name, *args, &block)
  end
end
non_refreshable_exception?(exception) click to toggle source
# File lib/access_token_wrapper/base.rb, line 46
def non_refreshable_exception?(exception)
  if config.skip_statuses.include?(exception.response.status)
    true
  else
    config.skip_refresh_block.call(exception.response)
  end
end
refresh_token!(exception = nil) click to toggle source
# File lib/access_token_wrapper/base.rb, line 54
def refresh_token!(exception = nil)
  @raw_token = @raw_token.refresh!
  @callback.call(@raw_token, exception)
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/access_token_wrapper/base.rb, line 63
def respond_to_missing?(method_name, include_private = false)
  @raw_token.respond_to?(method_name, include_private) || super
end
token_expiring?() click to toggle source
# File lib/access_token_wrapper/base.rb, line 59
def token_expiring?
  @raw_token.expires_at && @raw_token.expires_at < (Time.now.to_i + EXPIRY_GRACE_SEC)
end