class RackStep::Router

Attributes

routes[RW]

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

Public Class Methods

new() click to toggle source
# File lib/router.rb, line 18
def initialize
  @routes = Hash.new
end

Public Instance Methods

add_route(verb, path, controller) click to toggle source
# 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) click to toggle source

Adds new routes to the application, one for each possible http verb (GET, POST, DELETE and PUT).

# 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) click to toggle source

Given a request, will parse it’s path to find what it the apropriate controller to respond it.

# 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