Class: Rack::Cascade

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/cascade.rb

Overview

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).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apps, catch = 404) ⇒ Cascade

Returns a new instance of Cascade.



9
10
11
12
# File 'lib/rack/cascade.rb', line 9

def initialize(apps, catch=404)
  @apps = apps
  @catch = [*catch]
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



7
8
9
# File 'lib/rack/cascade.rb', line 7

def apps
  @apps
end

Instance Method Details

#add(app) ⇒ Object Also known as: <<



26
27
28
# File 'lib/rack/cascade.rb', line 26

def add app
  @apps << app
end

#call(env) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rack/cascade.rb', line 14

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

#include?(app) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rack/cascade.rb', line 30

def include? app
  @apps.include? app
end