sns_sender 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +119 -0
- data/lib/sns_sender/client.rb +50 -0
- data/lib/sns_sender/configuration.rb +27 -0
- data/lib/sns_sender/version.rb +5 -0
- data/lib/sns_sender.rb +35 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '0005482b6cdf954da73768ce4375bdf5a56ab7e6114ebb64e2d66298dd1012dd'
|
4
|
+
data.tar.gz: a8389fca53500874eb3c1e7293e76f539901a57ea8aaee004e0850823ddd8494
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df2cb70b49129dea51de2d25b15a4cc7faf6954273f4315aaf2e1394d104e0c1e3bcf18a93506f2a548b120de61183f65211f11953dff9479e8c65ff5cca0475
|
7
|
+
data.tar.gz: 9dd59bde7378b1e019b8ae8032baa23a378fbb36dc68b75b4b991afe20484c2b6a21573519ca94d4b97a43bc60744dc617345871ea65ab6c68e92ef987183585
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
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,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "aws-sdk-sns"
|
4
|
+
|
5
|
+
module SnsSender
|
6
|
+
class Client
|
7
|
+
attr_reader :sns_client
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
SnsSender.configuration.validate!
|
11
|
+
@sns_client = Aws::SNS::Client.new(
|
12
|
+
region: SnsSender.configuration.aws_region,
|
13
|
+
credentials: Aws::Credentials.new(
|
14
|
+
SnsSender.configuration.aws_access_key_id,
|
15
|
+
SnsSender.configuration.aws_secret_access_key
|
16
|
+
)
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
def publish(topic_arn:, message:, message_attributes: {})
|
21
|
+
raise ArgumentError, "topic_arn is required" if topic_arn.nil? || topic_arn.empty?
|
22
|
+
raise ArgumentError, "message is required" if message.nil? || message.empty?
|
23
|
+
|
24
|
+
formatted_message = message.is_a?(String) ? message : message.to_json
|
25
|
+
formatted_attributes = format_message_attributes(message_attributes)
|
26
|
+
|
27
|
+
begin
|
28
|
+
response = sns_client.publish(
|
29
|
+
topic_arn: topic_arn,
|
30
|
+
message: formatted_message,
|
31
|
+
message_attributes: formatted_attributes
|
32
|
+
)
|
33
|
+
{ success: true, message_id: response.message_id }
|
34
|
+
rescue Aws::SNS::Errors::ServiceError => e
|
35
|
+
raise PublishError, "Failed to publish message: #{e.message}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def format_message_attributes(attributes)
|
42
|
+
attributes.transform_values do |value|
|
43
|
+
{
|
44
|
+
data_type: value.is_a?(Numeric) ? "Number" : "String",
|
45
|
+
string_value: value.to_s
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SnsSender
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :aws_access_key_id,
|
6
|
+
:aws_secret_access_key,
|
7
|
+
:aws_region,
|
8
|
+
:default_topic_arn
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@aws_region = ENV["AWS_REGION"]
|
12
|
+
@aws_access_key_id = ENV["AWS_ACCESS_KEY_ID"]
|
13
|
+
@aws_secret_access_key = ENV["AWS_SECRET_ACCESS_KEY"]
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate!
|
17
|
+
missing_configs = []
|
18
|
+
missing_configs << "aws_region" unless aws_region
|
19
|
+
missing_configs << "aws_access_key_id" unless aws_access_key_id
|
20
|
+
missing_configs << "aws_secret_access_key" unless aws_secret_access_key
|
21
|
+
|
22
|
+
return true if missing_configs.empty?
|
23
|
+
|
24
|
+
raise ConfigurationError, "Missing required AWS configurations: #{missing_configs.join(", ")}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/sns_sender.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "sns_sender/version"
|
4
|
+
require_relative "sns_sender/configuration"
|
5
|
+
require_relative "sns_sender/client"
|
6
|
+
|
7
|
+
module SnsSender
|
8
|
+
class Error < StandardError; end
|
9
|
+
class ConfigurationError < Error; end
|
10
|
+
class PublishError < Error; end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_writer :configuration
|
14
|
+
|
15
|
+
def configuration
|
16
|
+
@configuration ||= Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def configure
|
20
|
+
yield(configuration)
|
21
|
+
end
|
22
|
+
|
23
|
+
def publish(topic_arn: nil, message:, message_attributes: {})
|
24
|
+
Client.new.publish(
|
25
|
+
topic_arn: topic_arn || configuration.default_topic_arn,
|
26
|
+
message: message,
|
27
|
+
message_attributes: message_attributes
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
def reset
|
32
|
+
@configuration = Configuration.new
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sns_sender
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Unnikrishnan KP
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-04-21 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: aws-sdk-sns
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1.0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '1.0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '13.0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '13.0'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: rspec
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.0'
|
47
|
+
type: :development
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.0'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: rubocop
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.21'
|
61
|
+
type: :development
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.21'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: nokogiri
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.15'
|
75
|
+
type: :development
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '1.15'
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: aws-sdk-core
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.0'
|
89
|
+
type: :development
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '3.0'
|
96
|
+
description: A simple interface for publishing messages to AWS SNS topics that can
|
97
|
+
be consumed by SQS queues
|
98
|
+
email:
|
99
|
+
- unnikrishnan.kp@bigbinary.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- lib/sns_sender.rb
|
107
|
+
- lib/sns_sender/client.rb
|
108
|
+
- lib/sns_sender/configuration.rb
|
109
|
+
- lib/sns_sender/version.rb
|
110
|
+
homepage: https://github.com/bigbinary/sns_sender
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata:
|
114
|
+
homepage_uri: https://github.com/bigbinary/sns_sender
|
115
|
+
source_code_uri: https://github.com/bigbinary/sns_sender
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 3.1.0
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubygems_version: 3.6.6
|
131
|
+
specification_version: 4
|
132
|
+
summary: A Ruby gem for publishing messages to AWS SNS topics
|
133
|
+
test_files: []
|