Default strict routes like /
or /user
match the same URI string.
An exception is done for the trailing /
, which is always omitted during the
parsing.
var route = new AppRoute("/")
assert route.match("")
assert route.match("/")
assert not route.match("/user")
assert not route.match("user")
route = new AppRoute("/user")
assert not route.match("/")
assert route.match("/user")
assert route.match("/user/")
assert not route.match("/user/10")
assert not route.match("/foo")
assert not route.match("user")
assert not route.match("/username")
popcorn :: AppRoute :: defaultinit
popcorn :: AppRoute :: resolve_path
Replace path parameters with concrete values from theuri
.
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
popcorn :: AppRoute :: defaultinit
core :: Object :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).popcorn :: AppRoute :: resolve_path
Replace path parameters with concrete values from theuri
.
# AppRoute provide services for path and uri manipulation and matching..
#
# Default strict routes like `/` or `/user` match the same URI string.
# An exception is done for the trailing `/`, which is always omitted during the
# parsing.
#
# ~~~
# var route = new AppRoute("/")
# assert route.match("")
# assert route.match("/")
# assert not route.match("/user")
# assert not route.match("user")
#
# route = new AppRoute("/user")
# assert not route.match("/")
# assert route.match("/user")
# assert route.match("/user/")
# assert not route.match("/user/10")
# assert not route.match("/foo")
# assert not route.match("user")
# assert not route.match("/username")
# ~~~
class AppRoute
# Route relative path from server root.
var path: String
# Does self match the `req`?
fun match(uri: String): Bool do
uri = uri.simplify_path
var path = resolve_path(uri)
if uri.is_empty and path == "/" then return true
return uri == path
end
# Replace path parameters with concrete values from the `uri`.
#
# For strict routes, it returns the path unchanged:
# ~~~
# var route = new AppRoute("/")
# assert route.resolve_path("/user/10/profile") == "/"
#
# route = new AppRoute("/user")
# assert route.resolve_path("/user/10/profile") == "/user"
# ~~~
fun resolve_path(uri: String): String do return path.simplify_path
# Remove `resolved_path` prefix from `uri`.
#
# Mainly used to resolve and match mountable routes.
#
# ~~~
# var route = new AppRoute("/")
# assert route.uri_root("/user/10/profile") == "/user/10/profile"
#
# route = new AppRoute("/user")
# assert route.uri_root("/user/10/profile") == "/10/profile"
# ~~~
fun uri_root(uri: String): String do
var path = resolve_path(uri)
if path == "/" then return uri
return uri.substring(path.length, uri.length).simplify_path
end
end
lib/popcorn/pop_routes.nit:22,1--85,3