Class: Lepus::Message

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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 = publish_options || {}
end

Instance Attribute Details

#consumer_classObject

Returns the value of attribute consumer_class.



6
7
8
# File 'lib/lepus/message.rb', line 6

def consumer_class
  @consumer_class
end

#delivery_infoObject (readonly)

Returns the value of attribute delivery_info.



5
6
7
# File 'lib/lepus/message.rb', line 5

def delivery_info
  @delivery_info
end

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/lepus/message.rb', line 5

def 
  @metadata
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/lepus/message.rb', line 5

def payload
  @payload
end

#publish_optionsObject (readonly)

Returns the value of attribute publish_options.



5
6
7
# File 'lib/lepus/message.rb', line 5

def publish_options
  @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.

Parameters:

  • bunny_delivery_info (Bunny::DeliveryInfo, DeliveryInfo)

    The delivery info from Bunny or internal class

  • bunny_metadata (Bunny::MessageProperties, Metadata)

    The metadata from Bunny or internal class

  • payload (String)

    The raw message payload

Returns:



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

#channelBunny::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.

Returns:

  • (Bunny::Channel, nil)

    The channel or nil if unavailable



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: ==

Returns:

  • (Boolean)


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 || @publish_options
  ).tap do |message|
    message.consumer_class = consumer_class || @consumer_class
  end
end

#to_hObject



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_optionsHash

Converts metadata and delivery_info into a hash suitable for Publisher.publish. Used by producer middlewares to pass options to the publisher.

Returns:

  • (Hash)

    Options hash compatible with Publisher.publish



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 to_publish_options
  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] = .message_id if .message_id
    opts[:timestamp] = .timestamp if .timestamp
    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