Class: FaradayMiddleware::Auth

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/appbaseio/auth_middleware.rb

Overview

Request middleware that encodes the body as JSON.

Processes only requests with matching Content-type or those without a type. If a request doesn’t have a type but has a body, it sets the Content-type to JSON MIME-type.

Doesn’t try to encode bodies that already are in string form.

Constant Summary collapse

AUTH_HEADER =
'Authorization'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, token = nil, options = {}) ⇒ Auth

Returns a new instance of Auth.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
# File 'lib/appbaseio/auth_middleware.rb', line 12

def initialize(app, token = nil, options = {})
  super(app)
  options, token = token, nil if token.is_a? Hash
  @token = token && token.to_s
  @auth_header = options.fetch(:auth_header, AUTH_HEADER).to_s
  raise ArgumentError, ":auth_header can't be blank" if @auth_header.empty?
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
# File 'lib/appbaseio/auth_middleware.rb', line 20

def call(env)
  env[:request_headers][@auth_header] ||= @token
  @app.call env
end