class Pheme::TopicPublisher

Constants

EXPECTED_METADATA_SIZE
MESSAGE_SIZE_LIMIT
SNS_SIZE_LIMIT

Constant with message size limit. The message size also includes some metadata: ‘name’ and ‘type’. We give ourselves a buffer for this metadata.

Source: docs.aws.amazon.com/sns/latest/dg/SNSMessageAttributes.html#SNSMessageAttributesNTV

Attributes

_topic_arn[R]
topic_arn[RW]

Public Class Methods

new(topic_arn: self.class._topic_arn) click to toggle source
# File lib/pheme/topic_publisher.rb, line 26
def initialize(topic_arn: self.class._topic_arn)
  raise ArgumentError, "must specify non-nil topic_arn" if topic_arn.blank?

  @topic_arn = topic_arn
end
topic_arn(topic_arn) click to toggle source
# File lib/pheme/topic_publisher.rb, line 21
def topic_arn(topic_arn)
  @_topic_arn = topic_arn
end

Public Instance Methods

publish(message, sns_client: Pheme.configuration.sns_client, message_attributes: nil, message_deduplication_id: nil, message_group_id: nil) click to toggle source
# File lib/pheme/topic_publisher.rb, line 37
def publish(message,
  sns_client: Pheme.configuration.sns_client,
  message_attributes: nil,
  message_deduplication_id: nil,
  message_group_id: nil)
  payload = {
    message: "#{self.class} publishing message to #{topic_arn}",
    body: message,
    publisher: self.class.to_s,
    topic_arn: topic_arn,
  }

  Pheme.logger.info(payload.except(:body).to_json)

  sns_client.publish(
    topic_arn: topic_arn,
    message: serialize(message),
    message_attributes: message_attributes,
    message_deduplication_id: message_deduplication_id,
    message_group_id: message_group_id,
  )
end
publish_events() click to toggle source
# File lib/pheme/topic_publisher.rb, line 33
def publish_events
  raise NotImplementedError
end
serialize(message) click to toggle source
# File lib/pheme/topic_publisher.rb, line 60
def serialize(message)
  message = message.to_json unless message.is_a? String

  return compress(message) if message.bytesize > MESSAGE_SIZE_LIMIT

  message
end