class Liam::Producer

Constants

DEFAULT_SUBJECT
SKIPPED_MESSAGE
UNSUPPORTED_MESSAGE_ERROR
UNSUPPORTED_TOPIC_ERROR

Attributes

message[R]
options[R]
topic[R]

Public Class Methods

message(**args) click to toggle source
# File lib/liam/producer.rb, line 26
def self.message(**args)
  new(**args).send(:execute)
end

Private Class Methods

new(message:, topic:, options: {}) click to toggle source
# File lib/liam/producer.rb, line 20
def initialize(message:, topic:, options: {})
  @message = message
  @topic = topic
  @options = options
end

Private Instance Methods

execute() click to toggle source
# File lib/liam/producer.rb, line 36
def execute
  raise NoConfigForEnvError unless valid_config?
  return SKIPPED_MESSAGE if skipped?
  return UNSUPPORTED_TOPIC_ERROR unless supported_topic?
  return UNSUPPORTED_MESSAGE_ERROR unless message.is_a?(Hash)

  Liam.logger.info "Publishing message: #{message}"
  Aws::SNS::Client.new(client_options).publish(
    topic_arn: topic_arn,
    message: message.to_json,
    subject: options['subject'] || options[:subject] || DEFAULT_SUBJECT,
    message_attributes: message_attributes
  )
end
message_attributes() click to toggle source
# File lib/liam/producer.rb, line 55
def message_attributes
  { event_name: { string_value: topic, data_type: 'String' } }
end
skipped?() click to toggle source
# File lib/liam/producer.rb, line 69
def skipped?
  !!env_credentials['skip']
end
supported_topic?() click to toggle source
# File lib/liam/producer.rb, line 51
def supported_topic?
  (topic.is_a?(String) || topic.is_a?(Symbol)) && !topic.empty?
end
topic_arn() click to toggle source
# File lib/liam/producer.rb, line 59
def topic_arn
  raise NoTopicsInConfigFileError unless topics

  topics[topic]
end
topics() click to toggle source
# File lib/liam/producer.rb, line 73
def topics
  @topics ||= env_credentials['events']
end
valid_config?() click to toggle source
# File lib/liam/producer.rb, line 65
def valid_config?
  env_credentials && !env_credentials.empty?
end