Class: Tina4::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = 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_handlerObject (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_requiredObject

Returns the value of attribute auth_required.



7
8
9
# File 'lib/tina4/router.rb', line 7

def auth_required
  @auth_required
end

#cachedObject

Returns the value of attribute cached.



7
8
9
# File 'lib/tina4/router.rb', line 7

def cached
  @cached
end

#handlerObject (readonly)

Returns the value of attribute handler.



5
6
7
# File 'lib/tina4/router.rb', line 5

def handler
  @handler
end

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/tina4/router.rb', line 5

def method
  @method
end

#param_namesObject (readonly)

Returns the value of attribute param_names.



5
6
7
# File 'lib/tina4/router.rb', line 5

def param_names
  @param_names
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/tina4/router.rb', line 5

def path
  @path
end

#path_regexObject (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_metaObject (readonly)

Returns the value of attribute swagger_meta.



5
6
7
# File 'lib/tina4/router.rb', line 5

def swagger_meta
  @swagger_meta
end

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'lib/tina4/router.rb', line 5

def template
  @template
end

Instance Method Details

#cacheObject

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

Returns:

  • (Boolean)


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_authObject

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

#secureObject

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