Class: Mpp::Server::ComposedHandler

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mpp/server/compose.rb

Overview

Combines multiple method offers into a single callable that presents every method via multiple WWW-Authenticate headers (and x402 extras).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler:, offers:) ⇒ ComposedHandler

Returns a new instance of ComposedHandler.



130
131
132
133
134
135
# File 'lib/mpp/server/compose.rb', line 130

def initialize(handler:, offers:)
  Kernel.raise ArgumentError, "compose() requires at least one entry" if offers.empty?

  @handler = T.let(handler, T.untyped)
  @offers = T.let(offers, T::Array[ComposeOffer])
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



124
125
126
# File 'lib/mpp/server/compose.rb', line 124

def handler
  @handler
end

#offersObject (readonly)

Returns the value of attribute offers.



127
128
129
# File 'lib/mpp/server/compose.rb', line 127

def offers
  @offers
end

Class Method Details

.compose(handlers) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mpp/server/compose.rb', line 144

def self.compose(handlers)
  Kernel.raise ArgumentError, "compose() requires at least one handler" if handlers.empty?

  offers = handlers.flat_map do |value|
    if value.is_a?(ComposedHandler)
      value.offers
    elsif value.respond_to?(:offers)
      value.offers
    else
      Kernel.raise ArgumentError, "compose() expected a ComposedHandler, got #{value.class}"
    end
  end
  new(handler: handlers.first.handler, offers: offers)
end

.from_entries(handler, entries) ⇒ Object



138
139
140
# File 'lib/mpp/server/compose.rb', line 138

def self.from_entries(handler, entries)
  new(handler: handler, offers: entries.map { |entry| offer_from_entry(handler, entry) })
end

Instance Method Details

#call(authorization: nil, payment_signature: nil, body: nil, url: nil, accept_payment: nil, http_method: nil, scope: nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mpp/server/compose.rb', line 170

def call(authorization: nil, payment_signature: nil, body: nil, url: nil, accept_payment: nil, http_method: nil, scope: nil)
  if scope && !scope.empty?
    return self.class.new(handler: @handler, offers: @offers.map { |offer| offer.with_scope(scope) }).call(
      authorization: authorization,
      payment_signature: payment_signature,
      body: body,
      url: url,
      accept_payment: accept_payment,
      http_method: http_method
    )
  end

  dispatched = dispatch_credential(
    authorization: authorization,
    payment_signature: payment_signature,
    body: body,
    url: url,
    http_method: http_method
  )
  return dispatched if dispatched

  merge_challenges(body: body, url: url, accept_payment: accept_payment, http_method: http_method)
end