Skip to content

Instantly share code, notes, and snippets.

@luctus
Last active February 25, 2020 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luctus/21133f211efd96f948c86a405d052134 to your computer and use it in GitHub Desktop.
Save luctus/21133f211efd96f948c86a405d052134 to your computer and use it in GitHub Desktop.
The producer class used in Liam
# frozen_string_literal: true
# Author: Diego Acuña
module Liam
class Producer
def initialize(event, message, options = {})
@event = event
@message = message
@options = options
end
def self.publish(*args)
new(*args).send(:call)
end
private
private_class_method :new
attr_reader :message, :event, :options
def call
JSON.parse(message) # validate the message
messageSNS = Aws::SNS::Client.new(aws_options).publish({
topic_arn: Rails.configuration.liam['events'][event],
message: message,
subject: options[:subject],
message_structure: "string",
message_attributes: { "EventName" => { string_value: event, data_type: "String" } }.merge(extra_attributes)
})
{ status: :ok, message_id: messageSNS.message_id }
rescue JSON::ParserError, Seahorse::Client::NetworkingError, Aws::SNS::Errors::NotFound => e
{ status: :error, error: e }
end
def aws_options
options = {
region: AWS_REGION # Replace this!
access_key_id: ACCESS_KEY # Replace this!
secret_access_key: SECRET_ACCESS_KEY # Replace this!
}
options.merge!(endpoint: Rails.configuration.liam['aws']['sns_endpoint']) if Rails.configuration.liam['aws']['sns_endpoint']
options
end
def extra_attributes
options[:message_attributes] || {}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment