class Faraday::Request::Authorization

Request middleware for the Authorization HTTP header

Constants

KEY

Public Class Methods

build_hash(type, hash) click to toggle source

@param type [String] @param hash [Hash] @return [String] type followed by comma-separated key=value pairs @api private

# 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) click to toggle source

@param type [String, Symbol] @param token [String, Symbol, Hash] @return [String] a header value

# 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
new(app, type, token) click to toggle source

@param app [#call] @param type [String, Symbol] Type of Authorization @param token [String, Symbol, Hash] Token value for the Authorization

Calls superclass method Faraday::Middleware::new
# File lib/faraday/request/authorization.rb, line 43
def initialize(app, type, token)
  @header_value = self.class.header(type, token)
  super(app)
end

Public Instance Methods

call(env) click to toggle source

@param env [Faraday::Env]

# 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