Class: Bandiera::Middleware
- Inherits:
-
Object
- Object
- Bandiera::Middleware
- Defined in:
- lib/bandiera/middleware.rb
Overview
Rack middleware for talking to a Bandiera feature flagging service.
The benefit of using this middleware is two-fold when working with a Bandiera server:
-
Performance - Using this middleware, you will only call the Bandiera server once per-request (in contrast to using the ‘enabled?` method where you will make one request per-use).
-
Better Quality Code - If you use the more advanced features in Bandiera (user groups and percentages) you will no longer need to pass around user objects and UUIDs in your code. This does assume the use of other middlewares to supply user objects and UUIDs though.
This middleware can be used in conjunction with the Macmillan::Utils::Middleware::Uuid (or one of your own design) to automatically generate UUIDs for your users. See github.com/springernature/bandiera/wiki for more information on this approach.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Middleware
constructor
Builds a new instance of Bandiera::Middleware.
- #process(env) ⇒ Object
Constructor Details
#initialize(app, opts = {}) ⇒ Middleware
Builds a new instance of Bandiera::Middleware
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bandiera/middleware.rb', line 31 def initialize(app, opts = {}) @app = app @client = opts[:client] @groups = opts[:groups] || [] @uuid_env_key = opts[:uuid_env_key] || Macmillan::Utils::Middleware::Uuid.env_key @user_env_key = opts[:user_env_key] || 'current_user' @user_group_method = opts[:user_group_method] || :email fail ArgumentError, 'You must supply a Bandiera::Client' unless @client end |
Instance Method Details
#call(env) ⇒ Object
42 43 44 |
# File 'lib/bandiera/middleware.rb', line 42 def call(env) dup.process(env) end |
#process(env) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bandiera/middleware.rb', line 46 def process(env) request = Rack::Request.new(env) user = request.env[@user_env_key] user_group = user ? user.public_send(@user_group_method) : nil uuid = request.env[@uuid_env_key] if @groups.empty? @client.get_all(user_group: user_group, user_id: uuid).each do |group, flags| request.env["bandiera.#{group}"] = flags end else @groups.each do |group| request.env["bandiera.#{group}"] = @client.get_features_for_group(group, user_group: user_group, user_id: uuid) end end @app.call(request.env) end |