class Cloudenvoy::Message

Represents a Pub/Sub message

Attributes

id[RW]
metadata[RW]
payload[RW]
sub_uri[RW]
topic[W]

Public Class Methods

from_descriptor(input_payload) click to toggle source

Return an instantiated message from a Pub/Sub webhook payload.

@param [Hash] input_payload The Pub/Sub webhook hash describing the message to process.

@return [Cloudenvoy::Message] The instantiated message.

# File lib/cloudenvoy/message.rb, line 18
def self.from_descriptor(input_payload)
  # Build new message
  new(
    id: input_payload.dig('message', 'message_id'),
    payload: JSON.parse(Base64.decode64(input_payload.dig('message', 'data'))),
    metadata: input_payload.dig('message', 'attributes'),
    sub_uri: input_payload['subscription']
  )
end
new(id: nil, payload: nil, metadata: nil, topic: nil, sub_uri: nil) click to toggle source

Constructor

@param [String] id The message ID @param [Hash, String] payload The message payload @param [Hash] metadata The message attributes @param [String] topic The topic - will be inferred from sub_uri if left blank @param [String] sub_uri The sub_uri this message was sent for

# File lib/cloudenvoy/message.rb, line 37
def initialize(id: nil, payload: nil, metadata: nil, topic: nil, sub_uri: nil)
  @id = id
  @payload = payload
  @topic = topic
  @metadata = metadata || {}
  @sub_uri = sub_uri
end

Public Instance Methods

==(other) click to toggle source

Equality operator.

@param [Any] other The object to compare.

@return [Boolean] True if the object is equal.

# File lib/cloudenvoy/message.rb, line 92
def ==(other)
  other.is_a?(self.class) && other.id == id
end
subscriber() click to toggle source

Return the instantiated Subscriber designated to process this message.

@return [Subscriber] The instantiated subscriber.

# File lib/cloudenvoy/message.rb, line 62
def subscriber
  @subscriber ||= begin
    return nil unless sub_uri && (klass = Subscriber.from_sub_uri(sub_uri))

    klass.new(message: self)
  end
end
to_h() click to toggle source

Return a hash description of the message.

@return [Hash] The message description

# File lib/cloudenvoy/message.rb, line 75
def to_h
  {
    id: id,
    payload: payload,
    metadata: metadata,
    topic: topic,
    sub_uri: sub_uri
  }.compact
end
topic() click to toggle source

Return the message topic.

@return [String] The message topic.

# File lib/cloudenvoy/message.rb, line 50
def topic
  return @topic if @topic
  return nil unless sub_uri

  Subscriber.parse_sub_uri(sub_uri)[1]
end