Class: BugBunny::Routing::Route

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(http_method, path_pattern, to:, namespace: nil) ⇒ Route

Inicializa una nueva Ruta compilando su Expresión Regular.

Parameters:

  • http_method (String, Symbol)

    Verbo HTTP (ej. :get, ‘POST’).

  • path_pattern (String)

    Patrón de la URL. Los parámetros dinámicos deben iniciar con ‘:’ (ej. ‘users/:id’).

  • to (String)

    Destino en formato ‘controlador#accion’ (ej. ‘users#show’).

  • namespace (String, nil) (defaults to: nil)

    El namespace del controlador (ej: ‘Api::V1’).

Raises:

  • (ArgumentError)

    Si el formato del destino ‘to` es inválido.



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

#actionString (readonly)

Returns El nombre de la acción a ejecutar (ej. ‘show’).

Returns:

  • (String)

    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

#controllerString (readonly)

Returns El nombre del controlador en formato snake_case (ej. ‘api/v1/metrics’).

Returns:

  • (String)

    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_methodString (readonly)

Returns El verbo HTTP de la ruta (GET, POST, PUT, DELETE).

Returns:

  • (String)

    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

#namespaceString? (readonly)

Returns El namespace del controlador si existe (ej. ‘Api::V1’).

Returns:

  • (String, nil)

    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_patternString (readonly)

Returns El patrón original de la ruta (ej. ‘nodes/:node_id/metrics’).

Returns:

  • (String)

    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.

Examples:

route = Route.new('GET', 'users/:id', to: 'users#show')
route.extract_params('users/42') # => { 'id' => '42' }

Parameters:

  • path (String)

    La URL entrante (ej. ‘users/123’).

Returns:

  • (Hash)

    Diccionario con las variables extraídas (ej. { ‘id’ => ‘123’ }).



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.

Parameters:

  • method (String)

    El verbo HTTP entrante.

  • path (String)

    La URL entrante a evaluar.

Returns:

  • (Boolean)

    ‘true` si hace match, `false` en caso contrario.



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