Class: Bifrost::Message

Inherits:
Entity
  • Object
show all
Defined in:
lib/bifrost/message.rb

Overview

A message is a letter that we send to a topic. It must contain a subject and body The receiver can process both fields on receipt of the message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, meta = {}) ⇒ Message

A message must have a valid subject and body. The service bus is initialised in the Entity class



15
16
17
18
19
20
21
22
23
24
# File 'lib/bifrost/message.rb', line 15

def initialize(body, meta = {})
  @meta = meta
  if body.is_a?(Azure::ServiceBus::BrokeredMessage)
    merge(body)
  else
    @body ||= body
    @status ||= :undelivered
  end
  super()
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/bifrost/message.rb', line 8

def body
  @body
end

#message_idObject (readonly) Also known as: resource_id, id

Returns the value of attribute message_id.



8
9
10
# File 'lib/bifrost/message.rb', line 8

def message_id
  @message_id
end

#metaObject (readonly)

Returns the value of attribute meta.



8
9
10
# File 'lib/bifrost/message.rb', line 8

def meta
  @meta
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/bifrost/message.rb', line 8

def status
  @status
end

Instance Method Details

#publish(topic) ⇒ Object

A message can be posted to a particular topic



27
28
29
30
31
32
33
34
# File 'lib/bifrost/message.rb', line 27

def publish(topic)
  if topic.exists?
    send_message(topic, create_brokered_message)
    true
  else
    false
  end
end

#publish!(topic) ⇒ Object

A message can be posted to a particular topic, if the message is succssfully delivered we return a unique identifier for the message



38
39
40
41
42
43
44
45
# File 'lib/bifrost/message.rb', line 38

def publish!(topic)
  if topic.exists?
    send_message(topic, create_brokered_message)
    message_id
  else
    raise Bifrost::Exceptions::MessageDeliveryError, "Could not post message to #{topic}"
  end
end

#to_sObject

A message when serialised to a string just renders the messag identifier



48
49
50
# File 'lib/bifrost/message.rb', line 48

def to_s
  id
end