Class: Chespirito::Router
- Inherits:
-
Object
- Object
- Chespirito::Router
- Defined in:
- lib/chespirito/routes/router.rb
Instance Method Summary collapse
- #constraint_route(request) ⇒ Object
- #dispatch(request) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #not_found_response ⇒ Object
- #not_found_route ⇒ Object
- #register_route(*attrs) ⇒ Object
- #register_system_route(*attrs) ⇒ Object
- #route_for(request) ⇒ Object
- #simple_route(request) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
9 10 11 |
# File 'lib/chespirito/routes/router.rb', line 9 def initialize @routes = {} end |
Instance Method Details
#constraint_route(request) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/chespirito/routes/router.rb', line 42 def constraint_route(request) constraint_checker = RouteConstraintChecker.new(request) route = @routes.values.find(&constraint_checker.method(:match_route?)) return unless route route.tap do constraint_checker .extract_params(route) .each { |(name, value)| request.add_param!(name, value) } end end |
#dispatch(request) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/chespirito/routes/router.rb', line 25 def dispatch(request) route = route_for(request) return not_found_response unless route route .controller_klass .process(route.action, request) end |
#not_found_response ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/chespirito/routes/router.rb', line 57 def not_found_response ::Chespirito::Response.new.tap do |response| response.status = 404 response.headers = {} response.body = '' end end |
#not_found_route ⇒ Object
55 |
# File 'lib/chespirito/routes/router.rb', line 55 def not_found_route = @routes['404'] || @routes[:not_found] |
#register_route(*attrs) ⇒ Object
13 14 15 16 17 |
# File 'lib/chespirito/routes/router.rb', line 13 def register_route(*attrs) route = Route.new(*attrs) @routes[route.key] = route end |
#register_system_route(*attrs) ⇒ Object
19 20 21 22 23 |
# File 'lib/chespirito/routes/router.rb', line 19 def register_system_route(*attrs) route = SystemRoute.new(*attrs) @routes[route.key] = route end |
#route_for(request) ⇒ Object
34 35 36 |
# File 'lib/chespirito/routes/router.rb', line 34 def route_for(request) simple_route(request) || constraint_route(request) || not_found_route end |
#simple_route(request) ⇒ Object
38 39 40 |
# File 'lib/chespirito/routes/router.rb', line 38 def simple_route(request) @routes["#{request.verb} #{request.path}"] end |