phantom_events 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 067b8600a2bb23bf2b1d1a32e3a9c190d1d7c554c38e19faed4d90c59e6d4fbf
4
+ data.tar.gz: ebc5c03f3baff16f0873a61a8d980383662623f5ad0bfe50e26193924778b87c
5
+ SHA512:
6
+ metadata.gz: 626f1b66865c8507f3a6206d8b61d80d2f05174abb478deda3dbf75cf53079c9424dc8618a649b4e6f481e27f7332b567a67af62af49fd3d9c4f389a1b0d8268
7
+ data.tar.gz: 7f5ea1495cd758f9887f379feb83a98030d5bbcf224afbcfb683c273e560ac3522583df497b81447d27c5f9bf52c2b37bf56cbb068a376419d8f638b2b390624
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.0.1] - 2022-05-04
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 tomas@dundacek.cz. 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,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in phantom_events.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.11"
10
+ gem "rubocop", "~> 1.25"
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ phantom_events (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ parallel (1.21.0)
12
+ parser (3.1.0.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.1.1)
15
+ rake (13.0.6)
16
+ regexp_parser (2.2.0)
17
+ rexml (3.2.5)
18
+ rspec (3.11.0)
19
+ rspec-core (~> 3.11.0)
20
+ rspec-expectations (~> 3.11.0)
21
+ rspec-mocks (~> 3.11.0)
22
+ rspec-core (3.11.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-expectations (3.11.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-mocks (3.11.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-support (3.11.0)
31
+ rubocop (1.25.0)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.1.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.15.1, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.15.1)
41
+ parser (>= 3.0.1.1)
42
+ ruby-progressbar (1.11.0)
43
+ unicode-display_width (2.1.0)
44
+
45
+ PLATFORMS
46
+ x86_64-darwin-20
47
+
48
+ DEPENDENCIES
49
+ phantom_events!
50
+ rake (~> 13.0)
51
+ rspec (~> 3.11)
52
+ rubocop (~> 1.25)
53
+
54
+ BUNDLED WITH
55
+ 2.3.6
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # PhantomEvents
2
+
3
+ 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/phantom_events`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'phantom_events'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install phantom_events
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/phantom_events. 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]/phantom_events/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the PhantomEvents project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/phantom_events/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]
@@ -0,0 +1,56 @@
1
+ module PhantomEvents
2
+ module Adapters
3
+ class ActiveJobAdapter
4
+
5
+ def initialize(listeners_path:,
6
+ parent_class: ActiveJob::Base,
7
+ default_queue: :default)
8
+ @listeners_path = listeners_path
9
+ @parent_class = parent_class
10
+ @default_queue = default_queue
11
+
12
+ setup_adapter_job_class!
13
+ end
14
+
15
+ def handle_event(event_name, *args, **kwargs)
16
+ listeners_for_event(event_name).each do |listener_klass|
17
+ AdapterJob.perform_later(listener_klass, event_name, *args, **kwargs)
18
+ end
19
+ end
20
+
21
+ def handles_event?(event_name)
22
+ listeners_for_event(event_name).any?
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :listeners_path, :parent_class, :default_queue
28
+
29
+ def listeners
30
+ listeners_path.glob("**/*.rb").map do |pathname|
31
+ relative = pathname.relative_path_from(listeners_path).sub_ext("")
32
+ relative.to_s.classify.safe_constantize
33
+ end
34
+ end
35
+
36
+ def listeners_for_event(event_name)
37
+ listeners.select do |listener|
38
+ listener.instance_methods.include?(event_name)
39
+ end
40
+ end
41
+
42
+ def setup_adapter_job_class!
43
+ klass = Class.new parent_class do
44
+
45
+ def perform(klass, event_name, *args, **kwargs)
46
+ klass.new.public_send(event_name, *args, **kwargs)
47
+ end
48
+ end
49
+ klass.queue_as default_queue
50
+
51
+ self.class.send(:remove_const, :AdapterJob) if defined?(AdapterJob)
52
+ self.class.const_set(:AdapterJob, klass)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,45 @@
1
+ module PhantomEvents
2
+ module Adapters
3
+ class AmqpAdapter
4
+
5
+ def initialize(dsn: ENV['AMQP_URL'], exchange_name: :events)
6
+ @dsn = dsn
7
+ @exchange_name = exchange_name
8
+ end
9
+
10
+ def handle_event(event_name, *args, **kwargs)
11
+ if args.last.is_a?(Hash) && kwargs.empty?
12
+ kwargs = args.pop.symbolize_keys!
13
+ end
14
+
15
+ message = {
16
+ event_name: event_name,
17
+ args: args,
18
+ kwargs: kwargs
19
+ }.to_json
20
+
21
+ exchange.publish(message)
22
+ end
23
+
24
+ def handles_event?(_)
25
+ true
26
+ end
27
+
28
+ private
29
+
30
+ attr_accessor :dsn, :exchange_name
31
+
32
+ def exchange
33
+ @exchange ||= channel.fanout(exchange_name.to_s, durable: true)
34
+ end
35
+
36
+ def channel
37
+ @channel ||= connection.create_channel
38
+ end
39
+
40
+ def connection
41
+ @connection ||= Bunny.new(dsn).tap(&:start)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,57 @@
1
+ module PhantomEvents
2
+ module Adapters
3
+ class SidekiqAdapter
4
+
5
+ def initialize(listeners_path:,
6
+ default_queue: :default)
7
+ @listeners_path = listeners_path
8
+ @parent_class = parent_class
9
+ @default_queue = default_queue
10
+
11
+ setup_adapter_job_class!
12
+ end
13
+
14
+ def handle_event(event_name, *args, **kwargs)
15
+ listeners_for_event(event_name).each do |listener_klass|
16
+ AdapterJob.perform_async(listener_klass, event_name, *args + kwargs)
17
+ end
18
+ end
19
+
20
+ def handles_event?(event_name)
21
+ listeners_for_event(event_name).any?
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :listeners_path, :parent_class, :default_queue
27
+
28
+ def listeners
29
+ listeners_path.glob("**/*.rb").map do |pathname|
30
+ relative = pathname.relative_path_from(listeners_path).sub_ext("")
31
+ relative.to_s.classify.safe_constantize
32
+ end
33
+ end
34
+
35
+ def listeners_for_event(event_name)
36
+ listeners.select do |listener|
37
+ listener.instance_methods.include?(event_name)
38
+ end
39
+ end
40
+
41
+ def setup_adapter_job_class!
42
+ klass = Class.new do
43
+ include Sidekiq::Job
44
+
45
+ def perform(klass, event_name, *args)
46
+ kwargs = args.pop.symbolize_keys! if args.last.is_a?(Hash)
47
+ klass.safe_constantize.new.public_send(event_name, *args, **kwargs)
48
+ end
49
+ end
50
+ klass.sidekiq_options queue: default_queue
51
+
52
+ self.class.send(:remove_const, :AdapterJob) if defined?(AdapterJob)
53
+ self.class.const_set(:AdapterJob, klass)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,22 @@
1
+ module PhantomEvents
2
+ class Configuration
3
+
4
+ attr_reader :adapters
5
+
6
+ def initialize(&_block)
7
+ @adapters = []
8
+ @listeners = []
9
+ yield self
10
+ end
11
+
12
+ def register_adapter(name, **kwargs)
13
+ require "phantom_events/adapters/#{name}_adapter"
14
+ klass = "PhantomEvents::Adapters::#{name.to_s.camelize}Adapter".safe_constantize
15
+ @adapters << klass.new(**kwargs)
16
+ end
17
+
18
+ def register_listener(klass)
19
+ @listeners << klass
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module PhantomEvents
2
+ module Publisher
3
+ def publish(event_name, *args, **kwargs)
4
+ adapters_for(event_name).each do |adapter|
5
+ adapter.handle_event(event_name, *args, **kwargs)
6
+ end
7
+
8
+ event_name
9
+ end
10
+
11
+ private
12
+
13
+ def adapters_for(event_name)
14
+ PhantomEvents.config.adapters.select do |adapter|
15
+ adapter.handles_event?(event_name)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PhantomEvents
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'phantom_events/version'
4
+ require_relative 'phantom_events/configuration'
5
+ require_relative 'phantom_events/publisher'
6
+
7
+ module PhantomEvents
8
+ class Error < StandardError; end
9
+
10
+ def self.config
11
+ @@config
12
+ end
13
+
14
+ def self.configure(&block)
15
+ if defined?(::Rails)
16
+ Rails.application.config.to_prepare do
17
+ @@config = Configuration.new(&block)
18
+ end
19
+ else
20
+ @@config = Configuration.new(&block)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ module PhantomEvents
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phantom_events
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomáš Dundáček
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - tomas@dundacek.cz
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - README.md
27
+ - Rakefile
28
+ - lib/phantom_events.rb
29
+ - lib/phantom_events/adapters/active_job_adapter.rb
30
+ - lib/phantom_events/adapters/amqp_adapter.rb
31
+ - lib/phantom_events/adapters/sidekiq_adapter.rb
32
+ - lib/phantom_events/configuration.rb
33
+ - lib/phantom_events/publisher.rb
34
+ - lib/phantom_events/version.rb
35
+ - sig/phantom_events.rbs
36
+ homepage: https://github.com/tomasdundacek/phantom_events
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/tomasdundacek/phantom_events
41
+ source_code_uri: https://github.com/tomasdundacek/phantom_events
42
+ changelog_uri: https://github.com/tomasdundacek/phantom_events/blob/master/CHANGELOG.md
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.7.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.2.22
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Simple event processing bus
62
+ test_files: []