lib/rack/cascade.rb in rack-1.0.1 vs lib/rack/cascade.rb in rack-1.1.0
- old
+ new
@@ -2,34 +2,39 @@
# 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).
class Cascade
+ NotFound = [404, {}, []]
+
attr_reader :apps
def initialize(apps, catch=404)
- @apps = apps
- @catch = [*catch]
+ @apps = []; @has_app = {}
+ apps.each { |app| add app }
+
+ @catch = {}
+ [*catch].each { |status| @catch[status] = true }
end
def call(env)
- status = headers = body = nil
- raise ArgumentError, "empty cascade" if @apps.empty?
- @apps.each { |app|
- begin
- status, headers, body = app.call(env)
- break unless @catch.include?(status.to_i)
- end
- }
- [status, headers, body]
+ result = NotFound
+
+ @apps.each do |app|
+ result = app.call(env)
+ break unless @catch.include?(result[0].to_i)
+ end
+
+ result
end
def add app
+ @has_app[app] = true
@apps << app
end
def include? app
- @apps.include? app
+ @has_app.include? app
end
alias_method :<<, :add
end
end