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 inherited from Middleware

#app, #options

Attributes included from DependencyLoader

#load_error

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

#close

Methods included from DependencyLoader

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

Methods included from MiddlewareRegistry

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

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


43
44
45
46
# File 'lib/faraday/request/authorization.rb', line 43

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


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

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


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

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:


49
50
51
52
# File 'lib/faraday/request/authorization.rb', line 49

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