Class: FlipperTrail::Middleware

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

Overview

Rack middleware that resolves the current actor for the duration of a request and stores it on Current, so flag changes during that request are attributed to it.

Examples:

Insert into a Rails app

config.middleware.use FlipperTrail::Middleware, resolver: ->(env) { env["warden"].user }

Instance Method Summary collapse

Constructor Details

#initialize(app, resolver: nil) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • app (#call)

    the next Rack app in the stack

  • resolver (#call, nil) (defaults to: nil)

    resolves the request’s actor; may take the Rack env (called with it) or take no arguments (called zero-arity). When nil, falls back to the configured ‘actor_resolver`.



17
18
19
20
# File 'lib/flipper_trail/middleware.rb', line 17

def initialize(app, resolver: nil)
  @app = app
  @resolver = resolver
end

Instance Method Details

#call(env) ⇒ Array(Integer, Hash, #each)

Sets the resolved actor on Current for the wrapped request.

Parameters:

  • env (Hash)

    the Rack environment

Returns:

  • (Array(Integer, Hash, #each))

    the Rack response



26
27
28
# File 'lib/flipper_trail/middleware.rb', line 26

def call(env)
  Current.set(actor: resolve(env)) { @app.call(env) }
end