class RapidRunty::Router::Routes

This class registers all routes defined by the draw method.

Public Instance Methods

add(*args) click to toggle source
# File lib/rapid_runty/router/routes.rb, line 10
def add(*args)
  self << RapidRunty::Router::Route.new(*args)
end
draw(&block) click to toggle source

Provides familiar DSL to defining routes

Example:

DemoApplication.routes.draw do
  root "demo#index"
  get "/demo", to: "demo#index"
  get "/demo/new", to: "demo#new"
  get "/demo/:id", to: "demo#show"
  get "/demo/:id/edit", to: "demo#edit"
  post "/demo/", to: "demo#create"
  put "/demo/:id", to: "demo#update"
  patch "/demo/:id", to: "demo#update"
  delete "/demo/:id", to: "demo#destroy"
end
# File lib/rapid_runty/router/routes.rb, line 65
def draw(&block)
  instance_eval(&block)
end
find_matching_route(path, urls, routes) click to toggle source
# File lib/rapid_runty/router/routes.rb, line 40
def find_matching_route(path, urls, routes)
  url, placeholders, controller_action = RapidRunty::Router::Matcher.new.match(path, urls)
  return nil if url.nil?

  route = routes.detect { |router| router.path == url }.clone
  route.placeholders = placeholders
  route.options = controller_action
  route
end
find_route(verb, path) click to toggle source

Match incoming paths from env to existing routes

@param verb [String] HTTP verb extracted from Rack env @param path [String] the url path extracted from Rack env

@return A RapidRunty::Router::Route Instance:

Example:

RapidRunty::Router::Routes.find_route("GET", "/foo/4") => #<RapidRunty::Router::Route options={"controller"=>"foo", "action"=>"bar"}, path="/foo/:id", placeholders=[], verb=:get
# File lib/rapid_runty/router/routes.rb, line 24
def find_route(verb, path)
  return nil if empty?

  verb = verb.to_s.downcase.strip.to_sym
  routes = select { |route| route.verb == verb }

  urls = select_routes_by_verb(verb, routes)

  find_matching_route(path, urls, routes)
end
root(controller) click to toggle source
# File lib/rapid_runty/router/routes.rb, line 69
def root(controller)
  get '/', to: controller
end
select_routes_by_verb(verb, routes) click to toggle source
# File lib/rapid_runty/router/routes.rb, line 35
def select_routes_by_verb(verb, routes)
  urls = routes.map { |route| { url: route.path }.merge route.options }
  urls
end