added documentation for router
This commit is contained in:
parent
7578cb9adb
commit
839084aa90
@ -1,10 +1,36 @@
|
|||||||
module Zero
|
module Zero
|
||||||
|
# makes it possible to route urls to rack applications
|
||||||
|
#
|
||||||
|
# This class can be used to build a small rack application which routes
|
||||||
|
# requests to the given application.
|
||||||
|
# In the URLs it is also possible to use placeholders which then get assigned
|
||||||
|
# as variables to the parameters.
|
||||||
|
#
|
||||||
|
# @example of a simple router
|
||||||
|
# router = Zero::Router.new(
|
||||||
|
# '/' => WelcomeController,
|
||||||
|
# '/posts' => PostController
|
||||||
|
# )
|
||||||
|
#
|
||||||
|
# @example of a router with variables
|
||||||
|
# router = Zero::Router.new(
|
||||||
|
# '/foo/:id' => FooController
|
||||||
|
# )
|
||||||
class Router
|
class Router
|
||||||
# match for variables in routes
|
# match for variables in routes
|
||||||
VARIABLE_MATCH = %r{:(\w+)[^/]?}
|
VARIABLE_MATCH = %r{:(\w+)[^/]?}
|
||||||
# the replacement string to make it an regex
|
# the replacement string to make it an regex
|
||||||
VARIABLE_REGEX = '(?<\1>.+?)'
|
VARIABLE_REGEX = '(?<\1>.+?)'
|
||||||
|
|
||||||
|
# create a new router instance
|
||||||
|
#
|
||||||
|
# @example of a simple router
|
||||||
|
# router = Zero::Router.new(
|
||||||
|
# '/' => WelcomeController,
|
||||||
|
# '/posts' => PostController
|
||||||
|
# )
|
||||||
|
#
|
||||||
|
# @param routes [Hash] a map of URLs to rack compatible applications
|
||||||
def initialize(routes)
|
def initialize(routes)
|
||||||
@routes = {}
|
@routes = {}
|
||||||
routes.each do |route, target|
|
routes.each do |route, target|
|
||||||
@ -14,6 +40,12 @@ module Zero
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# call the router and call the matching application
|
||||||
|
#
|
||||||
|
# This method has to be called with a rack compatible environment, then the
|
||||||
|
# method will find a matching route and call the application.
|
||||||
|
# @param env [Hash] a rack environment
|
||||||
|
# @return [Array] a rack compatible response
|
||||||
def call(env)
|
def call(env)
|
||||||
request = Zero::Request.create(env)
|
request = Zero::Request.create(env)
|
||||||
@routes.each do |route, target|
|
@routes.each do |route, target|
|
||||||
|
Loading…
Reference in New Issue
Block a user