Sha256: 3541d67a28b38cafe82b6492a083a9b0daf871e8eb3f0865d3180a319308d732

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'aws-sdk-sns'
require 'forwardable'

module Liam
  class Producer
    DEFAULT_SUBJECT = 'liam message'
    UNSUPPORTED_MESSAGE_ERROR = 'Unsupported message argument'
    UNSUPPORTED_TOPIC_ERROR = 'Unsupported topic argument'
    private_constant :DEFAULT_SUBJECT

    include Common

    extend Forwardable

    def initialize(message:, options: {}, topic:)
      @message = message
      @options = options
      @topic = topic
    end

    def self.message(*args)
      new(*args).send(:execute)
    end

    private

    private_class_method :new

    attr_reader :topic, :message, :options

    def execute
      return UNSUPPORTED_TOPIC_ERROR unless supported_topic?
      return UNSUPPORTED_MESSAGE_ERROR unless message.is_a?(Hash)

      puts "[aws-liam] 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

    def supported_topic?
      (topic.is_a?(String) || topic.is_a?(Symbol)) && !topic.empty?
    end

    def message_attributes
      { event_name: { string_value: topic, data_type: 'String' } }
    end

    def topic_arn
      raise NoTopicsInConfigFileError unless topics

      topics[topic]
    end

    def topics
      @topics ||= env_credentials['events']
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aws-liam-0.0.6 lib/liam/producer.rb
aws-liam-0.0.5 lib/liam/producer.rb
aws-liam-0.0.4 lib/liam/producer.rb