Class: Lepus::Message
- Inherits:
-
Object
- Object
- Lepus::Message
- Defined in:
- lib/lepus/message.rb,
lib/lepus/message/metadata.rb,
lib/lepus/message/delivery_info.rb
Defined Under Namespace
Classes: DeliveryInfo, Metadata
Instance Attribute Summary collapse
-
#consumer_class ⇒ Object
Returns the value of attribute consumer_class.
-
#delivery_info ⇒ Object
readonly
Returns the value of attribute delivery_info.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#publish_options ⇒ Object
readonly
Returns the value of attribute publish_options.
Class Method Summary collapse
-
.coerce(bunny_delivery_info, bunny_metadata, payload) ⇒ Message
Coerce raw Bunny objects into a Message with internal data classes.
Instance Method Summary collapse
-
#channel ⇒ Bunny::Channel?
Returns the channel associated with this message.
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(delivery_info, metadata, payload, channel: nil, publish_options: nil) ⇒ Message
constructor
A new instance of Message.
- #mutate(payload: nil, metadata: nil, delivery_info: nil, consumer_class: nil, channel: nil, publish_options: nil) ⇒ Object
- #to_h ⇒ Object
-
#to_publish_options ⇒ Hash
Converts metadata and delivery_info into a hash suitable for Publisher.publish.
Constructor Details
#initialize(delivery_info, metadata, payload, channel: nil, publish_options: nil) ⇒ Message
Returns a new instance of Message.
34 35 36 37 38 39 40 |
# File 'lib/lepus/message.rb', line 34 def initialize(delivery_info, , payload, channel: nil, publish_options: nil) @delivery_info = delivery_info @metadata = @payload = payload @channel = channel @publish_options = || {} end |
Instance Attribute Details
#consumer_class ⇒ Object
Returns the value of attribute consumer_class.
6 7 8 |
# File 'lib/lepus/message.rb', line 6 def consumer_class @consumer_class end |
#delivery_info ⇒ Object (readonly)
Returns the value of attribute delivery_info.
5 6 7 |
# File 'lib/lepus/message.rb', line 5 def delivery_info @delivery_info end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
5 6 7 |
# File 'lib/lepus/message.rb', line 5 def @metadata end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
5 6 7 |
# File 'lib/lepus/message.rb', line 5 def payload @payload end |
#publish_options ⇒ Object (readonly)
Returns the value of attribute publish_options.
5 6 7 |
# File 'lib/lepus/message.rb', line 5 def @publish_options end |
Class Method Details
.coerce(bunny_delivery_info, bunny_metadata, payload) ⇒ Message
Coerce raw Bunny objects into a Message with internal data classes. This decouples the Message from Bunny-specific objects. If the objects are already internal classes, they are used as-is.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lepus/message.rb', line 16 def self.coerce(bunny_delivery_info, , payload) delivery_info = if bunny_delivery_info.is_a?(DeliveryInfo) bunny_delivery_info else DeliveryInfo.from_bunny(bunny_delivery_info) end = if .is_a?(Metadata) else Metadata.from_bunny() end channel = bunny_delivery_info.respond_to?(:channel) ? bunny_delivery_info.channel : nil new(delivery_info, , payload, channel: channel) end |
Instance Method Details
#channel ⇒ Bunny::Channel?
Returns the channel associated with this message. Falls back to checking out a new channel from the producer connection pool if none is set. Note: The fallback channel is not memoized; each call will checkout a new channel.
47 48 49 50 51 |
# File 'lib/lepus/message.rb', line 47 def channel return @channel if @channel checkout_channel end |
#eql?(other) ⇒ Boolean Also known as: ==
102 103 104 105 106 107 |
# File 'lib/lepus/message.rb', line 102 def eql?(other) other.is_a?(self.class) && delivery_info == other.delivery_info && == other. && payload == other.payload end |
#mutate(payload: nil, metadata: nil, delivery_info: nil, consumer_class: nil, channel: nil, publish_options: nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lepus/message.rb', line 53 def mutate(payload: nil, metadata: nil, delivery_info: nil, consumer_class: nil, channel: nil, publish_options: nil) self.class.new( delivery_info || @delivery_info, || @metadata, payload || @payload, channel: channel || @channel, publish_options: || @publish_options ).tap do || .consumer_class = consumer_class || @consumer_class end end |
#to_h ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/lepus/message.rb', line 65 def to_h { delivery: delivery_info&.to_h, metadata: &.to_h, payload: payload } end |
#to_publish_options ⇒ Hash
Converts metadata and delivery_info into a hash suitable for Publisher.publish. Used by producer middlewares to pass options to the publisher.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lepus/message.rb', line 77 def opts = @publish_options.dup if delivery_info opts[:routing_key] = delivery_info.routing_key if delivery_info.routing_key end if opts[:headers] = .headers if .headers opts[:content_type] = .content_type if .content_type opts[:content_encoding] = .content_encoding if .content_encoding opts[:correlation_id] = .correlation_id if .correlation_id opts[:reply_to] = .reply_to if .reply_to opts[:expiration] = .expiration if .expiration opts[:message_id] = . if . opts[:timestamp] = . if . opts[:type] = .type if .type opts[:app_id] = .app_id if .app_id opts[:priority] = .priority if .priority opts[:delivery_mode] = .delivery_mode if .delivery_mode end opts end |