event_hub 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: 0d70dbc69d43c7d32c39cb0e2bd8706be5832aad878449181991ec7a445d961e
4
+ data.tar.gz: 7cbcaa9b40d9769c9721340866e9752af59107fb3b927494113825627bfe2649
5
+ SHA512:
6
+ metadata.gz: d5817202591324e0be2e21b0f0cb7a23ebc7ec3a08cc1fb01f63b97b476a2a868190eacfb51f7a18cc6dd99f791668cfaeafcf5a6daada3b0dc64878bf1dbbec
7
+ data.tar.gz: 2fc8f07f0dca34b4037a44fdf010bf340fc3c413240620c97ba08ff2da5a66e453b0d1086bc9aaf912261435873fd01f102a486292015de2592d1c0f74c63fed
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,55 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ TargetRubyVersion: 2.6.0
4
+ NewCops: enable
5
+
6
+ require:
7
+ - rubocop-rake
8
+
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ Style/TrailingCommaInHashLiteral:
13
+ Enabled: false
14
+
15
+ Style/SymbolArray:
16
+ Enabled: false
17
+
18
+ Style/NumericLiterals:
19
+ Enabled: false
20
+
21
+ Style/TrailingCommaInArguments:
22
+ Enabled: false
23
+
24
+ Style/FormatStringToken:
25
+ Enabled: false
26
+
27
+ Metrics/MethodLength:
28
+ Max: 35
29
+
30
+ Metrics/AbcSize:
31
+ Max: 40
32
+
33
+ Style/WordArray:
34
+ Enabled: false
35
+
36
+ Metrics/ClassLength:
37
+ Max: 110
38
+
39
+ Metrics/CyclomaticComplexity:
40
+ Max: 10
41
+
42
+ Style/Lambda:
43
+ EnforcedStyle: literal
44
+
45
+ Lint/AmbiguousOperatorPrecedence:
46
+ Enabled: false
47
+
48
+ Bundler/OrderedGems:
49
+ Enabled: false
50
+
51
+ Style/ClassAndModuleChildren:
52
+ Enabled: false
53
+
54
+ Lint/MissingSuper:
55
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-26
4
+
5
+ - 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 bogdan@seekingalpha.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,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in event_hub.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'rubocop', '~> 1.21'
13
+ gem 'rubocop-rake'
14
+ gem 'rubocop-rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,97 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ event_hub (0.1.0)
5
+ aws-sdk-sns (~> 1.0)
6
+ aws-sdk-sqs (~> 1.0)
7
+ bunny (>= 2.13.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ amq-protocol (2.3.2)
13
+ ast (2.4.2)
14
+ aws-eventstream (1.2.0)
15
+ aws-partitions (1.770.0)
16
+ aws-sdk-core (3.173.1)
17
+ aws-eventstream (~> 1, >= 1.0.2)
18
+ aws-partitions (~> 1, >= 1.651.0)
19
+ aws-sigv4 (~> 1.5)
20
+ jmespath (~> 1, >= 1.6.1)
21
+ aws-sdk-sns (1.60.0)
22
+ aws-sdk-core (~> 3, >= 3.165.0)
23
+ aws-sigv4 (~> 1.1)
24
+ aws-sdk-sqs (1.55.0)
25
+ aws-sdk-core (~> 3, >= 3.165.0)
26
+ aws-sigv4 (~> 1.1)
27
+ aws-sigv4 (1.5.2)
28
+ aws-eventstream (~> 1, >= 1.0.2)
29
+ bunny (2.20.3)
30
+ amq-protocol (~> 2.3, >= 2.3.1)
31
+ sorted_set (~> 1, >= 1.0.2)
32
+ diff-lcs (1.5.0)
33
+ jmespath (1.6.2)
34
+ json (2.6.3)
35
+ parallel (1.23.0)
36
+ parser (3.2.2.1)
37
+ ast (~> 2.4.1)
38
+ rainbow (3.1.1)
39
+ rake (13.0.6)
40
+ rbtree (0.4.6)
41
+ regexp_parser (2.8.0)
42
+ rexml (3.2.5)
43
+ rspec (3.12.0)
44
+ rspec-core (~> 3.12.0)
45
+ rspec-expectations (~> 3.12.0)
46
+ rspec-mocks (~> 3.12.0)
47
+ rspec-core (3.12.2)
48
+ rspec-support (~> 3.12.0)
49
+ rspec-expectations (3.12.3)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.12.0)
52
+ rspec-mocks (3.12.5)
53
+ diff-lcs (>= 1.2.0, < 2.0)
54
+ rspec-support (~> 3.12.0)
55
+ rspec-support (3.12.0)
56
+ rubocop (1.51.0)
57
+ json (~> 2.3)
58
+ parallel (~> 1.10)
59
+ parser (>= 3.2.0.0)
60
+ rainbow (>= 2.2.2, < 4.0)
61
+ regexp_parser (>= 1.8, < 3.0)
62
+ rexml (>= 3.2.5, < 4.0)
63
+ rubocop-ast (>= 1.28.0, < 2.0)
64
+ ruby-progressbar (~> 1.7)
65
+ unicode-display_width (>= 2.4.0, < 3.0)
66
+ rubocop-ast (1.28.1)
67
+ parser (>= 3.2.1.0)
68
+ rubocop-capybara (2.18.0)
69
+ rubocop (~> 1.41)
70
+ rubocop-factory_bot (2.23.1)
71
+ rubocop (~> 1.33)
72
+ rubocop-rake (0.6.0)
73
+ rubocop (~> 1.0)
74
+ rubocop-rspec (2.22.0)
75
+ rubocop (~> 1.33)
76
+ rubocop-capybara (~> 2.17)
77
+ rubocop-factory_bot (~> 2.22)
78
+ ruby-progressbar (1.13.0)
79
+ set (1.0.3)
80
+ sorted_set (1.0.3)
81
+ rbtree
82
+ set (~> 1.0)
83
+ unicode-display_width (2.4.2)
84
+
85
+ PLATFORMS
86
+ x86_64-darwin-21
87
+
88
+ DEPENDENCIES
89
+ event_hub!
90
+ rake (~> 13.0)
91
+ rspec (~> 3.0)
92
+ rubocop (~> 1.21)
93
+ rubocop-rake
94
+ rubocop-rspec
95
+
96
+ BUNDLED WITH
97
+ 2.4.13
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 SeekingAlpha
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,39 @@
1
+ # EventHub
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/event_hub`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/event_hub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/event_hub/blob/master/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the EventHub project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/event_hub/blob/master/CODE_OF_CONDUCT.md).
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/event_hub.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/event_hub/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'event_hub'
7
+ spec.version = EventHub::VERSION
8
+ spec.authors = ['Bogdan Guban']
9
+ spec.email = ['biguban@gmail.com']
10
+
11
+ spec.summary = 'Event driven inter microservice communication'
12
+ spec.description = 'Event driven inter microservice communication'
13
+ spec.homepage = 'https://github.com/seekingalpha/event_hub'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.6.0'
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = spec.homepage
21
+ spec.metadata['changelog_uri'] = spec.homepage
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(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
28
+ end
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ spec.add_dependency 'aws-sdk-sns', '~> 1.0'
36
+ spec.add_dependency 'aws-sdk-sqs', '~> 1.0'
37
+ spec.add_dependency 'bunny', '>= 2.13.0'
38
+
39
+ # For more information and examples about making a new gem, check out our
40
+ # guide at: https://bundler.io/guides/creating_gem.html
41
+ spec.metadata['rubygems_mfa_required'] = 'true'
42
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventHub::Adapters::Aws::Message < EventHub::Message
4
+ def initialize(adapter, message)
5
+ @adapter = adapter
6
+ @message = message
7
+ end
8
+
9
+ def attributes
10
+ @attributes ||= parsed_body['MessageAttributes'].transform_values { |v| v['Value'] }
11
+ end
12
+
13
+ def body
14
+ parsed_body['Message']
15
+ end
16
+
17
+ def event
18
+ attributes['event']
19
+ end
20
+
21
+ def version
22
+ attributes['version']
23
+ end
24
+
25
+ def ack
26
+ # Delete the message from the queue.
27
+ @adapter.delete_message(@message.receipt_handle)
28
+ end
29
+
30
+ def reject
31
+ if @adapter.config[:dead_queue_url]
32
+ # TODO: publish the message to the dead_queue
33
+ end
34
+ ack
35
+ end
36
+
37
+ private
38
+
39
+ # {
40
+ # "Type" : "Notification",
41
+ # "MessageId" : "025727dd-bb27-5c53-8a79-7b38f0cfe19a",
42
+ # "SequenceNumber" : "10000000000000020000",
43
+ # "TopicArn" : "arn:aws:sns:us-west-2:744522205193:event-hub.fifo",
44
+ # "Subject" : "subject",
45
+ # "Message" : "body",
46
+ # "Timestamp" : "2023-05-22T10:53:28.806Z",
47
+ # "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:744522205193:event-hub.fifo:c8e1a595-4b0d-44b4-8087-094e7b0de09a",
48
+ # "MessageAttributes" : {
49
+ # "str_attr" : {"Type":"String","Value":"string_attr_val"}
50
+ # }
51
+ # }
52
+ def parsed_body
53
+ @parsed_body ||= JSON.parse(@message.body)
54
+ end
55
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-sns'
4
+ require 'aws-sdk-sqs'
5
+
6
+ class EventHub::Adapters::Aws
7
+ attr_reader :config
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ end
12
+
13
+ def subscribe(&block)
14
+ loop do
15
+ receive_message_result = sqs.receive_message({
16
+ queue_url: @config[:queue_url],
17
+ message_attribute_names: ['All'], # Receive all custom attributes.
18
+ max_number_of_messages: 10, # Receive at most one message.
19
+ wait_time_seconds: 15 # Do not wait to check for the message.
20
+ })
21
+
22
+ # Display information about the message.
23
+ # Display the message's body and each custom attribute value.
24
+ receive_message_result.messages.each do |aws_msg|
25
+ message = Message.new(self, aws_msg)
26
+ block.call(message)
27
+ end
28
+ end
29
+ end
30
+
31
+ def publish(event)
32
+ topic.publish({
33
+ message: event.to_json,
34
+ message_attributes: {
35
+ event: { data_type: 'String', string_value: event.class.event },
36
+ version: { data_type: 'String', string_value: event.class.version },
37
+ },
38
+ message_group_id: 'message_group_id',
39
+ message_deduplication_id: SecureRandom.uuid
40
+ })
41
+ end
42
+
43
+ def setup_bindings
44
+ policy = { event: @config[:subscribe].keys }.to_json
45
+ subscription = topic.subscriptions.find { |s| s.attributes['Endpoint'] == @config[:queue_arn] }
46
+ if subscription
47
+ subscription.set_attributes({
48
+ attribute_name: 'FilterPolicy',
49
+ attribute_value: policy
50
+ })
51
+ else
52
+ topic.subscribe({
53
+ protocol: 'sqs',
54
+ attributes: { 'FilterPolicy' => policy },
55
+ endpoint: @config[:queue_arn]
56
+ })
57
+ end
58
+ end
59
+
60
+ def delete_message(receipt_handle)
61
+ sqs.delete_message(
62
+ queue_url: @config[:queue_url],
63
+ receipt_handle: receipt_handle
64
+ )
65
+ end
66
+
67
+ def topic
68
+ @topic ||= sns.topic(@config[:exchange_arn])
69
+ end
70
+
71
+ def sns
72
+ @sns ||= Aws::SNS::Resource.new(@config[:credentials] || {})
73
+ end
74
+
75
+ def sqs
76
+ @sqs ||= Aws::SQS::Client.new(@config[:credentials] || {})
77
+ end
78
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventHub::Adapters::Bunny::Message < EventHub::Message
4
+ def initialize(delivery_info, properties, body, channel)
5
+ @delivery_info = delivery_info
6
+ @properties = properties
7
+ @body = body
8
+ @channel = channel
9
+ end
10
+
11
+ def attributes
12
+ @properties
13
+ end
14
+
15
+ attr_reader :body
16
+
17
+ def event
18
+ @properties[:event]
19
+ end
20
+
21
+ def version
22
+ @properties[:version]
23
+ end
24
+
25
+ def ack
26
+ @channel.ack(@delivery_info.delivery_tag)
27
+ end
28
+
29
+ def reject
30
+ @channel.reject(@delivery_info.delivery_tag)
31
+ end
32
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bunny'
4
+
5
+ class EventHub::Adapters::Bunny
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def subscribe(&block)
11
+ channel.prefetch(1)
12
+ queue.subscribe(block: true, manual_ack: true) do |delivery_info, properties, body|
13
+ message = Message.new(delivery_info, properties, body, channel)
14
+ block.call(message)
15
+ end
16
+ end
17
+
18
+ def publish(event)
19
+ exchange.publish(
20
+ event.body,
21
+ routing_key: event.class.event,
22
+ persistent: true,
23
+ event: event.class.event,
24
+ version: event.class.version
25
+ )
26
+ end
27
+
28
+ def setup_bindings
29
+ @config.subscribe.each_key do |routing_key|
30
+ queue.bind(exchange, routing_key: routing_key)
31
+ end
32
+ end
33
+
34
+ def channel
35
+ @channel ||= connection.create_channel
36
+ end
37
+
38
+ private
39
+
40
+ def exchange
41
+ @exchange ||= channel.direct(@config[:exchange])
42
+ end
43
+
44
+ def queue
45
+ @queue ||= channel.queue(@config[:queue], durable: true)
46
+ end
47
+
48
+ def connection
49
+ return @connection if defined?(@connection)
50
+
51
+ @connection = ::Bunny.new # TODO: config
52
+ @connection.start
53
+ @connection
54
+ end
55
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventHub::Adapters::Test::Message < EventHub::Message
4
+ def initialize(body, queue, attributes = {})
5
+ @body = body
6
+ @attributes = attributes
7
+ @queue = queue
8
+ end
9
+
10
+ attr_reader :attributes, :body
11
+
12
+ def event
13
+ @attributes[:event]
14
+ end
15
+
16
+ def version
17
+ @attributes[:version]
18
+ end
19
+
20
+ def ack
21
+ @ack = true
22
+ @queue.delete(self)
23
+ end
24
+
25
+ def ack?
26
+ !!@ack
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventHub::Adapters::Test
4
+ attr_accessor :queue
5
+
6
+ def initialize(config)
7
+ @config = config
8
+ @queue = []
9
+ end
10
+
11
+ def subscribe(&block)
12
+ @queue.each do |message|
13
+ block.call(message)
14
+ end
15
+ end
16
+
17
+ def publish(event)
18
+ @queue << Message.new(event, @queue)
19
+ end
20
+
21
+ def setup_bindings
22
+ true
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventHub::Adapters; end
4
+ require_relative 'adapters/bunny'
5
+ require_relative 'adapters/bunny/message'
6
+ require_relative 'adapters/aws'
7
+ require_relative 'adapters/aws/message'
8
+ require_relative 'adapters/test'
9
+ require_relative 'adapters/test/message'
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventHub::Event
4
+ def publish
5
+ EventHub.publish(self)
6
+ end
7
+
8
+ def body
9
+ to_json
10
+ end
11
+
12
+ def self.event(event = nil)
13
+ @event = event if event
14
+ @event
15
+ end
16
+
17
+ def self.version(version = nil)
18
+ @version = version if version
19
+ @version
20
+ end
21
+
22
+ # def self.field(field)
23
+ # self.fields ||= Set.new
24
+ # self.fields << field
25
+ # attr_accessor(field)
26
+ # end
27
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventHub::Handler
4
+ attr_reader :message
5
+
6
+ def self.version(version = nil)
7
+ @version = version if version
8
+ @version
9
+ end
10
+
11
+ def initialize(message)
12
+ @message = message
13
+ end
14
+
15
+ def on_incorrect_version
16
+ raise IncorrectVersion
17
+ end
18
+
19
+ def validate!
20
+ on_incorrect_version if @message.version != self.class.version
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventHub::Message
4
+ def body
5
+ raise NotImplementedError
6
+ end
7
+
8
+ def routing_key
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def headers
13
+ raise NotImplementedError
14
+ end
15
+
16
+ # Message acknowledgment
17
+ def ack
18
+ raise NotImplementedError
19
+ end
20
+
21
+ # moves message to the dead queue
22
+ def reject
23
+ raise NotImplementedError
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventHub
4
+ VERSION = '0.1.0'
5
+ end
data/lib/event_hub.rb ADDED
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'event_hub/version'
4
+ require_relative 'event_hub/event'
5
+ require_relative 'event_hub/handler'
6
+ require_relative 'event_hub/message'
7
+ require_relative 'event_hub/adapters'
8
+
9
+ class EventHub
10
+ class NoHandlerDefined < StandardError; end
11
+ class IncorrectVersion < StandardError; end
12
+ class IgnoreMessage < StandardError; end
13
+ class RejectMessage < StandardError; end
14
+
15
+ def self.configure(config)
16
+ @config = config
17
+ @instance = nil
18
+ end
19
+
20
+ def self.instance
21
+ @instance ||= new(@config)
22
+ end
23
+
24
+ def self.subscribe
25
+ instance.adapter.subscribe do |message|
26
+ handler_class = @config.dig(:subscribe, message.event.to_sym, :handler)
27
+ raise NoHandlerDefined unless handler_class
28
+
29
+ handler = handler_class.new(message)
30
+ handler.validate!
31
+
32
+ handler.call
33
+ message.ack
34
+ rescue IgnoreMessage
35
+ message.ack
36
+ rescue RejectMessage
37
+ message.reject
38
+ rescue Exception => e # rubocop:disable Lint/RescueException
39
+ @config[:on_failure]&.call(e, message)
40
+ message.reject
41
+ end
42
+ end
43
+
44
+ def self.publish(event)
45
+ instance.adapter.publish(event)
46
+ end
47
+
48
+ def self.adapter
49
+ instance.adapter
50
+ end
51
+
52
+ def initialize(config)
53
+ @config = config
54
+ @config[:subscribe] ||= {}
55
+ @config[:subscribe].each_value { |subscription| subscription[:handler] = subscription[:handler].constantize }
56
+ end
57
+
58
+ def adapter
59
+ @adapter ||= Adapters.const_get(@config[:adapter].camelize).new(@config)
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_hub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bogdan Guban
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-sns
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-sqs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bunny
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.13.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.13.0
55
+ description: Event driven inter microservice communication
56
+ email:
57
+ - biguban@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - CHANGELOG.md
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - event_hub.gemspec
72
+ - lib/event_hub.rb
73
+ - lib/event_hub/adapters.rb
74
+ - lib/event_hub/adapters/aws.rb
75
+ - lib/event_hub/adapters/aws/message.rb
76
+ - lib/event_hub/adapters/bunny.rb
77
+ - lib/event_hub/adapters/bunny/message.rb
78
+ - lib/event_hub/adapters/test.rb
79
+ - lib/event_hub/adapters/test/message.rb
80
+ - lib/event_hub/event.rb
81
+ - lib/event_hub/handler.rb
82
+ - lib/event_hub/message.rb
83
+ - lib/event_hub/version.rb
84
+ homepage: https://github.com/seekingalpha/event_hub
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ allowed_push_host: https://rubygems.org/
89
+ homepage_uri: https://github.com/seekingalpha/event_hub
90
+ source_code_uri: https://github.com/seekingalpha/event_hub
91
+ changelog_uri: https://github.com/seekingalpha/event_hub
92
+ rubygems_mfa_required: 'true'
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 2.6.0
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.4.6
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Event driven inter microservice communication
112
+ test_files: []