Skip to content
Newer
Older
100644 37 lines (30 sloc) 741 Bytes
f0f0840 @chneukirchen Add Rack::Cascade, to pass on the first non 404 result
chneukirchen authored
1 module Rack
376fa1e @chneukirchen Add RDocs
chneukirchen authored
2 # Rack::Cascade tries an request on several apps, and returns the
3 # first response that is not 404 (or in a list of configurable
4 # status codes).
5
f0f0840 @chneukirchen Add Rack::Cascade, to pass on the first non 404 result
chneukirchen authored
6 class Cascade
7 attr_reader :apps
8
9 def initialize(apps, catch=404)
10 @apps = apps
11 @catch = [*catch]
12 end
13
14 def call(env)
15 status = headers = body = nil
16 raise ArgumentError, "empty cascade" if @apps.empty?
17 @apps.each { |app|
18 begin
19 status, headers, body = app.call(env)
20 break unless @catch.include?(status.to_i)
21 end
22 }
23 [status, headers, body]
24 end
8dace60 @scytrin Addition of #add, #<<, and #include? to Cascade, allowing iterative a…
scytrin authored
25
26 def add app
27 @apps << app
28 end
29
30 def include? app
31 @apps.include? app
32 end
33
34 alias_method :<<, :add
f0f0840 @chneukirchen Add Rack::Cascade, to pass on the first non 404 result
chneukirchen authored
35 end
36 end
Something went wrong with that request. Please try again.