A route to an Action according to a path

Introduced properties

init defaultinit(path: nullable String, handler: Action)

nitcorn :: Route :: defaultinit

fun handler: Action

nitcorn :: Route :: handler

Action to activate when this route is traveled
protected fun handler=(handler: Action)

nitcorn :: Route :: handler=

Action to activate when this route is traveled
fun match(uri: nullable String): Bool

nitcorn :: Route :: match

Does self matches uri?
fun parse_params(uri: nullable String): Map[String, String]

nitcorn :: Route :: parse_params

Extract parameter values from uri.
fun path: nullable String

nitcorn :: Route :: path

Path to this action present in the URI
protected fun path=(path: nullable String)

nitcorn :: Route :: path=

Path to this action present in the URI
fun resolve_path(request: HttpRequest): nullable String

nitcorn :: Route :: resolve_path

Replace self.path parameters with concrete values from the request URI.

Redefined properties

redef type SELF: Route

nitcorn $ Route :: SELF

Type of this instance, automatically specialized in every class
redef init init

nitcorn $ Route :: init

redef init init

nitcorn :: vararg_routes $ Route :: init

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init defaultinit(path: nullable String, handler: Action)

nitcorn :: Route :: defaultinit

fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun handler: Action

nitcorn :: Route :: handler

Action to activate when this route is traveled
protected fun handler=(handler: Action)

nitcorn :: Route :: handler=

Action to activate when this route is traveled
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun match(uri: nullable String): Bool

nitcorn :: Route :: match

Does self matches uri?
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun parse_params(uri: nullable String): Map[String, String]

nitcorn :: Route :: parse_params

Extract parameter values from uri.
fun path: nullable String

nitcorn :: Route :: path

Path to this action present in the URI
protected fun path=(path: nullable String)

nitcorn :: Route :: path=

Path to this action present in the URI
fun resolve_path(request: HttpRequest): nullable String

nitcorn :: Route :: resolve_path

Replace self.path parameters with concrete values from the request URI.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram nitcorn::Route Route core::Object Object nitcorn::Route->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

nitcorn $ Route
# A route to an `Action` according to a `path`
class Route
	# Path to this action present in the URI
	var path: nullable String

	init
	do
		var path = path
		if path != null then self.path = "/" / path
	end

	# `Action` to activate when this route is traveled
	var handler: Action
end
lib/nitcorn/server_config.nit:59,1--72,3

nitcorn :: vararg_routes $ Route
# A route to an `Action` according to a `path`
redef class Route

	redef init do
		super
		parse_pattern(path)
	end

	# Replace `self.path` parameters with concrete values from the `request` URI.
	fun resolve_path(request: HttpRequest): nullable String do
		if pattern_parts.is_empty then return self.path
		var path = "/"
		for part in pattern_parts do
			if part isa UriString then
				path /= part.string
			else if part isa UriParam then
				path /= request.param(part.name) or else part.name
			end
		end
		return path
	end

	# Cut `path` into `UriParts`.
	private fun parse_pattern(path: nullable String) do
		if path == null then return
		path = standardize_path(path)
		var parts = path.split("/")
		for part in parts do
			if not part.is_empty and part.first == ':' then
				# is an uri param
				var name = part.substring(1, part.length)
				var param = new UriParam(name)
				pattern_parts.add param
			else
				# is a standard string
				pattern_parts.add new UriString(part)
			end
		end
	end

	# `UriPart` forming `self` pattern.
	private var pattern_parts = new Array[UriPart]

	# Does `self` matches `uri`?
	fun match(uri: nullable String): Bool do
		if pattern_parts.is_empty then return true
		if uri == null then return false
		uri = standardize_path(uri)
		var parts = uri.split("/")
		for i in [0 .. pattern_parts.length[ do
			if i >= parts.length then return false
			var ppart = pattern_parts[i]
			var part = parts[i]
			if not ppart.match(part) then return false
		end
		return true
	end

	# Extract parameter values from `uri`.
	fun parse_params(uri: nullable String): Map[String, String] do
		var res = new HashMap[String, String]
		if pattern_parts.is_empty then return res
		if uri == null then return res
		uri = standardize_path(uri)
		var parts = uri.split("/")
		for i in [0 .. pattern_parts.length[ do
			if i >= parts.length then return res
			var ppart = pattern_parts[i]
			var part = parts[i]
			if not ppart.match(part) then return res
			if ppart isa UriParam then
				res[ppart.name] = part
			end
		end
		return res
	end

	# Remove first occurence of `/`.
	private fun standardize_path(path: String): String do
		if not path.is_empty and path.first == '/' then
			return path.substring(1, path.length)
		end
		return path
	end
end
lib/nitcorn/vararg_routes.nit:126,1--210,3