File: cascade.rb

package info (click to toggle)
librack-ruby 1.1.0-4%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,196 kB
  • ctags: 1,101
  • sloc: ruby: 8,692; makefile: 9
file content (41 lines) | stat: -rw-r--r-- 771 bytes parent folder | download | duplicates (2)
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
38
39
40
41
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
    NotFound = [404, {}, []]

    attr_reader :apps

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

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

    def call(env)
      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
      @has_app.include? app
    end

    alias_method :<<, :add
  end
end