boom_nats 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4cfa0a64dc828e73223219cc03f643f20b278358f17214cf93acc4305a53b3e0
4
+ data.tar.gz: 83c1f75ecda6bb93a629cdd7d1ea47ee78ea173618c62846f8d290335bc95f2a
5
+ SHA512:
6
+ metadata.gz: 3c3b9d9a8b70867d1a0530c2addd6fb715023127a21e9c96b1c87187dd6e9213585e813bf61972f2dba2d78df7f8271b159b610d559272783bbc8f285e185aed
7
+ data.tar.gz: 0c01d74a16b7489d4090cd423666d7409f423da1b874d441472f63ef9859f9682bb5dbf9392032371340a43691e25b184d277beb153dc2e8e8c3541289ef03b0
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ # EditorConfig is awesome: https://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ [*]
7
+ indent_style = space
8
+ indent_size = 2
9
+ end_of_line = lf
10
+ charset = utf-8
11
+ trim_trailing_whitespace = false
12
+ insert_final_newline = false
@@ -0,0 +1,24 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ services:
9
+ nats:
10
+ image: nats
11
+ ports:
12
+ - 4222
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Set up Ruby
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: 3.0.2
19
+ bundler-cache: true
20
+ - name: Run the default task
21
+ env:
22
+ NATS_PORT: "${{ job.services.nats.ports[4222] }}"
23
+ NATS_HOST: localhost
24
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ .DS_Store
2
+
3
+ /.bundle/
4
+ /.yardoc
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ # Coverage reports
16
+ coverage
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.5
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## [Unreleased]
2
+
3
+ - Standalone version
4
+ - Rails based version
5
+
6
+ ## [0.1.0] - 2021-09-25
7
+
8
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at welington.sampaio@icloud.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in boom_nats.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
13
+
14
+ gem "debug", "~> 1.1"
15
+
16
+ gem "pry", "~> 0.14.1"
17
+
18
+ gem "simplecov", "~> 0.21.2"
data/Gemfile.lock ADDED
@@ -0,0 +1,103 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ boom_nats (0.1.0)
5
+ activesupport (>= 2.1)
6
+ concurrent-ruby-edge (~> 0.6, < 1.0)
7
+ nats (>= 0.11.0, < 1.0)
8
+ rack (~> 2.0, >= 2.0.9)
9
+ zeitwerk (>= 2.0, < 3.0)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (6.1.4.1)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ tzinfo (~> 2.0)
19
+ zeitwerk (~> 2.3)
20
+ ast (2.4.2)
21
+ coderay (1.1.3)
22
+ concurrent-ruby (1.1.9)
23
+ concurrent-ruby-edge (0.6.0)
24
+ concurrent-ruby (~> 1.1.6)
25
+ debug (1.1.0)
26
+ irb
27
+ reline (>= 0.2.7)
28
+ diff-lcs (1.4.4)
29
+ docile (1.4.0)
30
+ eventmachine (1.2.7)
31
+ i18n (1.8.10)
32
+ concurrent-ruby (~> 1.0)
33
+ io-console (0.5.9)
34
+ irb (1.3.7)
35
+ reline (>= 0.2.7)
36
+ method_source (1.0.0)
37
+ minitest (5.14.4)
38
+ nats (0.11.0)
39
+ eventmachine (~> 1.2, >= 1.2)
40
+ parallel (1.21.0)
41
+ parser (3.0.2.0)
42
+ ast (~> 2.4.1)
43
+ pry (0.14.1)
44
+ coderay (~> 1.1)
45
+ method_source (~> 1.0)
46
+ rack (2.2.3)
47
+ rainbow (3.0.0)
48
+ rake (13.0.6)
49
+ regexp_parser (2.1.1)
50
+ reline (0.2.7)
51
+ io-console (~> 0.5)
52
+ rexml (3.2.5)
53
+ rspec (3.10.0)
54
+ rspec-core (~> 3.10.0)
55
+ rspec-expectations (~> 3.10.0)
56
+ rspec-mocks (~> 3.10.0)
57
+ rspec-core (3.10.1)
58
+ rspec-support (~> 3.10.0)
59
+ rspec-expectations (3.10.1)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.10.0)
62
+ rspec-mocks (3.10.2)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.10.0)
65
+ rspec-support (3.10.2)
66
+ rubocop (1.21.0)
67
+ parallel (~> 1.10)
68
+ parser (>= 3.0.0.0)
69
+ rainbow (>= 2.2.2, < 4.0)
70
+ regexp_parser (>= 1.8, < 3.0)
71
+ rexml
72
+ rubocop-ast (>= 1.9.1, < 2.0)
73
+ ruby-progressbar (~> 1.7)
74
+ unicode-display_width (>= 1.4.0, < 3.0)
75
+ rubocop-ast (1.11.0)
76
+ parser (>= 3.0.1.1)
77
+ ruby-progressbar (1.11.0)
78
+ simplecov (0.21.2)
79
+ docile (~> 1.1)
80
+ simplecov-html (~> 0.11)
81
+ simplecov_json_formatter (~> 0.1)
82
+ simplecov-html (0.12.3)
83
+ simplecov_json_formatter (0.1.3)
84
+ tzinfo (2.0.4)
85
+ concurrent-ruby (~> 1.0)
86
+ unicode-display_width (2.1.0)
87
+ zeitwerk (2.4.2)
88
+
89
+ PLATFORMS
90
+ x86_64-darwin-20
91
+ x86_64-linux
92
+
93
+ DEPENDENCIES
94
+ boom_nats!
95
+ debug (~> 1.1)
96
+ pry (~> 0.14.1)
97
+ rake (~> 13.0)
98
+ rspec (~> 3.0)
99
+ rubocop (~> 1.7)
100
+ simplecov (~> 0.21.2)
101
+
102
+ BUNDLED WITH
103
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Welington Sampaio
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,146 @@
1
+ # BoomNats
2
+
3
+ NATS is a simple, secure and performant communications system for digital systems, services and devices. NATS is part of the Cloud Native Computing Foundation (CNCF). See more on [Github NATS](https://github.com/nats-io/nats-server).
4
+
5
+ This implementation works in both modes: Standalone and Rails based applications.
6
+
7
+ ## Documentation
8
+ - [Configurations](#configurations)
9
+ - [Rails](#rails)
10
+ - [Installing](#rails-installing)
11
+ - [Configuration](#rails-configuration)
12
+ - [Standalone](#standalone)
13
+ - [Installing](#standalone-installing)
14
+ - [Configuration](#standalone-configuration)
15
+ - [Starting Application](#standalone-starting-application)
16
+ ## configurations
17
+
18
+ ```ruby
19
+ require 'boom_nats'
20
+ # require_relative "consumers/...."
21
+
22
+ BoomNats.setup do
23
+ # basic NATS service
24
+ servers "nats://127.0.0.1:4222"
25
+
26
+ # Clustering NATS
27
+ servers ["nats://nats1:4222", "nats://nats2:4222"]
28
+
29
+ # with authentication
30
+ servers "nats://user:pass@localhost:4222"
31
+
32
+
33
+ # map NATS topics to Topic classes
34
+ draw_routes do
35
+ topic "topic-name", MyTopic
36
+ topic "namespace.topic", NamespacedTopic
37
+ topic "topic-queued", QueuedTopic, queue: 'queue-name'
38
+
39
+ # many topics queueds
40
+ in_queue "queue-name" do
41
+ topic "topic-queued", QueuedTopic
42
+ end
43
+
44
+ # auto unsubscribe after N messages
45
+ topic "auto-unsubscribe", TmpTopic, options: { max: 1 }
46
+ end
47
+ end
48
+
49
+ # start application
50
+ BoomNats.application.start
51
+ ```
52
+
53
+ ## Rails
54
+
55
+ To facilitate learning, you can download the default repository containing a boilerplate project with (Rails + Nats)
56
+
57
+ ```bash
58
+ git clone https://github.com/alboompro/boomnats-rails-example.git
59
+ ```
60
+
61
+ See details on [BoomNats Rails Project](https://github.com/alboompro/boomnats-rails-example)
62
+
63
+ ### Rails Installing
64
+
65
+ Add GEM to Gemfile
66
+
67
+ ```ruby
68
+ gem 'boom_nats', '~> 0.1.0'
69
+ ```
70
+
71
+ ### Rails Configuration
72
+
73
+ Using the generators to configure app, after create rails app
74
+
75
+ ```bash
76
+ rails g boom_nats:install
77
+ # create files:
78
+ # app/consumers/hello_consumer.rb
79
+ # config/initializers/boom_nats.rb
80
+ ```
81
+
82
+ Create consumers with generators too
83
+
84
+ ```bash
85
+ rails g boom_nats:consumer PaymentGetter
86
+ # creates file: app/consumers/payment_getter_consumer.rb
87
+ ```
88
+
89
+
90
+ ## Standalone
91
+
92
+ To facilitate learning, you can download the default repository containing a boilerplate project with (Ruby, Nats and ActiveRecord)
93
+
94
+ ```bash
95
+ git clone https://github.com/alboompro/boomnats-standard-example.git
96
+ ```
97
+
98
+ See details on [BoomNats Standard Project](https://github.com/alboompro/boomnats-standard-example)
99
+
100
+ ### Standalone Installing
101
+
102
+ Download de GEM with the following command:
103
+
104
+ ```bash
105
+ gem install boom_nats
106
+ ```
107
+
108
+ With bundle, add gem to Gemfile
109
+
110
+ ```ruby
111
+ gem 'boom_nats', '~> 0.1.0'
112
+ ```
113
+
114
+ ### Standalone Configuration
115
+
116
+ Create a simple file `boom_nats.rb` and put this content:
117
+
118
+ ```ruby
119
+ require "boom_nats"
120
+
121
+ class MyTopic < BoomNats::Topic
122
+ def consume
123
+ {
124
+ hello: params["name"]
125
+ }
126
+ rescue StandardError => e
127
+ puts e.message
128
+ end
129
+ end
130
+
131
+ BoomNats.setup do
132
+ servers "nats://127.0.0.1:4222"
133
+
134
+ draw_routes do
135
+ topic "hello", MyTopic
136
+ end
137
+ end
138
+
139
+ BoomNats.application.start
140
+ ```
141
+
142
+ ### Standalone Starting Application
143
+
144
+ ```bash
145
+ ruby boom_nats.rb
146
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "boom_nats"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require "pry"
12
+ Pry.start
data/bin/example ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "boom_nats"
6
+
7
+ class MyTopic < BoomNats::Topic
8
+ def consume
9
+ {
10
+ hello: params["name"]
11
+ }
12
+ rescue StandardError => e
13
+ puts e.message
14
+ end
15
+ end
16
+
17
+ BoomNats.setup do
18
+ servers "nats://0.0.0.0:4222"
19
+
20
+ draw_routes do
21
+ in_queue "test" do
22
+ topic "hello", MyTopic
23
+ end
24
+ end
25
+ end
26
+
27
+ BoomNats.application.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/boom_nats.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/boom_nats/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "boom_nats"
7
+ spec.version = BoomNats::VERSION
8
+ spec.authors = ["Welington Sampaio"]
9
+ spec.email = ["welington.sampaio@outlook.com"]
10
+
11
+ spec.summary = "Native NATS integration server to standalone or Rails based app"
12
+ spec.description = "Create ruby server or integrates with a Rails app to integrate NATS messages consumer"
13
+ spec.homepage = "https://opensource.alboompro.com/#alboom-nats-rb"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.5.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/alboompro/alboom-nats-rb"
21
+ spec.metadata["changelog_uri"] = "https://github.com/alboompro/alboom-nats-rb/master/Changelog"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency "activesupport", ">= 2.1"
34
+ spec.add_dependency "concurrent-ruby-edge", "~> 0.6", "< 1.0"
35
+ spec.add_dependency "nats", ">= 0.11.0", "< 1.0"
36
+ spec.add_dependency "rack", "~> 2.0", ">= 2.0.9"
37
+ spec.add_dependency "zeitwerk", ">= 2.0", "< 3.0"
38
+
39
+ # For more information and examples about making a new gem, checkout our
40
+ # guide at: https://bundler.io/guides/creating_gem.html
41
+ end
@@ -0,0 +1,98 @@
1
+ require "nats/client"
2
+ require "concurrent-edge"
3
+
4
+ module BoomNats
5
+ class Application
6
+ attr_accessor :router, :nats_options
7
+ attr_reader :route_topics, :nats
8
+
9
+ def initialize
10
+ @route_topics = []
11
+ @subscriptions = []
12
+ @mutex = Mutex.new
13
+ end
14
+
15
+ def servers(value)
16
+ stop
17
+
18
+ @nats = nats_connect(value)
19
+ end
20
+
21
+ def draw_routes(&block)
22
+ raise Error, "required block given" unless block_given?
23
+
24
+ @router = BoomNats::Router.new(self)
25
+ @router.setup(&block)
26
+ end
27
+
28
+ def setup(&block)
29
+ instance_eval(&block) if block_given?
30
+ end
31
+
32
+ def start
33
+ @route_topics.each do |rt|
34
+ @subscriptions << @nats.subscribe(rt.topic, rt.options) do |msg, reply, _sub|
35
+ rt.executor.new(msg, reply, @nats, rt.serializer, rt.parser)
36
+ end
37
+ end
38
+
39
+ BoomNats.logger.debug "BoomNats::started"
40
+
41
+ return if defined?(Rails::Railtie)
42
+
43
+ prepare_trap
44
+
45
+ wait
46
+ end
47
+
48
+ def stop
49
+ @subscriptions.each { |s| @nats.unsubscribe(s) }
50
+ @subscriptions = []
51
+
52
+ # disconnect from old server if already configured
53
+ if @nats&.connected?
54
+ @nats.drain do
55
+ @nats.stop
56
+ end
57
+ end
58
+ end
59
+
60
+ def kill
61
+ puts "exiting..." unless defined?(Rspec)
62
+ sleep 1
63
+ Thread.new do
64
+ @mutex.synchronize do
65
+ stop
66
+ exit unless defined?(Rspec)
67
+ end
68
+ end
69
+ end
70
+
71
+ protected
72
+
73
+ def nats_connect(servers)
74
+ ch = Concurrent::Channel.new
75
+ Concurrent::Channel.go do
76
+ # Connect to NATS service
77
+ NATS.start({
78
+ servers: servers,
79
+ **(nats_options.is_a?(Hash) ? nats_options : {})
80
+ }) do |nc|
81
+ ch.put nc
82
+ end
83
+ end
84
+
85
+ ch.take
86
+ end
87
+
88
+ def prepare_trap
89
+ %w[INT TERM].each do |s|
90
+ trap(s) { kill }
91
+ end
92
+ end
93
+
94
+ def wait
95
+ sleep
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,17 @@
1
+ module BoomNats
2
+ class Railtie < Rails::Railtie
3
+ initializer "activeservice.autoload", before: :set_autoload_paths do |app|
4
+ # app.config.autoload_paths << ActiveService::Configuration.path
5
+ app.config.eager_load_paths << Rails.root.join("app", "consumers").to_s
6
+ end
7
+
8
+ initializer "boom_nats.railtie.configure_rails_initialization" do |app|
9
+ app.config.nats = BoomNats.setup
10
+ BoomNats.logger = Rails.logger
11
+ end
12
+
13
+ server do |app|
14
+ app.config.nats.start
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ module BoomNats
2
+ class RouteTopic
3
+ attr_reader :topic, :executor, :queue, :serializer, :parser, :options
4
+
5
+ def initialize(application)
6
+ @application = application
7
+ end
8
+
9
+ def setup(topic_name, executor, serializer: nil, parser: nil, queue: nil, options: nil)
10
+ @topic = topic_name
11
+ @executor = executor
12
+ @serializer = default_serializer(serializer)
13
+ @parser = default_parser(parser)
14
+ @queue = queue
15
+ @options = default_options(options, queue)
16
+
17
+ subscribe
18
+ end
19
+
20
+ protected
21
+
22
+ def default_parser(parser)
23
+ [
24
+ parser,
25
+ BoomNats::Serializer.default_parser
26
+ ].each do |entry|
27
+ return entry if entry.respond_to? :call
28
+ end
29
+ BoomNats::Serializer::JSONParser
30
+ end
31
+
32
+ def default_serializer(serializer)
33
+ [
34
+ serializer,
35
+ BoomNats::Serializer.default_serializer
36
+ ].each do |entry|
37
+ return entry if entry.respond_to? :call
38
+ end
39
+ BoomNats::Serializer::JSONSerializer
40
+ end
41
+
42
+ def default_options(options = {}, queue = nil)
43
+ {
44
+ queue: queue,
45
+ max: nil,
46
+ **(options.is_a?(Hash) ? options : {})
47
+ }
48
+ end
49
+
50
+ def subscribe
51
+ @application.route_topics << self
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,25 @@
1
+ module BoomNats
2
+ class Router
3
+ def initialize(application)
4
+ @application = application
5
+ end
6
+
7
+ def setup(&block)
8
+ instance_eval(&block)
9
+ end
10
+
11
+ def in_queue(name, &block)
12
+ @current_group = name
13
+ instance_eval(&block)
14
+ @current_group = nil
15
+ end
16
+
17
+ def topic(name, klass, options = {})
18
+ options = {
19
+ queue: @current_group,
20
+ **options
21
+ }
22
+ RouteTopic.new(@application).setup(name, klass, **options)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ require "json"
2
+
3
+ module BoomNats::Serializer
4
+ JSONSerializer = JSON.method(:generate)
5
+ JSONParser = JSON.method(:parse)
6
+
7
+ mattr_accessor :default_serializer, :default_parser
8
+ end
@@ -0,0 +1,9 @@
1
+ module BoomNats::Setup
2
+ mattr_accessor :application
3
+
4
+ def setup(&block)
5
+ BoomNats.application ||= BoomNats::Application.new
6
+ BoomNats.application.setup(&block) if block_given?
7
+ BoomNats.application
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ module BoomNats
2
+ class Topic
3
+ def initialize(message, reply_to, nats, serializer, parser)
4
+ @message = message
5
+ @reply_to = reply_to
6
+ @nats = nats
7
+ @serializer = serializer
8
+ @parser = parser
9
+
10
+ start
11
+ end
12
+
13
+ def consume
14
+ raise Error, "consume method do not implement yet"
15
+ end
16
+
17
+ protected
18
+
19
+ def start
20
+ @result = consume
21
+
22
+ unless @reply_to.nil?
23
+ # puts "ReplyTo(#{@reply_to}) with: #{parsed_result}"
24
+ @nats.publish(@reply_to, parsed_result)
25
+ end
26
+
27
+ @result
28
+ end
29
+
30
+ def params
31
+ @params ||= parsed_message
32
+ end
33
+
34
+ def parsed_message
35
+ @parser.call(@message)
36
+ end
37
+
38
+ def parsed_result
39
+ @serializer.call(@result)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoomNats
4
+ VERSION = "0.1.0"
5
+ end
data/lib/boom_nats.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ require "logger"
5
+ require "active_support/core_ext/module/attribute_accessors"
6
+
7
+ loader = Zeitwerk::Loader.for_gem
8
+ loader.setup
9
+
10
+ module BoomNats
11
+ class Error < StandardError; end
12
+
13
+ extend BoomNats::Serializer
14
+ extend BoomNats::Setup
15
+
16
+ mattr_accessor :logger
17
+ end
18
+
19
+ BoomNats.logger = Logger.new(STDOUT)
20
+
21
+ require "boom_nats/railtie" if defined?(Rails::Railtie)
@@ -0,0 +1,14 @@
1
+ class BoomNats::ConsumerGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path("templates", __dir__)
3
+
4
+ desc " asdasdasd "
5
+ def create_consumer_class
6
+ create_file "app/consumers/#{file_name}_consumer.rb", <<~FILE
7
+ class #{class_name}Consumer < BoomNats::Topic
8
+ def consume
9
+ { message: "Hello \#{params["name"]}, how are you?" }
10
+ end
11
+ end
12
+ FILE
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ class BoomNats::InstallGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path("templates", __dir__)
3
+
4
+ desc " asdasdasd "
5
+ def install
6
+ copy_file "initializer.rb", "config/initializers/boom_nats.rb"
7
+ create_file "app/consumers/hello_consumer.rb", <<~FILE
8
+ class HelloConsumer < BoomNats::Topic
9
+ def consume
10
+ { message: "Hello \#{params["name"]}, how are you?" }
11
+ end
12
+ end
13
+ FILE
14
+ end
15
+
16
+ # desc " asdasdasd sadasda"
17
+ # def consumer
18
+ # puts "asd: #{asd}"
19
+ # end
20
+ end
@@ -0,0 +1,20 @@
1
+ class BoomNats::InstallGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path("templates", __dir__)
3
+
4
+ desc " asdasdasd "
5
+ def install
6
+ copy_file "initializer.rb", "config/initializers/boom_nats.rb"
7
+ create_file "app/consumers/hello_consumer.rb", <<~FILE
8
+ class HelloConsumer < BoomNats::Topic
9
+ def consume
10
+ { message: "Hello \#{params["name"]}, how are you?" }
11
+ end
12
+ end
13
+ FILE
14
+ end
15
+
16
+ desc " asdasdasd sadasda"
17
+ def consumer
18
+ puts "asd: #{asd}"
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ BoomNats.setup do
2
+ servers "nats://localhost:4222"
3
+
4
+ draw_routes do
5
+ in_queue "queue_name" do
6
+ topic "hello", HelloConsumer
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boom_nats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Welington Sampaio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: concurrent-ruby-edge
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '1.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '0.6'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: nats
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.11.0
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.0'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 0.11.0
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rack
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '2.0'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.0.9
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '2.0'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 2.0.9
87
+ - !ruby/object:Gem::Dependency
88
+ name: zeitwerk
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '2.0'
94
+ - - "<"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ type: :runtime
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '3.0'
107
+ description: Create ruby server or integrates with a Rails app to integrate NATS messages
108
+ consumer
109
+ email:
110
+ - welington.sampaio@outlook.com
111
+ executables: []
112
+ extensions: []
113
+ extra_rdoc_files: []
114
+ files:
115
+ - ".editorconfig"
116
+ - ".github/workflows/main.yml"
117
+ - ".gitignore"
118
+ - ".rspec"
119
+ - ".rubocop.yml"
120
+ - CHANGELOG.md
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/example
129
+ - bin/setup
130
+ - boom_nats.gemspec
131
+ - lib/boom_nats.rb
132
+ - lib/boom_nats/application.rb
133
+ - lib/boom_nats/railtie.rb
134
+ - lib/boom_nats/route_topic.rb
135
+ - lib/boom_nats/router.rb
136
+ - lib/boom_nats/serializer.rb
137
+ - lib/boom_nats/setup.rb
138
+ - lib/boom_nats/topic.rb
139
+ - lib/boom_nats/version.rb
140
+ - lib/generators/boom_nats/consumer_generator.rb
141
+ - lib/generators/boom_nats/install_generator.rb
142
+ - lib/generators/boom_nats/nats_generator.rb
143
+ - lib/generators/boom_nats/templates/initializer.rb
144
+ homepage: https://opensource.alboompro.com/#alboom-nats-rb
145
+ licenses:
146
+ - MIT
147
+ metadata:
148
+ homepage_uri: https://opensource.alboompro.com/#alboom-nats-rb
149
+ source_code_uri: https://github.com/alboompro/alboom-nats-rb
150
+ changelog_uri: https://github.com/alboompro/alboom-nats-rb/master/Changelog
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: 2.5.0
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubygems_version: 3.2.3
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Native NATS integration server to standalone or Rails based app
170
+ test_files: []