Class: PetstoreApiClient::Authentication::Composite
- Defined in:
- lib/petstore_api_client/authentication/composite.rb
Overview
Composite authentication strategy for applying multiple auth methods simultaneously
This authenticator implements the Composite pattern, allowing multiple authentication strategies to be applied to a single request. This is particularly useful during migration periods when transitioning from one auth method to another, or when an API accepts multiple authentication methods.
The Petstore API accepts both API Key and OAuth2 authentication. Using this composite authenticator, you can send both headers simultaneously, allowing the server to accept either authentication method.
All configured strategies are applied in the order they were added. Each strategy’s apply() method is called, allowing it to add its own headers to the request.
Instance Attribute Summary collapse
- #strategies ⇒ Object readonly
Instance Method Summary collapse
-
#apply(env) ⇒ void
Apply all configured authentication strategies to the request.
-
#configured? ⇒ Boolean
Check if any authentication strategy is configured.
-
#configured_types ⇒ Array<String>
Get list of configured strategy types.
-
#initialize(strategies = []) ⇒ Composite
constructor
Initialize composite authenticator with multiple strategies.
-
#inspect ⇒ String
(also: #to_s)
String representation showing all configured strategies.
Methods inherited from Base
Constructor Details
#initialize(strategies = []) ⇒ Composite
Initialize composite authenticator with multiple strategies
rubocop:disable Lint/MissingSuper
58 59 60 61 62 63 64 65 |
# File 'lib/petstore_api_client/authentication/composite.rb', line 58 def initialize(strategies = []) raise ArgumentError, "strategies must be an Array (got #{strategies.class})" unless strategies.is_a?(Array) validate_strategies!(strategies) @strategies = strategies # Performance optimization: cache configured strategies to avoid repeated checks @configured_strategies = @strategies.select(&:configured?) end |
Instance Attribute Details
#strategies ⇒ Object (readonly)
42 43 44 |
# File 'lib/petstore_api_client/authentication/composite.rb', line 42 def strategies @strategies end |
Instance Method Details
#apply(env) ⇒ void
This method returns an undefined value.
Apply all configured authentication strategies to the request
Iterates through all strategies and calls apply() on each one if it’s configured. This allows multiple authentication headers to be added to the same request.
81 82 83 84 85 |
# File 'lib/petstore_api_client/authentication/composite.rb', line 81 def apply(env) # Performance optimization: use cached configured strategies # Avoids checking configured? on every request @configured_strategies.each { |strategy| strategy.apply(env) } end |
#configured? ⇒ Boolean
Check if any authentication strategy is configured
Returns true if at least one strategy in the composite is configured. Returns false if no strategies are configured or if strategies array is empty.
109 110 111 112 |
# File 'lib/petstore_api_client/authentication/composite.rb', line 109 def configured? # Performance optimization: use cached configured strategies !@configured_strategies.empty? end |
#configured_types ⇒ Array<String>
Get list of configured strategy types
Returns array of type names for strategies that are actually configured. Useful for debugging and logging.
127 128 129 |
# File 'lib/petstore_api_client/authentication/composite.rb', line 127 def configured_types @strategies.select(&:configured?).map(&:type) end |
#inspect ⇒ String Also known as: to_s
String representation showing all configured strategies
140 141 142 143 144 145 146 |
# File 'lib/petstore_api_client/authentication/composite.rb', line 140 def inspect all_types = @strategies.map(&:type).join(", ") configured = configured_types.join(", ") configured = "none" if configured.empty? "#<#{self.class.name} strategies=[#{all_types}] configured=[#{configured}]>" end |