aws-liam 0.0.2
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/.aws/config +3 -0
- data/.aws/credentials +3 -0
- data/.github/workflows/main.yaml +46 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +10 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +65 -0
- data/LICENSE.txt +21 -0
- data/README.md +81 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docker-compose.localstack.yml +13 -0
- data/liam.gemspec +25 -0
- data/lib/.DS_Store +0 -0
- data/lib/generators/liam/install_generator.rb +13 -0
- data/lib/generators/liam/templates/liam.rake +12 -0
- data/lib/generators/liam/templates/liam.yml +15 -0
- data/lib/liam.rb +9 -0
- data/lib/liam/.DS_Store +0 -0
- data/lib/liam/common.rb +26 -0
- data/lib/liam/consumer.rb +43 -0
- data/lib/liam/exceptions/message_without_value_attribute_error.rb +14 -0
- data/lib/liam/exceptions/no_topics_in_config_file_error.rb +9 -0
- data/lib/liam/exceptions/unexpected_message_error.rb +9 -0
- data/lib/liam/exceptions/uninitialized_message_processor_error.rb +24 -0
- data/lib/liam/message_processor.rb +47 -0
- data/lib/liam/producer.rb +63 -0
- data/lib/liam/version.rb +3 -0
- metadata +166 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 29e5710e923fdc2f368b1ada96c2f0589ceebdc9f3a440ca449874522eba535b
|
|
4
|
+
data.tar.gz: 55d0ccd2b582926b4e739f2733cf2b1b29dd2aa7e8b1cfe6e5e9fd03d01a9b38
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '0936be1285b88074b78700ba88ace247fef5c679f6e34e70d249a3e898f7128d8b3e5447257972271c714abc05c56ebd73a85fff9bf1d99d05d7ae1f77f01115'
|
|
7
|
+
data.tar.gz: 4bf722b61c044610637a8deef09c844c6a4464e6f299ae46bd30ceeda3ac851d5e3b5561a3277bed3be91200b49418e1962554d79b90d3a0865a8004c1ab9e92
|
data/.aws/config
ADDED
data/.aws/credentials
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
name: Main
|
|
2
|
+
|
|
3
|
+
on: [pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
vm-job:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
strategy:
|
|
9
|
+
max-parallel: 4
|
|
10
|
+
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v1
|
|
13
|
+
|
|
14
|
+
- name: Set up Python 3.7
|
|
15
|
+
uses: actions/setup-python@v1
|
|
16
|
+
with:
|
|
17
|
+
python_version: 3.7
|
|
18
|
+
|
|
19
|
+
- name: Set up Ruby 2.6
|
|
20
|
+
uses: actions/setup-ruby@v1
|
|
21
|
+
with:
|
|
22
|
+
ruby_version: 2.6.3
|
|
23
|
+
|
|
24
|
+
- name: Set up pip
|
|
25
|
+
run: python -m pip install --upgrade pip
|
|
26
|
+
|
|
27
|
+
- name: Set up LocalStack
|
|
28
|
+
run: |
|
|
29
|
+
docker-compose -f docker-compose.localstack.yml up -d
|
|
30
|
+
curl "https://d1vvhvl2y92vvt.cloudfront.net/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
|
|
31
|
+
unzip -q awscliv2.zip
|
|
32
|
+
sudo ./aws/install
|
|
33
|
+
cp -avr .aws/ ~/
|
|
34
|
+
while ! curl -s http://localhost:4575 > /dev/null; do echo waiting for localstack; sleep 3; done;
|
|
35
|
+
aws --endpoint-url=http://localhost:4575 sns create-topic --name liam_TestProducer
|
|
36
|
+
|
|
37
|
+
- name: Build
|
|
38
|
+
env:
|
|
39
|
+
RAILS_ENV: test
|
|
40
|
+
run: |
|
|
41
|
+
gem install bundler -v 1.16
|
|
42
|
+
bundle _1.16_ install --jobs 4 --retry 3
|
|
43
|
+
|
|
44
|
+
- name: Run tests
|
|
45
|
+
run: |
|
|
46
|
+
RAILS_ENV=test bundle exec rspec -fd
|
data/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.gem
|
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at alexis.mansilla04@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
aws-liam (1.0.0)
|
|
5
|
+
aws-sdk-sns (~> 1.21)
|
|
6
|
+
aws-sdk-sqs (~> 1.23, >= 1.23.1)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
addressable (2.7.0)
|
|
12
|
+
public_suffix (>= 2.0.2, < 5.0)
|
|
13
|
+
aws-eventstream (1.0.3)
|
|
14
|
+
aws-partitions (1.269.0)
|
|
15
|
+
aws-sdk-core (3.89.1)
|
|
16
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
|
17
|
+
aws-partitions (~> 1, >= 1.239.0)
|
|
18
|
+
aws-sigv4 (~> 1.1)
|
|
19
|
+
jmespath (~> 1.0)
|
|
20
|
+
aws-sdk-sns (1.21.0)
|
|
21
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
|
22
|
+
aws-sigv4 (~> 1.1)
|
|
23
|
+
aws-sdk-sqs (1.23.1)
|
|
24
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
|
25
|
+
aws-sigv4 (~> 1.1)
|
|
26
|
+
aws-sigv4 (1.1.0)
|
|
27
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
|
28
|
+
crack (0.4.3)
|
|
29
|
+
safe_yaml (~> 1.0.0)
|
|
30
|
+
diff-lcs (1.3)
|
|
31
|
+
hashdiff (1.0.0)
|
|
32
|
+
jmespath (1.4.0)
|
|
33
|
+
public_suffix (4.0.3)
|
|
34
|
+
rake (10.5.0)
|
|
35
|
+
rspec (3.9.0)
|
|
36
|
+
rspec-core (~> 3.9.0)
|
|
37
|
+
rspec-expectations (~> 3.9.0)
|
|
38
|
+
rspec-mocks (~> 3.9.0)
|
|
39
|
+
rspec-core (3.9.1)
|
|
40
|
+
rspec-support (~> 3.9.1)
|
|
41
|
+
rspec-expectations (3.9.0)
|
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
43
|
+
rspec-support (~> 3.9.0)
|
|
44
|
+
rspec-mocks (3.9.1)
|
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
46
|
+
rspec-support (~> 3.9.0)
|
|
47
|
+
rspec-support (3.9.2)
|
|
48
|
+
safe_yaml (1.0.5)
|
|
49
|
+
webmock (3.7.6)
|
|
50
|
+
addressable (>= 2.3.6)
|
|
51
|
+
crack (>= 0.3.2)
|
|
52
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
|
53
|
+
|
|
54
|
+
PLATFORMS
|
|
55
|
+
ruby
|
|
56
|
+
|
|
57
|
+
DEPENDENCIES
|
|
58
|
+
aws-liam!
|
|
59
|
+
bundler (~> 1.16)
|
|
60
|
+
rake (~> 10.0)
|
|
61
|
+
rspec (~> 3.9.0)
|
|
62
|
+
webmock (~> 3.7.6)
|
|
63
|
+
|
|
64
|
+
BUNDLED WITH
|
|
65
|
+
1.17.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 alexismansilla
|
|
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,81 @@
|
|
|
1
|
+
# Welcome to Liam Gem!
|
|
2
|
+
|
|
3
|
+
Liam is an integration between your (Ruby on Rails) App and SNS, SQS AWS Services. It allows sending a message to the AWS queue (SQS) and then processing the topic name on other (Ruby on Rails) App. (microservices)
|
|
4
|
+
|
|
5
|
+
We called it Liam in honor to (Liam Neeson) for so many reasons!
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
Liam sends an important message to Marko from Tropoja
|
|
10
|
+
|
|
11
|
+
> *I DONT KNOW WHO YOU ARE, I DON’T KNOW WHAT YOU WANT, BUT I WILL FIND YOU AND I WILL KILL YOU*...
|
|
12
|
+
|
|
13
|
+
We have a medium post [Here](https://medium.com/archdaily-on-technology/microservices-events-aws-our-path-to-improve-communication-between-our-ruby-on-rails-apps-501b65e35fa3) on how we improved communication between our (Ruby on Rails) apps and avoided creating new endpoints in our apps.
|
|
14
|
+
But what happens when the App A needs data from App B and App B is down for a microsecond?
|
|
15
|
+
|
|
16
|
+
**Welcome, Liam!**
|
|
17
|
+
|
|
18
|
+
With Liam we can send a message easily to SQS Service through SNS Service and process the message data through the consumer that Liam has
|
|
19
|
+
|
|
20
|
+
The first thing:
|
|
21
|
+
|
|
22
|
+
- *What's SNS Service?*
|
|
23
|
+
Amazon Simple Notification Service (SNS) [Documentation](https://docs.aws.amazon.com/sns/latest/dg/welcome.html)
|
|
24
|
+
|
|
25
|
+
- *What's SQS Service?*
|
|
26
|
+
Amazon Simple Queue Service (SQS) [Documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html)
|
|
27
|
+
|
|
28
|
+

|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
Add this line to your application's Gemfile:
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
gem 'liam', git: 'https://github.com/alexismansilla/liam.git'
|
|
36
|
+
|
|
37
|
+
$ bundle install
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
If you want the liam file in your rails app then execute:
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
$ bundle exec rails install:liam
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
We're going to create 2 Liam files
|
|
47
|
+
|
|
48
|
+
1. /config/liam.yaml
|
|
49
|
+
2. /lib/task/liam.rake
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
* Producer Message
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
message = { id: self.id, title: self.title, created_at: self.created_at }.to_json
|
|
57
|
+
topic_name = 'liam_CreateArticle'
|
|
58
|
+
|
|
59
|
+
Liam::Producer.message(topic: topic_name, message: message)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
* Consumer Message
|
|
63
|
+
```ruby
|
|
64
|
+
Liam::Consumer.message
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Testing
|
|
68
|
+
Can you run the test easily executing
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
bundle exec rspec
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
if you want to run all test successfully, we need the LocalStack daemon when sending a message
|
|
75
|
+
|
|
76
|
+
## Localstack Running
|
|
77
|
+
TODO
|
|
78
|
+
|
|
79
|
+
## Contributing
|
|
80
|
+
|
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/alexismansilla/liam.
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "liam"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
version: '3'
|
|
2
|
+
|
|
3
|
+
services:
|
|
4
|
+
localstack:
|
|
5
|
+
image: localstack/localstack
|
|
6
|
+
ports:
|
|
7
|
+
- "4567-4597:4567-4597"
|
|
8
|
+
environment:
|
|
9
|
+
- SERVICES=sqs,sns
|
|
10
|
+
- DOCKER_HOST=unix:///var/run/docker.sock
|
|
11
|
+
volumes:
|
|
12
|
+
- "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
|
|
13
|
+
- "/var/run/docker.sock:/var/run/docker.sock"
|
data/liam.gemspec
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
|
|
6
|
+
require 'liam/version'
|
|
7
|
+
|
|
8
|
+
Gem::Specification.new do |spec|
|
|
9
|
+
spec.name = 'aws-liam'
|
|
10
|
+
spec.version = Liam::VERSION
|
|
11
|
+
spec.authors = ['alexismansilla', 'sebastian-palma']
|
|
12
|
+
spec.email = ['alexis.mansilla04@gmail.com', 'sebastianpalma@protonmail.com']
|
|
13
|
+
spec.summary = 'AWS SQS+SNS middleware integration between Ruby microservices'
|
|
14
|
+
spec.description = 'Improved communication between our (Ruby on Rails) apps with AWS SNS - SQS'
|
|
15
|
+
spec.homepage = 'http://github.com/archdaily/aws-liam'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
|
18
|
+
spec.require_paths = ['lib']
|
|
19
|
+
spec.add_dependency 'aws-sdk-sns', '~> 1.21'
|
|
20
|
+
spec.add_dependency 'aws-sdk-sqs', '~> 1.23', '>= 1.23.1'
|
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.9.0'
|
|
24
|
+
spec.add_development_dependency 'webmock', '~> 3.7.6'
|
|
25
|
+
end
|
data/lib/.DS_Store
ADDED
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liam
|
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
|
5
|
+
source_root File.expand_path('../templates/', __FILE__)
|
|
6
|
+
desc 'Generates the config files needed to make Liam work.'
|
|
7
|
+
|
|
8
|
+
def copy_settings
|
|
9
|
+
copy_file 'liam.yml', 'config/liam.yml'
|
|
10
|
+
copy_file 'liam.rake', 'lib/tasks/liam.rake'
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
data/lib/liam.rb
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'liam/version'
|
|
4
|
+
require 'liam/consumer'
|
|
5
|
+
require 'liam/producer'
|
|
6
|
+
require 'liam/exceptions/message_without_value_attribute_error'
|
|
7
|
+
require 'liam/exceptions/uninitialized_message_processor_error'
|
|
8
|
+
require 'liam/exceptions/no_topics_in_config_file_error'
|
|
9
|
+
require 'liam/exceptions/unexpected_message_error'
|
data/lib/liam/.DS_Store
ADDED
|
Binary file
|
data/lib/liam/common.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'yaml'
|
|
4
|
+
|
|
5
|
+
module Liam
|
|
6
|
+
module Common
|
|
7
|
+
CONFIG_FILE = File.expand_path('config/liam.yml')
|
|
8
|
+
|
|
9
|
+
def client_options
|
|
10
|
+
{
|
|
11
|
+
access_key_id: env_credentials['aws']['access_key_id'],
|
|
12
|
+
endpoint: env_credentials['aws']['sns']['endpoint'],
|
|
13
|
+
region: env_credentials['aws']['region'],
|
|
14
|
+
secret_access_key: env_credentials['aws']['secret_access_key']
|
|
15
|
+
}.compact
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def env_credentials
|
|
19
|
+
@env_credentials ||= credentials[ENV['RAILS_ENV']]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def credentials
|
|
23
|
+
YAML.load_file(CONFIG_FILE)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'aws-sdk-sqs'
|
|
4
|
+
require 'liam/common'
|
|
5
|
+
require 'liam/message_processor'
|
|
6
|
+
|
|
7
|
+
module Liam
|
|
8
|
+
class Consumer
|
|
9
|
+
include Common
|
|
10
|
+
|
|
11
|
+
def initialize(options: {})
|
|
12
|
+
@options = options
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.message(*args)
|
|
16
|
+
new(*args).send(:execute)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
attr_reader :options
|
|
22
|
+
|
|
23
|
+
def execute
|
|
24
|
+
poller.poll(poller_options) do |messages|
|
|
25
|
+
messages.each do |message|
|
|
26
|
+
MessageProcessor.process(message)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def poller
|
|
32
|
+
Aws::SQS::QueuePoller.new(sqs_queue, client: Aws::SQS::Client.new(client_options))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def sqs_queue
|
|
36
|
+
env_credentials.dig('aws', 'sqs', 'queue')
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def poller_options
|
|
40
|
+
{ max_number_of_messages: options['max_message'] || options[:max_message] || 10 }
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liam
|
|
4
|
+
class MessageWithoutValueAttributeError < StandardError
|
|
5
|
+
def initialize
|
|
6
|
+
super(
|
|
7
|
+
<<~MSG.gsub(/\n/, '')
|
|
8
|
+
Expected to get a message attribute value to initialize the class to process
|
|
9
|
+
this message, but the value received is invalid.
|
|
10
|
+
MSG
|
|
11
|
+
)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liam
|
|
4
|
+
class UninitializedMessageProcessorError < StandardError
|
|
5
|
+
def initialize(error)
|
|
6
|
+
@error = error
|
|
7
|
+
super(
|
|
8
|
+
<<~MSG.gsub(/\n/, '')
|
|
9
|
+
Expected file #{class_name}
|
|
10
|
+
defined in app/services/liam to process the message,
|
|
11
|
+
but it has not been initialized.
|
|
12
|
+
MSG
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
attr_reader :error
|
|
19
|
+
|
|
20
|
+
def class_name
|
|
21
|
+
error.to_s[/[\w+::+\w+]+(?=\z)/]
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'forwardable'
|
|
5
|
+
|
|
6
|
+
module Liam
|
|
7
|
+
class MessageProcessor
|
|
8
|
+
extend Forwardable
|
|
9
|
+
|
|
10
|
+
def initialize(message)
|
|
11
|
+
@message = message
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.process(message)
|
|
15
|
+
raise UnexpectedMessageError, message unless message.is_a?(Aws::SQS::Types::Message)
|
|
16
|
+
|
|
17
|
+
new(message).send(:process)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :message
|
|
23
|
+
|
|
24
|
+
private(*def_delegator(:message, :body))
|
|
25
|
+
private(*def_delegator(:processor, :process))
|
|
26
|
+
|
|
27
|
+
def parsed_message
|
|
28
|
+
JSON.parse(body)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def processor
|
|
32
|
+
Object.const_get(message_topic_name).new(parsed_message['Message'])
|
|
33
|
+
rescue NameError => e
|
|
34
|
+
raise UninitializedMessageProcessorError, e
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def message_topic_name
|
|
38
|
+
message_attribute_value.sub('_', '::').gsub(/(?<=^)(.*)(?=::)/, &:capitalize)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def message_attribute_value
|
|
42
|
+
parsed_message.dig('MessageAttributes', 'event_name', 'Value').tap do |value|
|
|
43
|
+
raise MessageWithoutValueAttributeError unless value
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'aws-sdk-sns'
|
|
4
|
+
require 'forwardable'
|
|
5
|
+
|
|
6
|
+
module Liam
|
|
7
|
+
class Producer
|
|
8
|
+
DEFAULT_SUBJECT = 'liam message'
|
|
9
|
+
UNSUPPORTED_MESSAGE_ERROR = 'Unsupported message argument'
|
|
10
|
+
UNSUPPORTED_TOPIC_ERROR = 'Unsupported topic argument'
|
|
11
|
+
private_constant :DEFAULT_SUBJECT
|
|
12
|
+
|
|
13
|
+
include Common
|
|
14
|
+
|
|
15
|
+
extend Forwardable
|
|
16
|
+
|
|
17
|
+
def initialize(message:, options: {}, topic:)
|
|
18
|
+
@message = message
|
|
19
|
+
@options = options
|
|
20
|
+
@topic = topic
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.message(*args)
|
|
24
|
+
new(*args).send(:execute)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
private_class_method :new
|
|
30
|
+
|
|
31
|
+
attr_reader :topic, :message, :options
|
|
32
|
+
|
|
33
|
+
def execute
|
|
34
|
+
return UNSUPPORTED_TOPIC_ERROR unless supported_topic?
|
|
35
|
+
return UNSUPPORTED_MESSAGE_ERROR unless message.is_a?(Hash)
|
|
36
|
+
|
|
37
|
+
Aws::SNS::Client.new(client_options).publish(
|
|
38
|
+
topic_arn: topic_arn,
|
|
39
|
+
message: message.to_json,
|
|
40
|
+
subject: options['subject'] || options[:subject] || DEFAULT_SUBJECT,
|
|
41
|
+
message_attributes: message_attributes
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def supported_topic?
|
|
46
|
+
(topic.is_a?(String) || topic.is_a?(Symbol)) && !topic.empty?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def message_attributes
|
|
50
|
+
{ event_name: { string_value: topic, data_type: 'String' } }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def topic_arn
|
|
54
|
+
raise NoTopicsInConfigFileError unless topics
|
|
55
|
+
|
|
56
|
+
topics[topic]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def topics
|
|
60
|
+
@topics ||= env_credentials['events']
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/liam/version.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: aws-liam
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- alexismansilla
|
|
8
|
+
- sebastian-palma
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2020-02-17 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: aws-sdk-sns
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - "~>"
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: '1.21'
|
|
21
|
+
type: :runtime
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - "~>"
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '1.21'
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: aws-sdk-sqs
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - "~>"
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '1.23'
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: 1.23.1
|
|
38
|
+
type: :runtime
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - "~>"
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '1.23'
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 1.23.1
|
|
48
|
+
- !ruby/object:Gem::Dependency
|
|
49
|
+
name: bundler
|
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.16'
|
|
55
|
+
type: :development
|
|
56
|
+
prerelease: false
|
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '1.16'
|
|
62
|
+
- !ruby/object:Gem::Dependency
|
|
63
|
+
name: rake
|
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
type: :development
|
|
70
|
+
prerelease: false
|
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '10.0'
|
|
76
|
+
- !ruby/object:Gem::Dependency
|
|
77
|
+
name: rspec
|
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 3.9.0
|
|
83
|
+
type: :development
|
|
84
|
+
prerelease: false
|
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 3.9.0
|
|
90
|
+
- !ruby/object:Gem::Dependency
|
|
91
|
+
name: webmock
|
|
92
|
+
requirement: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 3.7.6
|
|
97
|
+
type: :development
|
|
98
|
+
prerelease: false
|
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: 3.7.6
|
|
104
|
+
description: Improved communication between our (Ruby on Rails) apps with AWS SNS
|
|
105
|
+
- SQS
|
|
106
|
+
email:
|
|
107
|
+
- alexis.mansilla04@gmail.com
|
|
108
|
+
- sebastianpalma@protonmail.com
|
|
109
|
+
executables: []
|
|
110
|
+
extensions: []
|
|
111
|
+
extra_rdoc_files: []
|
|
112
|
+
files:
|
|
113
|
+
- ".aws/config"
|
|
114
|
+
- ".aws/credentials"
|
|
115
|
+
- ".github/workflows/main.yaml"
|
|
116
|
+
- ".gitignore"
|
|
117
|
+
- ".rubocop.yml"
|
|
118
|
+
- CODE_OF_CONDUCT.md
|
|
119
|
+
- Gemfile
|
|
120
|
+
- Gemfile.lock
|
|
121
|
+
- LICENSE.txt
|
|
122
|
+
- README.md
|
|
123
|
+
- Rakefile
|
|
124
|
+
- bin/console
|
|
125
|
+
- bin/setup
|
|
126
|
+
- docker-compose.localstack.yml
|
|
127
|
+
- liam.gemspec
|
|
128
|
+
- lib/.DS_Store
|
|
129
|
+
- lib/generators/liam/install_generator.rb
|
|
130
|
+
- lib/generators/liam/templates/liam.rake
|
|
131
|
+
- lib/generators/liam/templates/liam.yml
|
|
132
|
+
- lib/liam.rb
|
|
133
|
+
- lib/liam/.DS_Store
|
|
134
|
+
- lib/liam/common.rb
|
|
135
|
+
- lib/liam/consumer.rb
|
|
136
|
+
- lib/liam/exceptions/message_without_value_attribute_error.rb
|
|
137
|
+
- lib/liam/exceptions/no_topics_in_config_file_error.rb
|
|
138
|
+
- lib/liam/exceptions/unexpected_message_error.rb
|
|
139
|
+
- lib/liam/exceptions/uninitialized_message_processor_error.rb
|
|
140
|
+
- lib/liam/message_processor.rb
|
|
141
|
+
- lib/liam/producer.rb
|
|
142
|
+
- lib/liam/version.rb
|
|
143
|
+
homepage: http://github.com/archdaily/aws-liam
|
|
144
|
+
licenses:
|
|
145
|
+
- MIT
|
|
146
|
+
metadata: {}
|
|
147
|
+
post_install_message:
|
|
148
|
+
rdoc_options: []
|
|
149
|
+
require_paths:
|
|
150
|
+
- lib
|
|
151
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
|
+
requirements:
|
|
153
|
+
- - ">="
|
|
154
|
+
- !ruby/object:Gem::Version
|
|
155
|
+
version: '0'
|
|
156
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
|
+
requirements:
|
|
158
|
+
- - ">="
|
|
159
|
+
- !ruby/object:Gem::Version
|
|
160
|
+
version: '0'
|
|
161
|
+
requirements: []
|
|
162
|
+
rubygems_version: 3.0.3
|
|
163
|
+
signing_key:
|
|
164
|
+
specification_version: 4
|
|
165
|
+
summary: AWS SQS+SNS middleware integration between Ruby microservices
|
|
166
|
+
test_files: []
|