cascade.rb 808 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module Rack
  2. # Rack::Cascade tries an request on several apps, and returns the
  3. # first response that is not 404 (or in a list of configurable
  4. # status codes).
  5. class Cascade
  6. NotFound = [404, {"Content-Type" => "text/plain"}, []]
  7. attr_reader :apps
  8. def initialize(apps, catch=[404, 405])
  9. @apps = []; @has_app = {}
  10. apps.each { |app| add app }
  11. @catch = {}
  12. [*catch].each { |status| @catch[status] = true }
  13. end
  14. def call(env)
  15. result = NotFound
  16. @apps.each do |app|
  17. result = app.call(env)
  18. break unless @catch.include?(result[0].to_i)
  19. end
  20. result
  21. end
  22. def add app
  23. @has_app[app] = true
  24. @apps << app
  25. end
  26. def include? app
  27. @has_app.include? app
  28. end
  29. alias_method :<<, :add
  30. end
  31. end