A Ruby gem that provides a simple interface for publishing messages to AWS SNS topics, which can then be consumed by SQS queues.
Add this line to your application's Gemfile:
gem 'sns_sender'And then execute:
$ bundle install
Or install it yourself as:
$ gem install sns_sender
Configure the gem with your AWS credentials. In a Rails application, you might want to put this in an initializer:
# config/initializers/sns_sender.rb
SnsSender.configure do |config|
config.aws_region = 'us-east-1' # Required
config.aws_access_key_id = 'YOUR_ACCESS_KEY' # Required
config.aws_secret_access_key = 'YOUR_SECRET_KEY' # Required
config.default_topic_arn = 'YOUR_DEFAULT_TOPIC_ARN' # Optional
endYou can also use environment variables for AWS credentials:
- AWS_REGION
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
# Send a simple message
SnsSender.publish(
topic_arn: 'arn:aws:sns:us-east-1:123456789012:MyTopic',
message: 'Hello, World!'
)
# Send a JSON message
SnsSender.publish(
topic_arn: 'arn:aws:sns:us-east-1:123456789012:MyTopic',
message: {
event: 'user_created',
data: {
id: 123,
name: 'John Doe',
email: '[email protected]'
}
}
)
# Using the default topic (if configured)
SnsSender.publish(
message: 'Hello, World!'
)
# With message attributes
SnsSender.publish(
topic_arn: 'arn:aws:sns:us-east-1:123456789012:MyTopic',
message: 'Hello, World!',
message_attributes: {
event_type: 'greeting',
priority: 'high'
}
)The publish method returns a hash with the following structure:
{
success: true,
message_id: 'abc123...' # The SNS message ID
}The gem can raise the following errors:
begin
SnsSender.publish(topic_arn: 'arn:...', message: 'Hello')
rescue SnsSender::ConfigurationError => e
# Handle missing or invalid AWS configuration
rescue SnsSender::PublishError => e
# Handle SNS publishing errors
rescue ArgumentError => e
# Handle invalid arguments (missing topic_arn or message)
endAfter checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
Bug reports and pull requests are welcome on GitHub at https://github.com/bigbinary/sns_sender.
The gem is available as open source under the terms of the MIT License.