Class: Faraday::Request::Authorization

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

Overview

Request middleware for the Authorization HTTP header

Constant Summary collapse

KEY =
'Authorization'

Instance Attribute Summary

Attributes included from DependencyLoader

#load_error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MiddlewareRegistry

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

Methods included from DependencyLoader

#dependency, #inherited, #loaded?, #new

Constructor Details

#initialize(app, type, token) ⇒ Authorization

Returns a new instance of Authorization.

Parameters:

  • app (#call)
  • type (String, Symbol)

    Type of Authorization

  • token (String, Symbol, Hash)

    Token value for the Authorization



41
42
43
44
# File 'lib/faraday/request/authorization.rb', line 41

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

Class Method Details

.build_hash(type, hash) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns type followed by comma-separated key=value pairs.

Parameters:

  • type (String)
  • hash (Hash)

Returns:

  • (String)

    type followed by comma-separated key=value pairs



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

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

.header(type, token) ⇒ String

Returns a header value.

Parameters:

  • type (String, Symbol)
  • token (String, Symbol, Hash)

Returns:

  • (String)

    a header value



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

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

Parameters:



47
48
49
50
# File 'lib/faraday/request/authorization.rb', line 47

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