class FaradayMiddleware::OAuth2Refresh

Constants

AUTH_HEADER

Attributes

oauth2_token[R]

Public Class Methods

new(app = nil, token = nil) click to toggle source
Calls superclass method
# File lib/faraday_middleware/oauth2_refresh.rb, line 30
def initialize(app = nil, token = nil)
  super app
  @oauth2_token = token
end

Public Instance Methods

call(env) click to toggle source
# File lib/faraday_middleware/oauth2_refresh.rb, line 13
def call(env)
  if @oauth2_token.expired?
    @oauth2_token = @oauth2_token.refresh!({ headers: { 'Authorization' => 'Basic ' + get_api_key() } })
  end
  
  unless @oauth2_token.token.to_s.empty?
    env[:request_headers][AUTH_HEADER] = %(Bearer #{@oauth2_token.token})
  end

  @app.call env
end
get_api_key() click to toggle source
# File lib/faraday_middleware/oauth2_refresh.rb, line 25
def get_api_key
  api_key = Base64.encode64("#{@oauth2_token.client.id}:#{@oauth2_token.client.secret}").gsub(/[\s]/,'')
  return api_key
end