class Arturo::Middleware
A Rack middleware that requires a feature to be present. By default, checks feature availability against an ‘arturo.recipient` object in the `env`. If that object is missing, this middleware always fails, even if the feature is available for everyone.
## Usage
use Arturo::Middleware, :feature => :foo
## Options
* feature -- the name of the feature to require, as a Symbol; required * recipient -- the key in the `env` hash under which the feature recipient can be found; defaults to "arturo.recipient". * on_unavailable -- a Rack-like object (has `#call(Hash) -> [status, headers, body]`) that is called when the feature is unavailable; defaults to returning `[ 404, {}, ['Not Found'] ]`.
Constants
- DEFAULT_ON_UNAVAILABLE
- DEFAULT_RECIPIENT_KEY
- MISSING_FEATURE_ERROR
Public Class Methods
new(app, options = {})
click to toggle source
# File lib/arturo/middleware.rb, line 30 def initialize(app, options = {}) @app = app @feature = options[:feature] raise ArgumentError.new(MISSING_FEATURE_ERROR) unless @feature @recipient_key = options[:recipient] || DEFAULT_RECIPIENT_KEY @on_unavailable = options[:on_unavailable] || DEFAULT_ON_UNAVAILABLE end
Public Instance Methods
call(env)
click to toggle source
# File lib/arturo/middleware.rb, line 38 def call(env) if enabled_for_recipient?(env) @app.call(env) else fail(env) end end
Private Instance Methods
enabled_for_recipient?(env)
click to toggle source
# File lib/arturo/middleware.rb, line 48 def enabled_for_recipient?(env) ::Arturo.feature_enabled_for?(@feature, recipient(env)) end
fail(env)
click to toggle source
# File lib/arturo/middleware.rb, line 56 def fail(env) @on_unavailable.call(env) end
recipient(env)
click to toggle source
# File lib/arturo/middleware.rb, line 52 def recipient(env) env[@recipient_key] end