Property definitions

popcorn $ AppRoute :: defaultinit
# 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