Class: PetstoreApiClient::Middleware::Authentication

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/petstore_api_client/middleware/authentication.rb

Overview

Faraday middleware for authentication Applies authentication strategy to each request

This middleware follows the Interceptor pattern - it intercepts outgoing requests and adds authentication headers before they’re sent

This is the same pattern used by industry-standard gems:

  • Octokit (GitHub API)

  • Slack-ruby-client

  • Stripe Ruby

Examples:

# In Faraday connection setup
conn.use PetstoreApiClient::Middleware::Authentication,
         authenticator: ApiKey.new("special-key")

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Authentication

Initialize middleware with authentication strategy

Parameters:

  • app (#call)

    The next middleware in the stack

  • options (Hash) (defaults to: {})

    Middleware options

Options Hash (options):



26
27
28
29
# File 'lib/petstore_api_client/middleware/authentication.rb', line 26

def initialize(app, options = {})
  super(app)
  @authenticator = options[:authenticator]
end

Instance Method Details

#call(env) ⇒ Faraday::Response

Process request - apply authentication before sending

Parameters:

  • env (Faraday::Env)

    The request environment

Returns:

  • (Faraday::Response)

    The response from the next middleware



35
36
37
38
39
40
41
# File 'lib/petstore_api_client/middleware/authentication.rb', line 35

def call(env)
  # Apply authentication if configured
  @authenticator&.apply(env) if @authenticator&.configured?

  # Continue to next middleware
  @app.call(env)
end