Class: Pheme::TopicPublisher
- Inherits:
-
Object
- Object
- Pheme::TopicPublisher
- 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
-
#topic_arn ⇒ Object
Returns the value of attribute topic_arn.
Instance Method Summary collapse
-
#initialize(topic_arn:) ⇒ TopicPublisher
constructor
A new instance of TopicPublisher.
- #publish(message) ⇒ Object
- #publish_events ⇒ Object
- #serialize(message) ⇒ Object
Methods included from Compression
Constructor Details
#initialize(topic_arn:) ⇒ TopicPublisher
Returns a new instance of TopicPublisher.
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_arn ⇒ Object
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() payload = { message: "#{self.class} publishing message to #{topic_arn}", body: , 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()) end |
#publish_events ⇒ Object
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() = .to_json unless .is_a? String return compress() if .bytesize > MESSAGE_SIZE_LIMIT end |