Sha256: a0f15ab245adc310d16820e8d09de78ff7a1a611bf9a049b43d54fa7cd8e8eb1

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require_relative 'compression'

module Pheme
  class TopicPublisher
    include Compression

    #
    # Constant with message size limit.
    # The message size also includes some metadata: 'name' and 'type'.
    # We give ourselves a buffer for this metadata.
    #
    # Source: https://docs.aws.amazon.com/sns/latest/dg/SNSMessageAttributes.html#SNSMessageAttributesNTV
    #
    SNS_SIZE_LIMIT = 256.kilobytes
    EXPECTED_METADATA_SIZE = 1.kilobyte
    MESSAGE_SIZE_LIMIT = SNS_SIZE_LIMIT - EXPECTED_METADATA_SIZE

    class << self
      attr_reader :_topic_arn

      def topic_arn(topic_arn)
        @_topic_arn = topic_arn
      end
    end

    def initialize(topic_arn: self.class._topic_arn)
      raise ArgumentError, "must specify non-nil topic_arn" unless topic_arn.present?

      @topic_arn = topic_arn
    end
    attr_accessor :topic_arn

    def publish_events
      raise NotImplementedError
    end

    def publish(message)
      payload = {
        message: "#{self.class} publishing message to #{topic_arn}",
        body: message,
        publisher: self.class.to_s,
        topic_arn: topic_arn,
      }
      Pheme.logger.info(payload.to_json)
      Pheme.configuration.sns_client.publish(topic_arn: topic_arn, message: serialize(message))
    end

    def serialize(message)
      message = message.to_json unless message.is_a? String

      return compress(message) if message.bytesize > MESSAGE_SIZE_LIMIT

      message
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pheme-3.0.1 lib/pheme/topic_publisher.rb
pheme-3.0.0 lib/pheme/topic_publisher.rb
pheme-1.2.2 lib/pheme/topic_publisher.rb