class Builder
Builds an immutable route table.
Nested Classes and Modules
Definitions
def initialize
Initialize a route builder.
Implementation
def initialize
@routes = {}
end
def routes
A frozen snapshot of the configured routes.
Signature
-
returns
Hash The route table.
Implementation
def routes
@routes.transform_values do |handlers|
handlers.dup.freeze
end.freeze
end
def route(path, handler = nil, methods: nil, &block)
Add a route.
When methods is omitted, the handler accepts every HTTP method. Otherwise,
it may be a single method or an array of methods.
Signature
-
parameter
pathString The absolute path to match.
-
parameter
handlerInterface(:call) | Nil A callable route handler.
-
parameter
methodsString | Symbol | Array(String | Symbol) | Nil The accepted HTTP methods.
-
yields
{|request| ...} The route handler.
-
parameter
requestProtocol::HTTP::Request The original request.
-
parameter
-
returns
Builder The builder.
Implementation
def route(path, handler = nil, methods: nil, &block)
raise ArgumentError, "Provide a route handler or block, not both!" if handler && block
handler ||= block
raise ArgumentError, "A route handler is required!" unless handler
path = route_path(path)
methods = route_methods(methods)
handlers = (@routes[path] ||= {})
methods.each do |method|
if handlers.key?(method)
raise ArgumentError, "Route already defined for #{method || "any method"} #{path}!"
end
handlers[method] = handler
end
return self
end