Class: Arturo::Middleware

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

Overview

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'] ]`.

Constant Summary collapse

MISSING_FEATURE_ERROR =
"Cannot create an Arturo::Middleware without a :feature"
DEFAULT_RECIPIENT_KEY =
'arturo.recipient'
DEFAULT_ON_UNAVAILABLE =
lambda { |env| [ 404, {}, ['Not Found'] ] }

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
# File 'lib/arturo/middleware.rb', line 29

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

Instance Method Details

#call(env) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/arturo/middleware.rb', line 37

def call(env)
  if enabled_for_recipient?(env)
    @app.call(env)
  else
    fail(env)
  end
end