Class: FaradayMiddleware::OAuth2Refresh
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- FaradayMiddleware::OAuth2Refresh
- Extended by:
- Forwardable
- Defined in:
- lib/faraday_middleware/oauth2_refresh.rb
Constant Summary collapse
- AUTH_HEADER =
'Authorization'.freeze
Instance Attribute Summary collapse
-
#oauth2_token ⇒ Object
readonly
Returns the value of attribute oauth2_token.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #get_api_key ⇒ Object
-
#initialize(app = nil, token = nil) ⇒ OAuth2Refresh
constructor
A new instance of OAuth2Refresh.
Constructor Details
#initialize(app = nil, token = nil) ⇒ OAuth2Refresh
Returns a new instance of OAuth2Refresh.
30 31 32 33 |
# File 'lib/faraday_middleware/oauth2_refresh.rb', line 30 def initialize(app = nil, token = nil) super app @oauth2_token = token end |
Instance Attribute Details
#oauth2_token ⇒ Object (readonly)
Returns the value of attribute oauth2_token.
9 10 11 |
# File 'lib/faraday_middleware/oauth2_refresh.rb', line 9 def oauth2_token @oauth2_token end |
Instance Method Details
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# 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 ⇒ Object
25 26 27 28 |
# 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 |