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.
-
#return_raise ⇒ Object
Publisher Confirms (delivery_mode = :confirmed).
-
#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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bug_bunny/request.rb', line 53 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 @return_raise = nil end |
Instance Attribute Details
#app_id ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def app_id @app_id end |
#body ⇒ Object
El cuerpo del mensaje (Hash, Array o String).
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def body @body end |
#confirm_timeout ⇒ Object
Publisher Confirms (delivery_mode = :confirmed)
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def confirm_timeout @confirm_timeout end |
#content_encoding ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def content_encoding @content_encoding end |
#content_type ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def content_type @content_type end |
#correlation_id ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def correlation_id @correlation_id end |
#delivery_mode ⇒ Symbol
El modo de entrega (:rpc, :publish o :confirmed).
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def delivery_mode @delivery_mode end |
#exchange ⇒ String
El nombre del Exchange destino.
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def exchange @exchange end |
#exchange_options ⇒ Object
Configuración de Infraestructura Específica
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def @exchange_options end |
#exchange_type ⇒ String
El tipo de exchange (‘direct’, ‘topic’, ‘fanout’).
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def exchange_type @exchange_type end |
#expiration ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def expiration @expiration end |
#headers ⇒ Hash
Cabeceras personalizadas (Headers AMQP).
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def headers @headers end |
#mandatory ⇒ Object
Publisher Confirms (delivery_mode = :confirmed)
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def mandatory @mandatory end |
#method ⇒ Symbol, String
El verbo HTTP (:get, :post, :put, :delete). Default: :get.
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def method @method end |
#nack_raise ⇒ Object
Publisher Confirms (delivery_mode = :confirmed)
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def nack_raise @nack_raise end |
#params ⇒ Hash
Parámetros de query string (ej: { q: { foo: :bar } }).
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def params @params end |
#path ⇒ String
La ruta lógica del recurso (ej: ‘users’, ‘users/123’).
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def path @path end |
#persistent ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def persistent @persistent end |
#priority ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def priority @priority end |
#queue_options ⇒ Hash
Opciones específicas para la declaración de la Cola en esta petición.
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def @queue_options end |
#reply_to ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def reply_to @reply_to end |
#return_raise ⇒ Object
Publisher Confirms (delivery_mode = :confirmed)
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def return_raise @return_raise end |
#routing_key ⇒ String
La routing key específica. Si es nil, se usará #path.
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def routing_key @routing_key end |
#timeout ⇒ Integer
Tiempo máximo en segundos para timeout RPC.
35 36 37 |
# File 'lib/bug_bunny/request.rb', line 35 def timeout @timeout end |
#timestamp ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 def @timestamp end |
#type ⇒ Object
Metadatos AMQP Estándar
46 47 48 |
# File 'lib/bug_bunny/request.rb', line 46 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.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/bug_bunny/request.rb', line 114 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.
91 92 93 |
# File 'lib/bug_bunny/request.rb', line 91 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).
99 100 101 |
# File 'lib/bug_bunny/request.rb', line 99 def final_type type || full_path end |
#full_path ⇒ String
Combina el path con los params como query string.
78 79 80 81 82 |
# File 'lib/bug_bunny/request.rb', line 78 def full_path return path if params.nil? || params.empty? "#{path}?#{Rack::Utils.build_nested_query(params)}" end |