Class: Faraday::Request::Authorization

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

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

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware

Constructor Details

#initialize(app, type, token) ⇒ Authorization

Returns a new instance of Authorization


28
29
30
31
# File 'lib/faraday/request/authorization.rb', line 28

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

Class Method Details

.build_hash(type, hash) ⇒ Object

Internal


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

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
14
15
# File 'lib/faraday/request/authorization.rb', line 6

def self.header(type, token)
  case token
  when String, Symbol
    "#{type} #{token}"
  when Hash
    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


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

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