Class: ConsistentRandom::RackMiddleware

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

Overview

Rack middleware that wraps a request with consistent random scope so that you can generate consistent random values within a request.

Instance Method Summary collapse

Constructor Details

#initialize(app, seed_block = nil) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.

Parameters:

  • app (Object)

    Rack application to wrap

  • seed_block (Proc, #call, nil) (defaults to: nil)

    block to generate seed for the request If provided, the block will be called with the request env and the return value will be used as the seed for the request. You can use this to generate a seed based on the request state..



12
13
14
15
# File 'lib/consistent_random/rack_middleware.rb', line 12

def initialize(app, seed_block = nil)
  @app = app
  @seed_block = seed_block
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
# File 'lib/consistent_random/rack_middleware.rb', line 17

def call(env)
  seed = @seed_block.call(env) if @seed_block
  ConsistentRandom.scope(seed) do
    @app.call(env)
  end
end