data HTTP::Router(routes: Array[HTTP::Route], fallback: HTTP::Handler)
include HTTP::Handler
Routes by method and path pattern (/users/{id}, /files/{path...}):
the most specific match wins; a path that only other methods have is
answered 405.
router = HTTP::Router.build do |r|
r.get("/users/{id}") { |req| HTTP::Response.text("user #{req.params["id"]}") }
r.get("/users/me") { |req| HTTP::Response.text("me") }
end
router.call(HTTP::Request.get("/users/7")).body
# => HTTP::ResponseBody::Text(text: "user 7")
router.call(HTTP::Request.get("/users/me")).body
# => HTTP::ResponseBody::Text(text: "me")
router.call(HTTP::Request.post("/users/7")).status # => 405
router.call(HTTP::Request.get("/posts")).status # => 404
<std:http>:1542
Class methods
def self.build(&: (HTTP::RouterBuilder) -> Void) -> HTTP::Router
A router of the routes the block adds to its RouterBuilder. Raises
ArgumentError for two routes of a method that match the same paths
with equal specificity, or a pattern not starting with /.
<std:http>:1553
Methods
def routes -> Array[HTTP::Route]
The routes, in the order they were added.
<std:http>:1544
def fallback -> HTTP::Handler
The handler of a path no route has (RouterBuilder#not_found).
<std:http>:1546
def to_h -> Hash[Symbol, Object]
The fields as a Hash, by name.
<std:http>:1548
def with(routes: Array[HTTP::Route] = …, fallback: HTTP::Handler = …) -> HTTP::Router
A copy with the fields given replaced.
<std:http>:1548
def call(req: HTTP::Request) -> HTTP::Response
The response of the most specific route for the request's method and
path, its parameters merged into params. HEAD is served by a GET
route; a path only other methods have is answered 405 with Allow; a
path with no route goes to the fallback (404).
<std:http>:1563