Skip to content
Newer
Older
100644 42 lines (31 sloc) 771 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
0ab6b6c @jeremy Allow empty cascades. Reduce #call object allocation overhead. Speed …
jeremy authored
7 NotFound = [404, {}, []]
8
f0f0840 @chneukirchen Add Rack::Cascade, to pass on the first non 404 result
chneukirchen authored
9 attr_reader :apps
10
11 def initialize(apps, catch=404)
0ab6b6c @jeremy Allow empty cascades. Reduce #call object allocation overhead. Speed …
jeremy authored
12 @apps = []; @has_app = {}
13 apps.each { |app| add app }
14
15 @catch = {}
16 [*catch].each { |status| @catch[status] = true }
f0f0840 @chneukirchen Add Rack::Cascade, to pass on the first non 404 result
chneukirchen authored
17 end
18
19 def call(env)
0ab6b6c @jeremy Allow empty cascades. Reduce #call object allocation overhead. Speed …
jeremy authored
20 result = NotFound
21
22 @apps.each do |app|
23 result = app.call(env)
24 break unless @catch.include?(result[0].to_i)
25 end
26
27 result
f0f0840 @chneukirchen Add Rack::Cascade, to pass on the first non 404 result
chneukirchen authored
28 end
8dace60 @scytrin Addition of #add, #<<, and #include? to Cascade, allowing iterative a…
scytrin authored
29
30 def add app
0ab6b6c @jeremy Allow empty cascades. Reduce #call object allocation overhead. Speed …
jeremy authored
31 @has_app[app] = true
8dace60 @scytrin Addition of #add, #<<, and #include? to Cascade, allowing iterative a…
scytrin authored
32 @apps << app
33 end
34
35 def include? app
0ab6b6c @jeremy Allow empty cascades. Reduce #call object allocation overhead. Speed …
jeremy authored
36 @has_app.include? app
8dace60 @scytrin Addition of #add, #<<, and #include? to Cascade, allowing iterative a…
scytrin authored
37 end
38
39 alias_method :<<, :add
f0f0840 @chneukirchen Add Rack::Cascade, to pass on the first non 404 result
chneukirchen authored
40 end
41 end
Something went wrong with that request. Please try again.