Class: ZeroClick::Sellers::Middleware::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/zeroclick/sellers/middleware.rb

Overview

Shared wire-up. Subclasses decide what guarding means.

Direct Known Subclasses

Identify, Meter

Instance Method Summary collapse

Constructor Details

#initialize(app, service_slug:, seller: nil, max_body_bytes: DEFAULT_MAX_BODY_BYTES) ⇒ Base

seller may be a client, a callable returning one, or omitted.

Omitting it is the Rails path, and the reason it is optional at all: config.middleware.use evaluates its arguments in the Application class body, which runs BEFORE the Railtie initializer that reads config.zeroclick. Passing seller: ZeroClick::Sellers.seller there raises on boot, every time. Omit it and the process-wide client is resolved on the first request instead, by which point configuration exists.



123
124
125
126
127
128
# File 'lib/zeroclick/sellers/middleware.rb', line 123

def initialize(app, service_slug:, seller: nil, max_body_bytes: DEFAULT_MAX_BODY_BYTES)
  @app = app
  @seller_source = seller
  @service_slug = service_slug
  @max_body_bytes = max_body_bytes
end

Instance Method Details

#call(env) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/zeroclick/sellers/middleware.rb', line 130

def call(env)
  request, too_large = Middleware.request_from_env(env, max_body_bytes: @max_body_bytes)
  # Before verification on purpose: an unauthenticated caller must not be
  # able to make us buffer more than the ceiling.
  return to_rack(Response.json({ "error" => "payload_too_large" }, status: 413)) if too_large

  decision = decide(request)
  return to_rack(decision.response) unless decision.allow?

  env[CONTEXT_ENV_KEY] = decision.context
  respond(env, decision)
end