chneukirchen / rack-mirror

OUTDATED mirror of Rack's darcs repository, use github.com/chneukirchen/rack

This URL has Read+Write access

chneukirchen (author)
Sat May 24 08:54:49 -0700 2008
commit  5a117d0b45b4ed2785a714eaa060e8c66512c4c3
tree    0942c84f57f557d899fa767e06ca37a196563185
parent  ed86dfd676dcb46e276b6906f017df514e5632f9 parent  ebdd511d332d2b6cd33bda7bb4777518cfc89fa4
rack-mirror / lib / rack / cascade.rb
100644 37 lines (30 sloc) 0.741 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Rack
  # 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
    attr_reader :apps
 
    def initialize(apps, catch=404)
      @apps = apps
      @catch = [*catch]
    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]
    end
 
    def add app
      @apps << app
    end
 
    def include? app
      @apps.include? app
    end
 
    alias_method :<<, :add
  end
end