class Faraday::Request::Authorization

Request middleware for the Authorization HTTP header

Constants

KEY

Public Class Methods

new(app, type, *params) click to toggle source

@param app [#call] @param type [String, Symbol] Type of Authorization @param params [Array<String, Proc, call>] parameters to build the Authorization header.

If the type is `:basic`, then these can be a login and password pair.
Otherwise, a single value is expected that will be appended after the type.
This value can be a proc or an object responding to `.call`, in which case
it will be invoked on each request.
Calls superclass method Faraday::Middleware::new
# File lib/faraday/request/authorization.rb, line 16
def initialize(app, type, *params)
  @type = type
  @params = params
  super(app)
end

Public Instance Methods

on_request(env) click to toggle source

@param env [Faraday::Env]

# File lib/faraday/request/authorization.rb, line 23
def on_request(env)
  return if env.request_headers[KEY]

  env.request_headers[KEY] = header_from(@type, env, *@params)
end

Private Instance Methods

header_from(type, env, *params) click to toggle source

@param type [String, Symbol] @param env [Faraday::Env] @param params [Array] @return [String] a header value

# File lib/faraday/request/authorization.rb, line 35
def header_from(type, env, *params)
  if type.to_s.casecmp('basic').zero? && params.size == 2
    Utils.basic_header_from(*params)
  elsif params.size != 1
    raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
  else
    value = params.first
    if (value.is_a?(Proc) && value.arity == 1) || (value.respond_to?(:call) && value.method(:call).arity == 1)
      value = value.call(env)
    elsif value.is_a?(Proc) || value.respond_to?(:call)
      value = value.call
    end
    "#{type} #{value}"
  end
end