Class: Tina4::Route
- Inherits:
-
Object
- Object
- Tina4::Route
- Defined in:
- lib/tina4/router.rb
Instance Attribute Summary collapse
-
#auth_handler ⇒ Object
readonly
Returns the value of attribute auth_handler.
-
#auth_required ⇒ Object
Returns the value of attribute auth_required.
-
#cached ⇒ Object
Returns the value of attribute cached.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#param_names ⇒ Object
readonly
Returns the value of attribute param_names.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#path_regex ⇒ Object
readonly
Returns the value of attribute path_regex.
-
#swagger_meta ⇒ Object
readonly
Returns the value of attribute swagger_meta.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
-
#cache ⇒ Object
Mark this route as cacheable.
-
#initialize(method, path, handler, auth_handler: nil, swagger_meta: {}, middleware: [], template: nil) ⇒ Route
constructor
A new instance of Route.
-
#match?(request_path, request_method = nil) ⇒ Boolean
Returns params hash if matched, false otherwise.
-
#match_path(request_path) ⇒ Object
Returns params hash if matched, false otherwise.
-
#middleware(*middleware_classes) ⇒ Object
Dual-mode: getter (no args) returns the middleware array; setter (with args) appends middleware and returns self for chaining.
-
#no_auth ⇒ Object
Opt out of the secure-by-default auth on write routes.
-
#run_middleware(request, response) ⇒ Object
Run per-route middleware chain; returns true if all pass.
-
#secure ⇒ Object
Mark this route as requiring bearer-token authentication.
Constructor Details
#initialize(method, path, handler, auth_handler: nil, swagger_meta: {}, middleware: [], template: nil) ⇒ Route
Returns a new instance of Route.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tina4/router.rb', line 9 def initialize(method, path, handler, auth_handler: nil, swagger_meta: {}, middleware: [], template: nil) @method = method.to_s.upcase.freeze @path = normalize_path(path).freeze @handler = handler @auth_handler = auth_handler @swagger_meta = @middleware = middleware.freeze @template = template&.freeze # Write routes are secure by default, unless custom middleware is registered # (developer handles auth themselves via middleware) @auth_required = %w[POST PUT PATCH DELETE].include?(@method) && middleware.empty? @cached = false @param_names = [] @path_regex = compile_pattern(@path) @param_names.freeze end |
Instance Attribute Details
#auth_handler ⇒ Object (readonly)
Returns the value of attribute auth_handler.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def auth_handler @auth_handler end |
#auth_required ⇒ Object
Returns the value of attribute auth_required.
7 8 9 |
# File 'lib/tina4/router.rb', line 7 def auth_required @auth_required end |
#cached ⇒ Object
Returns the value of attribute cached.
7 8 9 |
# File 'lib/tina4/router.rb', line 7 def cached @cached end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def handler @handler end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def method @method end |
#param_names ⇒ Object (readonly)
Returns the value of attribute param_names.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def param_names @param_names end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def path @path end |
#path_regex ⇒ Object (readonly)
Returns the value of attribute path_regex.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def path_regex @path_regex end |
#swagger_meta ⇒ Object (readonly)
Returns the value of attribute swagger_meta.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def @swagger_meta end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def template @template end |
Instance Method Details
#cache ⇒ Object
Mark this route as cacheable. Returns self for chaining: Router.get(“/path”) { … }.cache
42 43 44 45 |
# File 'lib/tina4/router.rb', line 42 def cache @cached = true self end |
#match?(request_path, request_method = nil) ⇒ Boolean
Returns params hash if matched, false otherwise
61 62 63 64 |
# File 'lib/tina4/router.rb', line 61 def match?(request_path, request_method = nil) return false if request_method && @method != "ANY" && @method != request_method.to_s.upcase match_path(request_path) end |
#match_path(request_path) ⇒ Object
Returns params hash if matched, false otherwise
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tina4/router.rb', line 67 def match_path(request_path) match = @path_regex.match(request_path) return false unless match if @param_names.empty? {} else params = {} @param_names.each_with_index do |param_def, i| raw_value = match[i + 1] params[param_def[:name]] = cast_param(raw_value, param_def[:type]) end params end end |
#middleware(*middleware_classes) ⇒ Object
Dual-mode: getter (no args) returns the middleware array; setter (with args) appends middleware and returns self for chaining. Router.post(“/api”) { … }.middleware(AuthMiddleware)
50 51 52 53 54 55 56 57 58 |
# File 'lib/tina4/router.rb', line 50 def middleware(*middleware_classes) return @middleware if middleware_classes.empty? @middleware = @middleware.dup + middleware_classes # Custom middleware means developer handles auth — disable built-in gate # unless .secure was explicitly called. @auth_required = false unless @auth_required self end |
#no_auth ⇒ Object
Opt out of the secure-by-default auth on write routes. Returns self for chaining: Router.post(“/login”) { … }.no_auth
35 36 37 38 |
# File 'lib/tina4/router.rb', line 35 def no_auth @auth_required = false self end |
#run_middleware(request, response) ⇒ Object
Run per-route middleware chain; returns true if all pass
84 85 86 87 88 89 90 |
# File 'lib/tina4/router.rb', line 84 def run_middleware(request, response) @middleware.each do |mw| result = mw.call(request, response) return false if result == false end true end |
#secure ⇒ Object
Mark this route as requiring bearer-token authentication. Returns self for chaining: Router.get(“/path”) { … }.secure
28 29 30 31 |
# File 'lib/tina4/router.rb', line 28 def secure @auth_required = true self end |