class Osbourne::Message

Attributes

message[R]

Public Class Methods

new(message) click to toggle source
# File lib/osbourne/message.rb, line 11
def initialize(message)
  @message = message
end

Public Instance Methods

delete() click to toggle source

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`

# File lib/osbourne/message.rb, line 54
def delete
  message.delete
  Osbourne.logger.info "[Osbourne] [MSG ID: #{id}] Cleared"
end
id() click to toggle source

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

@return [String] The UUID of the recieved message

# File lib/osbourne/message.rb, line 35
def id
  message.message_id
end
json?() click to toggle source

@return [Boolean] This will be `true` if the SNS message is also JSON

# File lib/osbourne/message.rb, line 18
def json?
  return false unless valid?

  sns_body.is_a?(Hash)
end
message_body() click to toggle source

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

@return [Hash] If the message was JSON @return [String] If the message was not JSON @return [nil] If the message was not broadcast via SNS. @see raw_body raw_body for the raw body string

# File lib/osbourne/message.rb, line 46
def message_body
  sns_body
end
raw_body() click to toggle source

@return [String] The raw string representation of the message

# File lib/osbourne/message.rb, line 71
def raw_body
  message.body
end
sns?() click to toggle source

Just because a message was recieved via SQS, doesn't mean it was originally broadcast via SNS @return [Boolean] Was the message broadcast via SNS?

# File lib/osbourne/message.rb, line 78
def sns?
  json_body.is_a?(Hash) && (%w[Message Type TopicArn MessageId] - json_body.keys).empty?
end
topic() click to toggle source

The SNS topic that this message was broadcast to @return [String] if the message was broadcast via SNS, this will be the topic @return [nil] if the message was not broadcast via SNS

# File lib/osbourne/message.rb, line 63
def topic
  return nil unless sns?

  json_body["TopicArn"].split(":").last
end
valid?() click to toggle source

Does the message match the checksum? If not, the message has likely been mangled in transit @return [Boolean]

# File lib/osbourne/message.rb, line 27
def valid?
  @valid ||= message.md5_of_body == Digest::MD5.hexdigest(message.body)
end

Private Instance Methods

json_body() click to toggle source
# File lib/osbourne/message.rb, line 96
def json_body
  @json_body || safe_json(message.body)
end
safe_json(content) click to toggle source
# File lib/osbourne/message.rb, line 84
def safe_json(content)
  JSON.parse(content)
rescue JSON::ParserError
  false
end
sns_body() click to toggle source
# File lib/osbourne/message.rb, line 90
def sns_body
  return unless sns?

  @sns_body ||= safe_json(json_body["Message"]) || json_body["Message"]
end