Class: Kirei::Routing::Router
- Inherits:
-
Object
- Object
- Kirei::Routing::Router
- Extended by:
- T::Sig
- Includes:
- Singleton
- Defined in:
- lib/kirei/routing/router.rb
Overview
Usage:
Router.add_routes([
Route.new(
verb: Verb::GET,
path: "/livez",
controller: Controllers::HealthController,
action: "livez",
),
])
Constant Summary collapse
- RoutesHash =
T.type_alias do T::Hash[String, Route] end
- ResolveResult =
T.type_alias do T.nilable([Route, T::Hash[String, String]]) end
Instance Attribute Summary collapse
-
#current_env ⇒ Object
Returns the value of attribute current_env.
-
#dynamic_routes ⇒ Object
readonly
Returns the value of attribute dynamic_routes.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
Instance Method Summary collapse
- #get(verb, path) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #resolve(verb, path) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
38 39 40 41 |
# File 'lib/kirei/routing/router.rb', line 38 def initialize @routes = T.let({}, RoutesHash) @dynamic_routes = T.let([], T::Array[Route]) end |
Instance Attribute Details
#current_env ⇒ Object
Returns the value of attribute current_env.
35 36 37 |
# File 'lib/kirei/routing/router.rb', line 35 def current_env @current_env end |
#dynamic_routes ⇒ Object (readonly)
Returns the value of attribute dynamic_routes.
47 48 49 |
# File 'lib/kirei/routing/router.rb', line 47 def dynamic_routes @dynamic_routes end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
44 45 46 |
# File 'lib/kirei/routing/router.rb', line 44 def routes @routes end |
Class Method Details
.add_health_routes! ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/kirei/routing/router.rb', line 90 def self.add_health_routes! add_routes( [ Route.new(verb: Verb::GET, path: "/livez", controller: Kirei::Controllers::Health, action: "livez"), Route.new(verb: Verb::GET, path: "/readyz", controller: Kirei::Controllers::Health, action: "readyz"), Route.new(verb: Verb::GET, path: "/healthz", controller: Kirei::Controllers::Health, action: "healthz"), ], ) end |
.add_routes(routes) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kirei/routing/router.rb', line 77 def self.add_routes(routes) routes.each do |route| if route.dynamic? instance.dynamic_routes << route else key = "#{route.verb.serialize} #{route.path}" instance.routes[key] = route end end end |
Instance Method Details
#get(verb, path) ⇒ Object
56 57 58 59 |
# File 'lib/kirei/routing/router.rb', line 56 def get(verb, path) key = "#{verb.serialize} #{path}" routes[key] end |
#resolve(verb, path) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/kirei/routing/router.rb', line 69 def resolve(verb, path) static_route = get(verb, path) return [static_route, {}] unless static_route.nil? match_dynamic(verb, path) end |