Class: RackStep::Router
Instance Attribute Summary collapse
-
#routes ⇒ Object
Will store all the possible routes.
Instance Method Summary collapse
- #add_route(verb, path, controller) ⇒ Object
-
#add_route_for_all_verbs(path, controller) ⇒ Object
Adds new routes to the application, one for each possible http verb (GET, POST, DELETE and PUT).
-
#find_route_for(path, verb) ⇒ Object
Given a request, will parse it’s path to find what it the apropriate controller to respond it.
-
#initialize ⇒ Router
constructor
A new instance of Router.
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
18 19 20 |
# File 'lib/router.rb', line 18 def initialize @routes = Hash.new end |
Instance Attribute Details
#routes ⇒ Object
Will store all the possible routes. By default it’s empty and should be populated by the application (RackStep will add a 404 route, the others should be added by the user).
16 17 18 |
# File 'lib/router.rb', line 16 def routes @routes end |
Instance Method Details
#add_route(verb, path, controller) ⇒ Object
22 23 24 25 26 |
# File 'lib/router.rb', line 22 def add_route(verb, path, controller) path = path[1..-1] if path[0] == '/' # Ignoring the first char if path starts with '/'. route = Route.new(verb, path, controller) routes[route.id] = route end |
#add_route_for_all_verbs(path, controller) ⇒ Object
Adds new routes to the application, one for each possible http verb (GET, POST, DELETE and PUT).
46 47 48 49 50 51 |
# File 'lib/router.rb', line 46 def add_route_for_all_verbs(path, controller) add_route('GET', path, controller) add_route('POST', path, controller) add_route('DELETE', path, controller) add_route('PUT', path, controller) end |
#find_route_for(path, verb) ⇒ Object
Given a request, will parse it’s path to find what it the apropriate controller to respond it.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/router.rb', line 30 def find_route_for(path, verb) # Ignoring the first char if path starts with '/'. This way the path of # 'http//localhost/' will be the same of 'http://localhost' (both will # be empty strings). path = path[1..-1] if path[0] == '/' route_id = verb + path route = routes[route_id] # If no route was found, set it to 'notfound' route (maintaining the # original verb). route = routes["#{verb}notfound"] if route == nil return route end |