Class: Lepus::Testing::MessageBuilder
- Inherits:
-
Object
- Object
- Lepus::Testing::MessageBuilder
- Defined in:
- lib/lepus/testing/message_builder.rb
Overview
Builder class for creating test messages with realistic Bunny objects
Instance Method Summary collapse
-
#build ⇒ Object
Build the Lepus::Message using internal data classes.
-
#initialize ⇒ MessageBuilder
constructor
A new instance of MessageBuilder.
-
#with_app_id(app_id) ⇒ Object
Set app ID.
-
#with_channel(channel) ⇒ Object
Set the channel (for middlewares that need it).
-
#with_consumer_tag(consumer_tag) ⇒ Object
Set consumer tag.
-
#with_content_type(content_type) ⇒ Object
Set content type.
-
#with_correlation_id(correlation_id) ⇒ Object
Set correlation ID.
-
#with_delivery_info_attrs(attrs) ⇒ Object
Set custom delivery info attributes.
-
#with_delivery_mode(mode) ⇒ Object
Set delivery mode (1 = non-persistent, 2 = persistent).
-
#with_delivery_tag(tag) ⇒ Object
Set delivery tag.
-
#with_exchange(exchange) ⇒ Object
Set exchange name.
-
#with_expiration(expiration) ⇒ Object
Set message expiration.
-
#with_headers(headers) ⇒ Object
Set headers.
-
#with_message_id(message_id) ⇒ Object
Set message ID.
-
#with_metadata_attrs(attrs) ⇒ Object
Set custom metadata attributes.
-
#with_payload(payload) ⇒ Object
Set the message payload.
-
#with_priority(priority) ⇒ Object
Set priority.
-
#with_redelivered(redelivered = true) ⇒ Object
Set redelivered flag.
-
#with_reply_to(reply_to) ⇒ Object
Set reply to queue.
-
#with_routing_key(routing_key) ⇒ Object
Set routing key.
-
#with_timestamp(timestamp) ⇒ Object
Set timestamp.
-
#with_type(type) ⇒ Object
Set message type.
-
#with_user_id(user_id) ⇒ Object
Set user ID.
Constructor Details
#initialize ⇒ MessageBuilder
Returns a new instance of MessageBuilder.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lepus/testing/message_builder.rb', line 7 def initialize @delivery_info_attrs = { delivery_tag: 1, redelivered: false, exchange: "test_exchange", routing_key: "test.routing.key", consumer_tag: "test_consumer_tag" } @metadata_attrs = { content_type: "application/json", content_encoding: "utf-8", headers: {}, delivery_mode: 2, priority: 0, correlation_id: nil, reply_to: nil, expiration: nil, message_id: SecureRandom.uuid, timestamp: Time.now.to_i, type: nil, user_id: nil, app_id: nil, cluster_id: nil } @payload = nil @channel = nil end |
Instance Method Details
#build ⇒ Object
Build the Lepus::Message using internal data classes
167 168 169 170 171 172 173 174 |
# File 'lib/lepus/testing/message_builder.rb', line 167 def build raise ArgumentError, "Payload is required" if @payload.nil? delivery_info = Lepus::Message::DeliveryInfo.new(**@delivery_info_attrs) = Lepus::Message::Metadata.new(**@metadata_attrs) Lepus::Message.new(delivery_info, , @payload, channel: @channel) end |
#with_app_id(app_id) ⇒ Object
Set app ID
131 132 133 134 |
# File 'lib/lepus/testing/message_builder.rb', line 131 def with_app_id(app_id) @metadata_attrs[:app_id] = app_id self end |
#with_channel(channel) ⇒ Object
Set the channel (for middlewares that need it)
161 162 163 164 |
# File 'lib/lepus/testing/message_builder.rb', line 161 def with_channel(channel) @channel = channel self end |
#with_consumer_tag(consumer_tag) ⇒ Object
Set consumer tag
65 66 67 68 |
# File 'lib/lepus/testing/message_builder.rb', line 65 def with_consumer_tag(consumer_tag) @delivery_info_attrs[:consumer_tag] = consumer_tag self end |
#with_content_type(content_type) ⇒ Object
Set content type
77 78 79 80 |
# File 'lib/lepus/testing/message_builder.rb', line 77 def with_content_type(content_type) @metadata_attrs[:content_type] = content_type self end |
#with_correlation_id(correlation_id) ⇒ Object
Set correlation ID
89 90 91 92 |
# File 'lib/lepus/testing/message_builder.rb', line 89 def with_correlation_id(correlation_id) @metadata_attrs[:correlation_id] = correlation_id self end |
#with_delivery_info_attrs(attrs) ⇒ Object
Set custom delivery info attributes
149 150 151 152 |
# File 'lib/lepus/testing/message_builder.rb', line 149 def with_delivery_info_attrs(attrs) @delivery_info_attrs.merge!(attrs) self end |
#with_delivery_mode(mode) ⇒ Object
Set delivery mode (1 = non-persistent, 2 = persistent)
137 138 139 140 |
# File 'lib/lepus/testing/message_builder.rb', line 137 def with_delivery_mode(mode) @metadata_attrs[:delivery_mode] = mode self end |
#with_delivery_tag(tag) ⇒ Object
Set delivery tag
47 48 49 50 |
# File 'lib/lepus/testing/message_builder.rb', line 47 def with_delivery_tag(tag) @delivery_info_attrs[:delivery_tag] = tag self end |
#with_exchange(exchange) ⇒ Object
Set exchange name
59 60 61 62 |
# File 'lib/lepus/testing/message_builder.rb', line 59 def with_exchange(exchange) @delivery_info_attrs[:exchange] = exchange self end |
#with_expiration(expiration) ⇒ Object
Set message expiration
101 102 103 104 |
# File 'lib/lepus/testing/message_builder.rb', line 101 def with_expiration(expiration) @metadata_attrs[:expiration] = expiration self end |
#with_headers(headers) ⇒ Object
Set headers
83 84 85 86 |
# File 'lib/lepus/testing/message_builder.rb', line 83 def with_headers(headers) @metadata_attrs[:headers] = headers self end |
#with_message_id(message_id) ⇒ Object
Set message ID
107 108 109 110 |
# File 'lib/lepus/testing/message_builder.rb', line 107 def () @metadata_attrs[:message_id] = self end |
#with_metadata_attrs(attrs) ⇒ Object
Set custom metadata attributes
155 156 157 158 |
# File 'lib/lepus/testing/message_builder.rb', line 155 def (attrs) @metadata_attrs.merge!(attrs) self end |
#with_payload(payload) ⇒ Object
Set the message payload
36 37 38 39 40 41 42 43 44 |
# File 'lib/lepus/testing/message_builder.rb', line 36 def with_payload(payload) if payload.is_a?(Hash) || payload.is_a?(Array) payload = MultiJson.dump(payload) @metadata_attrs[:content_type] ||= "application/json" end @payload = payload self end |
#with_priority(priority) ⇒ Object
Set priority
143 144 145 146 |
# File 'lib/lepus/testing/message_builder.rb', line 143 def with_priority(priority) @metadata_attrs[:priority] = priority self end |
#with_redelivered(redelivered = true) ⇒ Object
Set redelivered flag
71 72 73 74 |
# File 'lib/lepus/testing/message_builder.rb', line 71 def with_redelivered(redelivered = true) @delivery_info_attrs[:redelivered] = redelivered self end |
#with_reply_to(reply_to) ⇒ Object
Set reply to queue
95 96 97 98 |
# File 'lib/lepus/testing/message_builder.rb', line 95 def with_reply_to(reply_to) @metadata_attrs[:reply_to] = reply_to self end |
#with_routing_key(routing_key) ⇒ Object
Set routing key
53 54 55 56 |
# File 'lib/lepus/testing/message_builder.rb', line 53 def with_routing_key(routing_key) @delivery_info_attrs[:routing_key] = routing_key self end |
#with_timestamp(timestamp) ⇒ Object
Set timestamp
113 114 115 116 |
# File 'lib/lepus/testing/message_builder.rb', line 113 def () @metadata_attrs[:timestamp] = self end |
#with_type(type) ⇒ Object
Set message type
119 120 121 122 |
# File 'lib/lepus/testing/message_builder.rb', line 119 def with_type(type) @metadata_attrs[:type] = type self end |
#with_user_id(user_id) ⇒ Object
Set user ID
125 126 127 128 |
# File 'lib/lepus/testing/message_builder.rb', line 125 def with_user_id(user_id) @metadata_attrs[:user_id] = user_id self end |