Class: Faraday::Request::Authorization

Inherits:
Middleware show all
Defined in:
lib/faraday/request/authorization.rb

Direct Known Subclasses

BasicAuthentication, TokenAuthentication

Constant Summary collapse

KEY =
"Authorization".freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

dependency, inherited, loaded?, new

Methods included from MiddlewareRegistry

#lookup_middleware, #register_middleware

Constructor Details

#initialize(app, type, token) ⇒ Authorization

Returns a new instance of Authorization



26
27
28
29
# File 'lib/faraday/request/authorization.rb', line 26

def initialize(app, type, token)
  @header_value = self.class.header(type, token)
  super(app)
end

Class Method Details

.build_hash(type, hash) ⇒ Object

Internal



16
17
18
19
20
21
22
23
24
# File 'lib/faraday/request/authorization.rb', line 16

def self.build_hash(type, hash)
  offset = KEY.size + type.size + 3
  comma = ",\n#{' ' * offset}"
  values = []
  hash.each do |key, value|
    values << "#{key}=#{value.to_s.inspect}"
  end
  "#{type} #{values * comma}"
end

.header(type, token) ⇒ Object

Public



6
7
8
9
10
11
12
13
# File 'lib/faraday/request/authorization.rb', line 6

def self.header(type, token)
  case token
  when String, Symbol then "#{type} #{token}"
  when Hash then build_hash(type.to_s, token)
  else
    raise ArgumentError, "Can't build an Authorization #{type} header from #{token.inspect}"
  end
end

Instance Method Details

#call(env) ⇒ Object

Public



32
33
34
35
36
37
# File 'lib/faraday/request/authorization.rb', line 32

def call(env)
  unless env[:request_headers][KEY]
    env[:request_headers][KEY] = @header_value
  end
  @app.call(env)
end