|
| 1 | +# SNS Sender |
| 2 | + |
| 3 | +A Ruby gem that provides a simple interface for publishing messages to AWS SNS topics, which can then be consumed by SQS queues. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add this line to your application's Gemfile: |
| 8 | + |
| 9 | +```ruby |
| 10 | +gem 'sns_sender' |
| 11 | +``` |
| 12 | + |
| 13 | +And then execute: |
| 14 | + |
| 15 | + $ bundle install |
| 16 | + |
| 17 | +Or install it yourself as: |
| 18 | + |
| 19 | + $ gem install sns_sender |
| 20 | + |
| 21 | +## Configuration |
| 22 | + |
| 23 | +Configure the gem with your AWS credentials. In a Rails application, you might want to put this in an initializer: |
| 24 | + |
| 25 | +```ruby |
| 26 | +# config/initializers/sns_sender.rb |
| 27 | +SnsSender.configure do |config| |
| 28 | + config.aws_region = 'us-east-1' # Required |
| 29 | + config.aws_access_key_id = 'YOUR_ACCESS_KEY' # Required |
| 30 | + config.aws_secret_access_key = 'YOUR_SECRET_KEY' # Required |
| 31 | + config.default_topic_arn = 'YOUR_DEFAULT_TOPIC_ARN' # Optional |
| 32 | +end |
| 33 | +``` |
| 34 | + |
| 35 | +You can also use environment variables for AWS credentials: |
| 36 | +- AWS_REGION |
| 37 | +- AWS_ACCESS_KEY_ID |
| 38 | +- AWS_SECRET_ACCESS_KEY |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +### Basic Usage |
| 43 | + |
| 44 | +```ruby |
| 45 | +# Send a simple message |
| 46 | +SnsSender.publish( |
| 47 | + topic_arn: 'arn:aws:sns:us-east-1:123456789012:MyTopic', |
| 48 | + message: 'Hello, World!' |
| 49 | +) |
| 50 | + |
| 51 | +# Send a JSON message |
| 52 | +SnsSender.publish( |
| 53 | + topic_arn: 'arn:aws:sns:us-east-1:123456789012:MyTopic', |
| 54 | + message: { |
| 55 | + event: 'user_created', |
| 56 | + data: { |
| 57 | + id: 123, |
| 58 | + name: 'John Doe', |
| 59 | + email: 'john@example.com' |
| 60 | + } |
| 61 | + } |
| 62 | +) |
| 63 | + |
| 64 | +# Using the default topic (if configured) |
| 65 | +SnsSender.publish( |
| 66 | + message: 'Hello, World!' |
| 67 | +) |
| 68 | + |
| 69 | +# With message attributes |
| 70 | +SnsSender.publish( |
| 71 | + topic_arn: 'arn:aws:sns:us-east-1:123456789012:MyTopic', |
| 72 | + message: 'Hello, World!', |
| 73 | + message_attributes: { |
| 74 | + event_type: 'greeting', |
| 75 | + priority: 'high' |
| 76 | + } |
| 77 | +) |
| 78 | +``` |
| 79 | + |
| 80 | +### Response Format |
| 81 | + |
| 82 | +The `publish` method returns a hash with the following structure: |
| 83 | + |
| 84 | +```ruby |
| 85 | +{ |
| 86 | + success: true, |
| 87 | + message_id: 'abc123...' # The SNS message ID |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Error Handling |
| 92 | + |
| 93 | +The gem can raise the following errors: |
| 94 | + |
| 95 | +```ruby |
| 96 | +begin |
| 97 | + SnsSender.publish(topic_arn: 'arn:...', message: 'Hello') |
| 98 | +rescue SnsSender::ConfigurationError => e |
| 99 | + # Handle missing or invalid AWS configuration |
| 100 | +rescue SnsSender::PublishError => e |
| 101 | + # Handle SNS publishing errors |
| 102 | +rescue ArgumentError => e |
| 103 | + # Handle invalid arguments (missing topic_arn or message) |
| 104 | +end |
| 105 | +``` |
| 106 | + |
| 107 | +## Development |
| 108 | + |
| 109 | +After 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. |
| 110 | + |
| 111 | +To install this gem onto your local machine, run `bundle exec rake install`. |
| 112 | + |
| 113 | +## Contributing |
| 114 | + |
| 115 | +Bug reports and pull requests are welcome on GitHub at https://github.com/bigbinary/sns_sender. |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
0 commit comments