Class: BugBunny::Routing::Route
- Inherits:
-
Object
- Object
- BugBunny::Routing::Route
- Defined in:
- lib/bug_bunny/routing/route.rb
Overview
Representa una ruta individual dentro del mapa de rutas de BugBunny.
Esta clase se encarga de compilar un patrón de URL (ej. ‘users/:id’) en una Expresión Regular capaz de evaluar coincidencias y extraer los parámetros dinámicos nombrados de forma automática.
Instance Attribute Summary collapse
-
#action ⇒ String
readonly
El nombre de la acción a ejecutar (ej. ‘show’).
-
#controller ⇒ String
readonly
El nombre del controlador en formato snake_case (ej. ‘api/v1/metrics’).
-
#http_method ⇒ String
readonly
El verbo HTTP de la ruta (GET, POST, PUT, DELETE).
-
#namespace ⇒ String?
readonly
El namespace del controlador si existe (ej. ‘Api::V1’).
-
#path_pattern ⇒ String
readonly
El patrón original de la ruta (ej. ‘nodes/:node_id/metrics’).
Instance Method Summary collapse
-
#extract_params(path) ⇒ Hash
Extrae los parámetros dinámicos de una URL que hizo coincidencia con el patrón.
-
#initialize(http_method, path_pattern, to:, namespace: nil) ⇒ Route
constructor
Inicializa una nueva Ruta compilando su Expresión Regular.
-
#match?(method, path) ⇒ Boolean
Evalúa si una petición entrante coincide con esta ruta.
Constructor Details
#initialize(http_method, path_pattern, to:, namespace: nil) ⇒ Route
Inicializa una nueva Ruta compilando su Expresión Regular.
33 34 35 36 37 38 39 40 |
# File 'lib/bug_bunny/routing/route.rb', line 33 def initialize(http_method, path_pattern, to:, namespace: nil) @http_method = http_method.to_s.upcase @path_pattern = normalize_path(path_pattern) @namespace = namespace parse_destination!(to) compile_regex! end |
Instance Attribute Details
#action ⇒ String (readonly)
Returns El nombre de la acción a ejecutar (ej. ‘show’).
24 25 26 |
# File 'lib/bug_bunny/routing/route.rb', line 24 def action @action end |
#controller ⇒ String (readonly)
Returns El nombre del controlador en formato snake_case (ej. ‘api/v1/metrics’).
18 19 20 |
# File 'lib/bug_bunny/routing/route.rb', line 18 def controller @controller end |
#http_method ⇒ String (readonly)
Returns El verbo HTTP de la ruta (GET, POST, PUT, DELETE).
12 13 14 |
# File 'lib/bug_bunny/routing/route.rb', line 12 def http_method @http_method end |
#namespace ⇒ String? (readonly)
Returns El namespace del controlador si existe (ej. ‘Api::V1’).
21 22 23 |
# File 'lib/bug_bunny/routing/route.rb', line 21 def namespace @namespace end |
#path_pattern ⇒ String (readonly)
Returns El patrón original de la ruta (ej. ‘nodes/:node_id/metrics’).
15 16 17 |
# File 'lib/bug_bunny/routing/route.rb', line 15 def path_pattern @path_pattern end |
Instance Method Details
#extract_params(path) ⇒ Hash
Extrae los parámetros dinámicos de una URL que hizo coincidencia con el patrón.
61 62 63 64 65 66 67 68 69 |
# File 'lib/bug_bunny/routing/route.rb', line 61 def extract_params(path) normalized_path = normalize_path(path) match_data = @regex.match(normalized_path) return {} unless match_data # match_data.named_captures devuelve un Hash con las variables que definimos en la Regex match_data.named_captures end |
#match?(method, path) ⇒ Boolean
Evalúa si una petición entrante coincide con esta ruta.
47 48 49 50 51 52 |
# File 'lib/bug_bunny/routing/route.rb', line 47 def match?(method, path) return false unless @http_method == method.to_s.upcase normalized_path = normalize_path(path) @regex.match?(normalized_path) end |