artsy-event_publisher 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: c0ab4a82306ab2003e5ddcba9a9ee1007e169e3062fd9dc5e430902609005ea3
4
+ data.tar.gz: c7d0a36f257d85966a9ba93820846b5d1b75e9def88d0da63ac01fdeffc1908b
5
+ SHA512:
6
+ metadata.gz: 6d08aad5664fd481332252f2cd2c5829c9ce2213493600cbe4b4d061994cedc01c4f89bc9d5d0274dc01e2cd5d7a24eca171dc1e2338181f175d3dbafb683b2f
7
+ data.tar.gz: ece529b1f0f3e650761e204c5fdb31e481161e53ef26f7f7836ad35218f42c0f96adf8a82b8589261b9bf5b6c366f9856e9c317cc5fd4ce78c952d63d834102e
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.2.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Joey Aghion
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,51 @@
1
+ # Artsy::EventPublisher
2
+
3
+ Lightweight helper for publishing events to rabbitmq in a pattern consistent with Artsy's ecosystem.
4
+
5
+ ## Installation
6
+
7
+ Add following line to your Gemfile
8
+
9
+ ```ruby
10
+ gem "artsy-event_publisher"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Example initialization:
16
+
17
+ ```ruby
18
+ # config/initializers/artsy_event-publisher.rb
19
+ Artsy::EventPublisher.configure do |config|
20
+ config.app_id = "my-app" # identifies RabbitMQ connection
21
+ config.enabled = true # enable/disable publishing events
22
+ config.rabbitmq_url = "amqp(s)://<user>:<pass>@<host>:<port>/<vhost>" # required
23
+ config.logger = Rails.logger
24
+ end
25
+ ```
26
+
27
+ Publishing a sample event:
28
+
29
+ ```ruby
30
+ Artsy::EventPublisher.publish(
31
+ "auctions",
32
+ "bidder.pending_approval",
33
+ verb: "pending_approval",
34
+ subject: {id: bidder.id.to_s, root_type: "Bidder", display: bidder.name},
35
+ object: {id: sale.id.to_s, root_type: "Sale", display: sale.name}
36
+ )
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
42
+
43
+ To install this gem on 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).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/artsy/artsy-event_publisher.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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 "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Artsy
4
+ module EventPublisher
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "event_publisher/version"
4
+ require "json"
5
+ require "bunny"
6
+
7
+ module Artsy
8
+ module EventPublisher
9
+ class Error < StandardError; end
10
+
11
+ def self.publish(topic, routing_key = "", subject: nil, verb: nil, object: nil, properties: nil)
12
+ return unless Config.enabled
13
+
14
+ data = {
15
+ subject: subject,
16
+ verb: verb,
17
+ object: object,
18
+ properties: properties
19
+ }.compact
20
+ payload = data.to_json
21
+ Connection.publish(topic: topic, routing_key: routing_key, payload: payload)
22
+ Config.logger&.debug("[event] #{payload}")
23
+ data
24
+ end
25
+
26
+ def self.configure
27
+ yield Config if block_given?
28
+ Config
29
+ end
30
+
31
+ module Config
32
+ extend self
33
+
34
+ attr_accessor :app_id
35
+ attr_accessor :enabled
36
+ attr_accessor :rabbitmq_url
37
+ attr_accessor :logger
38
+ end
39
+
40
+ class Connection
41
+ OPTIONS = {
42
+ persistent: true,
43
+ content_type: "application/json",
44
+ headers: {}
45
+ }
46
+ @connection = nil
47
+ @mutex = Mutex.new
48
+
49
+ def self.publish(topic:, routing_key:, payload:)
50
+ with_channel do |channel|
51
+ options = OPTIONS.merge(routing_key: routing_key, app_id: Config.app_id)
52
+ channel.topic(topic, durable: true).publish(payload, options)
53
+ raise Error, "Publishing failed" unless channel.wait_for_confirms
54
+ end
55
+ end
56
+
57
+ def self.with_channel
58
+ channel = get_connection.create_channel
59
+ channel.confirm_select
60
+ yield channel if block_given?
61
+ ensure
62
+ channel.close if channel&.open?
63
+ end
64
+
65
+ # Synchronized access to the connection
66
+ def self.get_connection
67
+ @mutex.synchronize do
68
+ @connection ||= build_connection
69
+ @connection = build_connection if @connection.closed?
70
+ @connection
71
+ end
72
+ end
73
+
74
+ def self.build_connection
75
+ Bunny.new(Config.rabbitmq_url).tap(&:start)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,6 @@
1
+ module Artsy
2
+ module EventPublisher
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artsy-event_publisher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joey Aghion
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - joey@aghion.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".standard.yml"
36
+ - ".tool-versions"
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/artsy/event_publisher.rb
41
+ - lib/artsy/event_publisher/version.rb
42
+ - sig/artsy/event_publisher.rbs
43
+ homepage: https://github.com/artsy/artsy-event_publisher
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ allowed_push_host: https://rubygems.org
48
+ homepage_uri: https://github.com/artsy/artsy-event_publisher
49
+ source_code_uri: https://github.com/artsy/artsy-event_publisher
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.6.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.4.19
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Simple library for consistent publishing of RabbitMQ events at Artsy.
69
+ test_files: []