aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/rack/cascade.rb
blob: d5af7fc77e73cc330ba81126a4c1f9208fe5650d (plain) (blame)
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
require 'active_support/ordered_hash'

module Rails
  module Rack
    # Try a request on several apps; return the first non-404 response.
    class Cascade
      attr_reader :apps

      def initialize(apps)
        @apps = ActiveSupport::OrderedHash.new
        apps.each { |app| add app }
      end

      def call(env)
        @apps.keys.each do |app|
          result = app.call(env)
          return result unless result[0].to_i == 404
        end
        Metal::NotFoundResponse
      end

      def add(app)
        @apps[app] = true
      end

      def include?(app)
        @apps.include?(app)
      end
    end
  end
end