Class: Kazoo::Router
- Inherits:
-
Object
- Object
- Kazoo::Router
- Defined in:
- lib/kazoo/router.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
- #error_handler(app) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #match(path, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
9 10 11 |
# File 'lib/kazoo/router.rb', line 9 def initialize @routes = [] end |
Class Method Details
.map(&blk) ⇒ Object
3 4 5 6 7 |
# File 'lib/kazoo/router.rb', line 3 def self.map(&blk) instance = new instance.instance_eval(&blk) instance end |
Instance Method Details
#call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/kazoo/router.rb', line 26 def call(env) @routes.each do |route| if matches = route[0].match(env['REQUEST_PATH']) env['HTTP_PREFIX'] = matches[0] env['PATH_INFO'] = env['PATH_INFO'].sub(route[0], '') response = route[1].call(env) return response if response[1]['X-Cascade'] != 'pass' end end # If no routes found default_response = [404, {'Content-Type' => 'text/plain', "X-Cascade" => "pass"}, 'The requested URI is not found'] if @error_handler env['error'] = 'not_found' env['default_response'] = default_response @error_handler.call(env) else default_response end end |
#error_handler(app) ⇒ Object
22 23 24 |
# File 'lib/kazoo/router.rb', line 22 def error_handler(app) @error_handler = app end |
#match(path, options = {}) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/kazoo/router.rb', line 13 def match(path, = {}) raise "You must match a route to an app using :to" unless [:to] && [:to].respond_to?(:call) if path.is_a?(Regexp) @routes << [ path, [:to] ] elsif path.is_a?(String) @routes << [ Regexp.new("^#{path}"), [:to] ] end end |