Class: Pheme::TopicPublisher

Inherits:
Object
  • Object
show all
Includes:
Compression
Defined in:
lib/pheme/topic_publisher.rb

Constant Summary collapse

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

256.kilobytes
EXPECTED_METADATA_SIZE =
1.kilobyte
MESSAGE_SIZE_LIMIT =
SNS_SIZE_LIMIT - EXPECTED_METADATA_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Compression

#compress, #decompress

Constructor Details

#initialize(topic_arn:) ⇒ TopicPublisher

Returns a new instance of TopicPublisher.

Raises:

  • (ArgumentError)


20
21
22
23
# File 'lib/pheme/topic_publisher.rb', line 20

def initialize(topic_arn:)
  raise ArgumentError, "must specify non-nil topic_arn" unless topic_arn.present?
  @topic_arn = topic_arn
end

Instance Attribute Details

#topic_arnObject

Returns the value of attribute topic_arn.



18
19
20
# File 'lib/pheme/topic_publisher.rb', line 18

def topic_arn
  @topic_arn
end

Instance Method Details

#publish(message) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/pheme/topic_publisher.rb', line 29

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

#publish_eventsObject

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/pheme/topic_publisher.rb', line 25

def publish_events
  raise NotImplementedError
end

#serialize(message) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/pheme/topic_publisher.rb', line 40

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

  return compress(message) if message.bytesize > MESSAGE_SIZE_LIMIT

  message
end