Class: Shipeasy::SDK::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/shipeasy/sdk/rack_middleware.rb

Overview

Rack middleware that mints the shared ‘__se_anon_id` bucketing cookie.

For every request without a valid ‘__se_anon_id` cookie it mints a UUIDv4, exposes it for the duration of the request, and Set-Cookies it on the response. Once installed, gate/experiment evaluations with no explicit user_id / anonymous_id automatically bucket on the cookie id — anonymous visitors get stable, SSR/browser-consistent bucketing with zero per-call wiring.

Rails apps get this automatically (a Railtie inserts it). For Sinatra / Hanami / bare Rack, add it yourself:

use Shipeasy::SDK::RackMiddleware

The resolved id is also stored in the Rack env under “shipeasy.anon_id” for callers that prefer to read it explicitly.

Constant Summary collapse

ENV_KEY =
"shipeasy.anon_id".freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



24
25
26
# File 'lib/shipeasy/sdk/rack_middleware.rb', line 24

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shipeasy/sdk/rack_middleware.rb', line 28

def call(env)
  id, minted = read_or_mint(env)
  env[ENV_KEY] = id
  AnonId.current = id
  begin
    status, headers, body = @app.call(env)
  ensure
    # Don't leak the id onto the next request handled by this thread.
    AnonId.current = nil
  end
  set_cookie!(headers, id, env) if minted
  [status, headers, body]
end