Class: BugBunny::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/bug_bunny/request.rb

Overview

Encapsula toda la información necesaria para realizar una petición o publicación.

Actúa como el objeto “Environment” en la arquitectura de middlewares. Contiene el cuerpo del mensaje, la configuración de enrutamiento, el **Verbo HTTP** y las opciones de infraestructura específicas para la petición.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Request

Inicializa un nuevo Request.

Parameters:

  • path (String)

    La ruta del recurso o acción (ej: ‘users/123’).



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bug_bunny/request.rb', line 50

def initialize(path)
  @path = path
  @method = :get # Verbo por defecto
  @headers = {}
  @params = {}
  @content_type = 'application/json'
  @timestamp = Time.now.to_i
  @persistent = false
  @exchange_type = 'direct'
  @delivery_mode = :rpc

  # Inicialización de opciones de infraestructura para evitar errores de nil durante el merge.
  @exchange_options = {}
  @queue_options = {}

  # Defaults para Publisher Confirms (modo :confirmed)
  @mandatory = false
  @confirm_timeout = nil
  @nack_raise = nil
end

Instance Attribute Details

#app_idObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def app_id
  @app_id
end

#bodyObject

El cuerpo del mensaje (Hash, Array o String).

Returns:

  • (Object)

    the current value of body



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def body
  @body
end

#confirm_timeoutObject

Publisher Confirms (delivery_mode = :confirmed)



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def confirm_timeout
  @confirm_timeout
end

#content_encodingObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def content_encoding
  @content_encoding
end

#content_typeObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def content_type
  @content_type
end

#correlation_idObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def correlation_id
  @correlation_id
end

#delivery_modeSymbol

El modo de entrega (:rpc, :publish o :confirmed).

Returns:

  • (Symbol)

    the current value of delivery_mode



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def delivery_mode
  @delivery_mode
end

#exchangeString

El nombre del Exchange destino.

Returns:

  • (String)

    the current value of exchange



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def exchange
  @exchange
end

#exchange_optionsObject

Configuración de Infraestructura Específica



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def exchange_options
  @exchange_options
end

#exchange_typeString

El tipo de exchange (‘direct’, ‘topic’, ‘fanout’).

Returns:

  • (String)

    the current value of exchange_type



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def exchange_type
  @exchange_type
end

#expirationObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def expiration
  @expiration
end

#headersHash

Cabeceras personalizadas (Headers AMQP).

Returns:

  • (Hash)

    the current value of headers



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def headers
  @headers
end

#mandatoryObject

Publisher Confirms (delivery_mode = :confirmed)



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def mandatory
  @mandatory
end

#methodSymbol, String

El verbo HTTP (:get, :post, :put, :delete). Default: :get.

Returns:

  • (Symbol, String)

    the current value of method



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def method
  @method
end

#nack_raiseObject

Publisher Confirms (delivery_mode = :confirmed)



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def nack_raise
  @nack_raise
end

#paramsHash

Parámetros de query string (ej: { q: { foo: :bar } }).

Returns:

  • (Hash)

    the current value of params



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def params
  @params
end

#pathString

La ruta lógica del recurso (ej: ‘users’, ‘users/123’).

Returns:

  • (String)

    the current value of path



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def path
  @path
end

#persistentObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def persistent
  @persistent
end

#priorityObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def priority
  @priority
end

#queue_optionsHash

Opciones específicas para la declaración de la Cola en esta petición.

Returns:

  • (Hash)

    the current value of queue_options



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def queue_options
  @queue_options
end

#reply_toObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def reply_to
  @reply_to
end

#routing_keyString

La routing key específica. Si es nil, se usará #path.

Returns:

  • (String)

    the current value of routing_key



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def routing_key
  @routing_key
end

#timeoutInteger

Tiempo máximo en segundos para timeout RPC.

Returns:

  • (Integer)

    the current value of timeout



32
33
34
# File 'lib/bug_bunny/request.rb', line 32

def timeout
  @timeout
end

#timestampObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def timestamp
  @timestamp
end

#typeObject

Metadatos AMQP Estándar



43
44
45
# File 'lib/bug_bunny/request.rb', line 43

def type
  @type
end

Instance Method Details

#amqp_optionsHash

Genera el Hash de opciones limpio para la gema Bunny.

Importante: Inyecta el verbo HTTP en los headers bajo la clave ‘x-http-method`. Esto permite al Consumer enrutar correctamente a la acción del controlador.

También inyecta los campos de OTel semantic conventions for messaging (ver OTel) con ‘operation=publish`. Los headers del usuario pueden sobrescribir los valores OTel (escape hatch); `x-http-method` nunca se pisa porque es lo último en el merge.

Returns:

  • (Hash)

    Opciones listas para pasar a ‘exchange.publish`.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bug_bunny/request.rb', line 110

def amqp_options
  otel_headers = BugBunny::OTel.messaging_headers(
    operation: 'publish',
    destination: exchange,
    routing_key: final_routing_key,
    message_id: correlation_id
  )
  # Orden del merge: OTel base -> headers del usuario -> x-http-method (inmutable)
  # OTel keys son symbols internamente; los stringificamos para Bunny AMQP headers.
  final_headers = otel_headers.transform_keys(&:to_s).merge(headers).merge('x-http-method' => method.to_s.upcase)

  {
    type: final_type,
    app_id: app_id,
    content_type: content_type,
    content_encoding: content_encoding,
    priority: priority,
    timestamp: timestamp,
    expiration: expiration,
    persistent: persistent,
    headers: final_headers,
    reply_to: reply_to,
    correlation_id: correlation_id,
    mandatory: (true if mandatory)
  }.compact
end

#final_routing_keyString

Calcula la Routing Key final que se usará en RabbitMQ.

Principio: “Convention over Configuration”. Si no se define una ‘routing_key` manual, se asume que el `path` actúa como tal. Los params NO afectan la routing key — son metadata de la petición, no del enrutamiento del exchange.

Returns:

  • (String)

    La routing key definitiva.



87
88
89
# File 'lib/bug_bunny/request.rb', line 87

def final_routing_key
  routing_key || path
end

#final_typeString

Calcula el valor para el header AMQP ‘type’. En esta arquitectura REST, el ‘type’ es la URL completa del recurso (path + query string).

Returns:

  • (String)

    El tipo de mensaje definitivo.



95
96
97
# File 'lib/bug_bunny/request.rb', line 95

def final_type
  type || full_path
end

#full_pathString

Combina el path con los params como query string.

Returns:

  • (String)

    El path completo con query string si hay params, o solo el path.



74
75
76
77
78
# File 'lib/bug_bunny/request.rb', line 74

def full_path
  return path if params.nil? || params.empty?

  "#{path}?#{Rack::Utils.build_nested_query(params)}"
end