Class: BugBunny::Request
- Inherits:
-
Object
- Object
- BugBunny::Request
- 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
-
#app_id ⇒ Object
Metadatos AMQP Estándar.
-
#body ⇒ Object
El cuerpo del mensaje (Hash, Array o String).
-
#confirm_timeout ⇒ Object
Publisher Confirms (delivery_mode = :confirmed).
-
#content_encoding ⇒ Object
Metadatos AMQP Estándar.
-
#content_type ⇒ Object
Metadatos AMQP Estándar.
-
#correlation_id ⇒ Object
Metadatos AMQP Estándar.
-
#delivery_mode ⇒ Symbol
El modo de entrega (:rpc, :publish o :confirmed).
-
#exchange ⇒ String
El nombre del Exchange destino.
-
#exchange_options ⇒ Object
Configuración de Infraestructura Específica.
-
#exchange_type ⇒ String
El tipo de exchange (‘direct’, ‘topic’, ‘fanout’).
-
#expiration ⇒ Object
Metadatos AMQP Estándar.
-
#headers ⇒ Hash
Cabeceras personalizadas (Headers AMQP).
-
#mandatory ⇒ Object
Publisher Confirms (delivery_mode = :confirmed).
-
#method ⇒ Symbol, String
El verbo HTTP (:get, :post, :put, :delete).
-
#nack_raise ⇒ Object
Publisher Confirms (delivery_mode = :confirmed).
-
#params ⇒ Hash
Parámetros de query string (ej: { q: { foo: :bar } }).
-
#path ⇒ String
La ruta lógica del recurso (ej: ‘users’, ‘users/123’).
-
#persistent ⇒ Object
Metadatos AMQP Estándar.
-
#priority ⇒ Object
Metadatos AMQP Estándar.
-
#queue_options ⇒ Hash
Opciones específicas para la declaración de la Cola en esta petición.
-
#reply_to ⇒ Object
Metadatos AMQP Estándar.
-
#routing_key ⇒ String
La routing key específica.
-
#timeout ⇒ Integer
Tiempo máximo en segundos para timeout RPC.
-
#timestamp ⇒ Object
Metadatos AMQP Estándar.
-
#type ⇒ Object
Metadatos AMQP Estándar.
Instance Method Summary collapse
-
#amqp_options ⇒ Hash
Genera el Hash de opciones limpio para la gema Bunny.
-
#final_routing_key ⇒ String
Calcula la Routing Key final que se usará en RabbitMQ.
-
#final_type ⇒ String
Calcula el valor para el header AMQP ‘type’.
-
#full_path ⇒ String
Combina el path con los params como query string.
-
#initialize(path) ⇒ Request
constructor
Inicializa un nuevo Request.
Constructor Details
#initialize(path) ⇒ Request
Inicializa un nuevo Request.
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_id ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def app_id @app_id end |
#body ⇒ Object
El cuerpo del mensaje (Hash, Array o String).
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def body @body end |
#confirm_timeout ⇒ Object
Publisher Confirms (delivery_mode = :confirmed)
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def confirm_timeout @confirm_timeout end |
#content_encoding ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def content_encoding @content_encoding end |
#content_type ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def content_type @content_type end |
#correlation_id ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def correlation_id @correlation_id end |
#delivery_mode ⇒ Symbol
El modo de entrega (:rpc, :publish o :confirmed).
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def delivery_mode @delivery_mode end |
#exchange ⇒ String
El nombre del Exchange destino.
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def exchange @exchange end |
#exchange_options ⇒ Object
Configuración de Infraestructura Específica
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def @exchange_options end |
#exchange_type ⇒ String
El tipo de exchange (‘direct’, ‘topic’, ‘fanout’).
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def exchange_type @exchange_type end |
#expiration ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def expiration @expiration end |
#headers ⇒ Hash
Cabeceras personalizadas (Headers AMQP).
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def headers @headers end |
#mandatory ⇒ Object
Publisher Confirms (delivery_mode = :confirmed)
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def mandatory @mandatory end |
#method ⇒ Symbol, String
El verbo HTTP (:get, :post, :put, :delete). Default: :get.
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def method @method end |
#nack_raise ⇒ Object
Publisher Confirms (delivery_mode = :confirmed)
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def nack_raise @nack_raise end |
#params ⇒ Hash
Parámetros de query string (ej: { q: { foo: :bar } }).
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def params @params end |
#path ⇒ String
La ruta lógica del recurso (ej: ‘users’, ‘users/123’).
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def path @path end |
#persistent ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def persistent @persistent end |
#priority ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def priority @priority end |
#queue_options ⇒ Hash
Opciones específicas para la declaración de la Cola en esta petición.
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def @queue_options end |
#reply_to ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def reply_to @reply_to end |
#routing_key ⇒ String
La routing key específica. Si es nil, se usará #path.
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def routing_key @routing_key end |
#timeout ⇒ Integer
Tiempo máximo en segundos para timeout RPC.
32 33 34 |
# File 'lib/bug_bunny/request.rb', line 32 def timeout @timeout end |
#timestamp ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def @timestamp end |
#type ⇒ Object
Metadatos AMQP Estándar
43 44 45 |
# File 'lib/bug_bunny/request.rb', line 43 def type @type end |
Instance Method Details
#amqp_options ⇒ Hash
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.
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 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: , expiration: expiration, persistent: persistent, headers: final_headers, reply_to: reply_to, correlation_id: correlation_id, mandatory: (true if mandatory) }.compact end |
#final_routing_key ⇒ String
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.
87 88 89 |
# File 'lib/bug_bunny/request.rb', line 87 def final_routing_key routing_key || path end |
#final_type ⇒ String
Calcula el valor para el header AMQP ‘type’. En esta arquitectura REST, el ‘type’ es la URL completa del recurso (path + query string).
95 96 97 |
# File 'lib/bug_bunny/request.rb', line 95 def final_type type || full_path end |
#full_path ⇒ String
Combina el path con los params como query string.
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 |