Class: Rack::Cascade

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/cascade.rb

Overview

Rack::Cascade tries an request on several apps, and returns the first response that is not 404 (or in a list of configurable status codes).

Constant Summary collapse

NotFound =
[404, {}, []]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apps, catch = 404) ⇒ Cascade

Returns a new instance of Cascade.


11
12
13
14
15
16
17
# File 'lib/rack/cascade.rb', line 11

def initialize(apps, catch=404)
  @apps = []; @has_app = {}
  apps.each { |app| add app }

  @catch = {}
  [*catch].each { |status| @catch[status] = true }
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps


9
10
11
# File 'lib/rack/cascade.rb', line 9

def apps
  @apps
end

Instance Method Details

#add(app) ⇒ Object Also known as: <<


30
31
32
33
# File 'lib/rack/cascade.rb', line 30

def add app
  @has_app[app] = true
  @apps << app
end

#call(env) ⇒ Object


19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/cascade.rb', line 19

def call(env)
  result = NotFound

  @apps.each do |app|
    result = app.call(env)
    break unless @catch.include?(result[0].to_i)
  end

  result
end

#include?(app) ⇒ Boolean

Returns:

  • (Boolean)

35
36
37
# File 'lib/rack/cascade.rb', line 35

def include? app
  @has_app.include? app
end