Class: Psc::Faraday::PscToken

Inherits:
Object
  • Object
show all
Defined in:
lib/psc/faraday/psc_token.rb

Overview

Faraday middleware that implements the psc_token authentication type. Tokens may either be static or computed per request.

Instance Method Summary collapse

Constructor Details

#initialize(app, token_or_creator) ⇒ PscToken

Create a new instance of the middleware.

Parameters:

  • app (#call)
  • token_or_creator (#call, String)

    if the value for this parameter responds to call, it will be called to create a token on each request. Otherwise it will be used as a static token value.



18
19
20
21
22
23
24
25
# File 'lib/psc/faraday/psc_token.rb', line 18

def initialize(app, token_or_creator)
  @app = app
  if token_or_creator.respond_to?(:call)
    @token_creator = token_or_creator
  else
    @token_creator = lambda { token_or_creator }
  end
end

Instance Method Details

#call(env) ⇒ Object

Adds the Authorization header using the configured token creator.



29
30
31
32
33
# File 'lib/psc/faraday/psc_token.rb', line 29

def call(env)
  env[:request_headers]['Authorization'] = "psc_token #{token}"

  @app.call(env)
end