Class: Osbourne::Message

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

Direct Known Subclasses

Test::Message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Message

Returns a new instance of Message.



11
12
13
# File 'lib/osbourne/message.rb', line 11

def initialize(message)
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



10
11
12
# File 'lib/osbourne/message.rb', line 10

def message
  @message
end

Instance Method Details

#deleteObject

Deletes the message from SQS to prevent retrying against another worker. Osbourne will automatically delete a message sent to a worker as long as the Osourbne::WorkerBase#process method returns true



54
55
56
57
# File 'lib/osbourne/message.rb', line 54

def delete
  message.delete
  Osbourne.logger.info "[Osbourne] [MSG ID: #{id}] Cleared"
end

#idString

Osbourne has built-in message deduplication, but it’s still a good idea to do some verification in a worker

Returns:

  • (String)

    The UUID of the recieved message



35
36
37
# File 'lib/osbourne/message.rb', line 35

def id
  message.message_id
end

#json?Boolean

Returns This will be true if the SNS message is also JSON.

Returns:

  • (Boolean)

    This will be true if the SNS message is also JSON



18
19
20
21
22
# File 'lib/osbourne/message.rb', line 18

def json?
  return false unless valid?

  sns_body.is_a?(Hash)
end

#message_bodyHash, ...

If the message was broadcast via SNS, the body will be available here.

Returns:

  • (Hash)

    If the message was JSON

  • (String)

    If the message was not JSON

  • (nil)

    If the message was not broadcast via SNS.

See Also:



46
47
48
# File 'lib/osbourne/message.rb', line 46

def message_body
  sns_body
end

#raw_bodyString

Returns The raw string representation of the message.

Returns:

  • (String)

    The raw string representation of the message



71
72
73
# File 'lib/osbourne/message.rb', line 71

def raw_body
  message.body
end

#sns?Boolean

Just because a message was recieved via SQS, doesn’t mean it was originally broadcast via SNS

Returns:

  • (Boolean)

    Was the message broadcast via SNS?



78
79
80
# File 'lib/osbourne/message.rb', line 78

def sns?
  json_body.is_a?(Hash) && (%w[Message Type TopicArn MessageId] - json_body.keys).empty?
end

#topicString?

The SNS topic that this message was broadcast to

Returns:

  • (String)

    if the message was broadcast via SNS, this will be the topic

  • (nil)

    if the message was not broadcast via SNS



63
64
65
66
67
# File 'lib/osbourne/message.rb', line 63

def topic
  return nil unless sns?

  json_body["TopicArn"].split(":").last
end

#valid?Boolean

Does the message match the checksum? If not, the message has likely been mangled in transit

Returns:

  • (Boolean)


27
28
29
# File 'lib/osbourne/message.rb', line 27

def valid?
  @valid ||= message.md5_of_body == Digest::MD5.hexdigest(message.body)
end