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 34
def self.build_hash(type, hash)
  comma = ', '
  values = []
  hash.each do |key, value|
    value = value.call if value.is_a?(Proc)
    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 16
def self.header(type, token)
  case token
  when String, Symbol, Proc
    token = token.call if token.is_a?(Proc)
    "#{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, param) click to toggle source

@param app [#call] @param type [String, Symbol] Type of Authorization @param param [String, Symbol, Hash, Proc] parameter to build the Authorization header.

This value can be a proc, in which case it will be invoked on each request.
Calls superclass method Faraday::Middleware::new
# File lib/faraday/request/authorization.rb, line 48
def initialize(app, type, param)
  @type = type
  @param = param
  super(app)
end

Public Instance Methods

on_request(env) click to toggle source

@param env [Faraday::Env]

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

  env.request_headers[KEY] = self.class.header(@type, @param)
end