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

NotFound =
[404, {"Content-Type" => "text/plain"}, []]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Cascade) initialize(apps, catch = [404, 405])

A new instance of Cascade



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

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

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

Instance Attribute Details

- (Object) apps (readonly)

Returns the value of attribute apps



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

def apps
  @apps
end

Instance Method Details

- (Object) add(app) Also known as: <<



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

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

- (Object) call(env)



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

- (Boolean) include?(app)

Returns:

  • (Boolean)


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

def include? app
  @has_app.include? app
end