bunny_events 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/Gemfile +5 -4
- data/Gemfile.lock +16 -0
- data/README.md +2 -0
- data/Rakefile +3 -3
- data/bin/console +3 -3
- data/bin/example +26 -29
- data/bunny_events.gemspec +22 -23
- data/lib/bunny_event.rb +1 -3
- data/lib/bunny_events/version.rb +1 -1
- data/lib/bunny_events.rb +30 -37
- metadata +16 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccc318c6413bcd031b944955322d4901090c15b77faf52929726e04ead11b3b3
|
4
|
+
data.tar.gz: 5a36d2c86cde7e3f1e49f2b443b9e527be434dfe32d80398b9ff5b264c54ffe7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3d92376e744871359def061da97f575edfabfc926d8762100c3a7ff430651950251b59bf2e95df3959192bd4d7071d9b2a291d0605b72c0d3fc329d804b7aef
|
7
|
+
data.tar.gz: c6cd4c92ee2aee2e4c31cabd94c3dcfb7988c1097e49a0308d3acc0e8887f4dbeaf0dca3d491526bb555554fb41afdd658f0d17dcfb4be65204ba847cd4d8a99
|
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
3
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
4
|
|
5
5
|
gemspec
|
6
6
|
|
7
|
-
gem
|
7
|
+
gem 'bunny', '>= 2.14.0'
|
8
8
|
gem 'bunny-mock'
|
9
|
-
gem 'coveralls', require: false
|
9
|
+
gem 'coveralls', require: false
|
10
|
+
gem 'rubocop'
|
data/Gemfile.lock
CHANGED
@@ -8,6 +8,7 @@ GEM
|
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
amq-protocol (2.3.0)
|
11
|
+
ast (2.4.0)
|
11
12
|
bunny (2.14.2)
|
12
13
|
amq-protocol (~> 2.3, >= 2.3.0)
|
13
14
|
bunny-mock (1.7.0)
|
@@ -20,7 +21,12 @@ GEM
|
|
20
21
|
tins (~> 1.6)
|
21
22
|
diff-lcs (1.3)
|
22
23
|
docile (1.3.2)
|
24
|
+
jaro_winkler (1.5.3)
|
23
25
|
json (2.2.0)
|
26
|
+
parallel (1.17.0)
|
27
|
+
parser (2.6.4.0)
|
28
|
+
ast (~> 2.4.0)
|
29
|
+
rainbow (3.0.0)
|
24
30
|
rake (10.5.0)
|
25
31
|
rspec (3.8.0)
|
26
32
|
rspec-core (~> 3.8.0)
|
@@ -35,6 +41,14 @@ GEM
|
|
35
41
|
diff-lcs (>= 1.2.0, < 2.0)
|
36
42
|
rspec-support (~> 3.8.0)
|
37
43
|
rspec-support (3.8.2)
|
44
|
+
rubocop (0.74.0)
|
45
|
+
jaro_winkler (~> 1.5.1)
|
46
|
+
parallel (~> 1.10)
|
47
|
+
parser (>= 2.6)
|
48
|
+
rainbow (>= 2.2.2, < 4.0)
|
49
|
+
ruby-progressbar (~> 1.7)
|
50
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
51
|
+
ruby-progressbar (1.10.1)
|
38
52
|
simplecov (0.16.1)
|
39
53
|
docile (~> 1.1)
|
40
54
|
json (>= 1.8, < 3)
|
@@ -44,6 +58,7 @@ GEM
|
|
44
58
|
tins (~> 1.0)
|
45
59
|
thor (0.20.3)
|
46
60
|
tins (1.21.1)
|
61
|
+
unicode-display_width (1.6.0)
|
47
62
|
|
48
63
|
PLATFORMS
|
49
64
|
ruby
|
@@ -56,6 +71,7 @@ DEPENDENCIES
|
|
56
71
|
coveralls
|
57
72
|
rake (~> 10.0)
|
58
73
|
rspec (~> 3.0)
|
74
|
+
rubocop
|
59
75
|
|
60
76
|
BUNDLED WITH
|
61
77
|
1.17.2
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/bunny_events.svg)](https://badge.fury.io/rb/bunny_events) [![Build Status](https://travis-ci.org/Nexus-Mods/bunny_events.svg?branch=master)](https://travis-ci.org/Nexus-Mods/bunny_events) [![Coverage Status](https://coveralls.io/repos/github/Nexus-Mods/bunny_events/badge.svg?branch=master)](https://coveralls.io/github/Nexus-Mods/bunny_events?branch=master)
|
4
4
|
|
5
|
+
<p><img height="200" width="200" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3333883/bunny_events_logo.png" /></p>
|
6
|
+
|
5
7
|
A simple wrapper gem to aid with producing events to a message queue, using Bunny, in a standardized and uniform way across multiple microservices.
|
6
8
|
|
7
9
|
Rather than using Bunny directly, this gem allows an application to define "Event Definitions" which can be defined and published
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bunny_events'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "bunny_events"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start(__FILE__)
|
data/bin/example
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bunny_events'
|
5
|
+
require 'bunny_event'
|
6
6
|
|
7
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
8
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -11,50 +11,47 @@ require "bunny_event"
|
|
11
11
|
# require "pry"
|
12
12
|
# Pry.start
|
13
13
|
|
14
|
-
p
|
14
|
+
p 'Defining DummyEvent'
|
15
15
|
class DummyEvent
|
16
16
|
include BunnyEvent
|
17
17
|
|
18
|
-
event_options :
|
19
|
-
:
|
20
|
-
|
18
|
+
event_options exchange: 'test_exchange',
|
19
|
+
exchange_opts: {
|
20
|
+
type: :direct
|
21
21
|
},
|
22
|
-
:
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
}
|
31
|
-
|
22
|
+
queues: {
|
23
|
+
some_queue: {
|
24
|
+
opts: {
|
25
|
+
durable: true
|
26
|
+
},
|
27
|
+
routing_key: ''
|
28
|
+
}
|
29
|
+
}
|
32
30
|
|
33
31
|
def initialize(msg)
|
34
32
|
@message = "My test message is #{msg}"
|
35
33
|
end
|
36
|
-
|
37
34
|
end
|
38
35
|
|
39
|
-
p
|
36
|
+
p 'Publishing event'
|
40
37
|
|
41
38
|
bunny_events = BunnyEvents.new
|
42
|
-
bunny = Bunny.new(
|
39
|
+
bunny = Bunny.new('amqp://rabbitmq:rabbitmq@localhost:5672').start
|
43
40
|
bunny_events.init bunny
|
44
41
|
|
45
|
-
bunny_events.publish DummyEvent.new
|
42
|
+
bunny_events.publish DummyEvent.new 'test'
|
46
43
|
|
47
|
-
p
|
48
|
-
p bunny.exchange_exists?
|
44
|
+
p 'deleting exchane'
|
45
|
+
p bunny.exchange_exists? 'test_exchange'
|
49
46
|
|
50
|
-
x = bunny_events.channels[
|
47
|
+
x = bunny_events.channels['DummyEvent'].exchange 'test_exchange'
|
51
48
|
x.delete
|
52
49
|
|
53
|
-
p
|
54
|
-
p bunny.exchange_exists?
|
50
|
+
p 'Does exchange exist?'
|
51
|
+
p bunny.exchange_exists? 'test_exchange'
|
55
52
|
|
56
|
-
bunny_events.publish DummyEvent.new
|
53
|
+
bunny_events.publish DummyEvent.new 'test'
|
57
54
|
|
58
|
-
p bunny.exchange_exists?
|
55
|
+
p bunny.exchange_exists? 'test_exchange'
|
59
56
|
|
60
|
-
p
|
57
|
+
p 'end'
|
data/bunny_events.gemspec
CHANGED
@@ -1,44 +1,43 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require 'bunny_events/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = 'bunny_events'
|
8
7
|
spec.version = BunnyEvent::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
8
|
+
spec.authors = ['Dean Lovett']
|
9
|
+
spec.email = ['dean.lovett@nexusmods.com']
|
11
10
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
11
|
+
spec.summary = 'A simple gem to define events for messages queues '
|
12
|
+
spec.description = 'This gem allows the use of "Messages" to be defined and published very easily, without your models/controllers having to worry about how messages are produced. Supports AMQP'
|
13
|
+
spec.homepage = 'https://www.nexusmods.com'
|
14
|
+
spec.license = 'MIT'
|
16
15
|
|
17
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
17
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
18
|
if spec.respond_to?(:metadata)
|
20
19
|
|
21
|
-
spec.metadata[
|
22
|
-
spec.metadata[
|
23
|
-
spec.metadata[
|
20
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
21
|
+
spec.metadata['source_code_uri'] = 'https://github.com/Nexus-Mods'
|
22
|
+
spec.metadata['changelog_uri'] = 'https://github.com/Nexus-Mods'
|
24
23
|
else
|
25
|
-
raise
|
26
|
-
|
24
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
25
|
+
'public gem pushes.'
|
27
26
|
end
|
28
27
|
|
29
28
|
# Specify which files should be added to the gem when it is released.
|
30
29
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
-
spec.files
|
30
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
32
31
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
32
|
end
|
34
|
-
spec.bindir =
|
33
|
+
spec.bindir = 'exe'
|
35
34
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
-
spec.require_paths = [
|
35
|
+
spec.require_paths = ['lib']
|
37
36
|
|
38
|
-
spec.add_dependency
|
37
|
+
spec.add_dependency 'bunny', '>= 2.14.0'
|
39
38
|
|
40
|
-
spec.add_development_dependency
|
41
|
-
spec.add_development_dependency
|
42
|
-
spec.add_development_dependency
|
43
|
-
spec.add_development_dependency
|
39
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
40
|
+
spec.add_development_dependency 'bunny-mock'
|
41
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
42
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
44
43
|
end
|
data/lib/bunny_event.rb
CHANGED
@@ -3,11 +3,9 @@ require 'bunny_events'
|
|
3
3
|
# Module that can be included into a ruby class to create a definition of a BunnyEvent. These events can then be published
|
4
4
|
# via the BunnyEvents system.
|
5
5
|
module BunnyEvent
|
6
|
-
|
7
6
|
attr_accessor :message
|
8
7
|
|
9
8
|
module ClassMethods
|
10
|
-
|
11
9
|
# Class method to allow the setting of event options in the message definitions
|
12
10
|
def event_options(options)
|
13
11
|
@options = options
|
@@ -30,4 +28,4 @@ module Exceptions
|
|
30
28
|
class InvalidBunnyConnection < StandardError; end
|
31
29
|
class InvalidBunnyEvent < StandardError; end
|
32
30
|
class InvalidExchange < StandardError; end
|
33
|
-
end
|
31
|
+
end
|
data/lib/bunny_events/version.rb
CHANGED
data/lib/bunny_events.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'bunny'
|
2
2
|
|
3
3
|
class BunnyEvents
|
4
|
-
|
5
4
|
attr_accessor :channels, :bunny_connection
|
6
5
|
|
7
6
|
# Keeps track of which events have been intiailized by this BunnyEvents worker. Used to ensure that the queue and
|
@@ -9,9 +8,9 @@ class BunnyEvents
|
|
9
8
|
attr_accessor :initialized_events
|
10
9
|
|
11
10
|
@@defaults = {
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
exchange: '',
|
12
|
+
exchange_type: :direct,
|
13
|
+
routing_key: 'message_queue_event'
|
15
14
|
}
|
16
15
|
|
17
16
|
# Initialise the BunnyEvents system by accepting a bunny connection.
|
@@ -23,10 +22,9 @@ class BunnyEvents
|
|
23
22
|
# bunny_events.init BunnyMock.new.start
|
24
23
|
#
|
25
24
|
def init(bunny_connection)
|
26
|
-
|
27
25
|
# Ensure the bunny_connection is valid
|
28
26
|
if bunny_connection.nil? || !bunny_connection.respond_to?(:connected?)
|
29
|
-
raise Exceptions::InvalidBunnyConnection
|
27
|
+
raise Exceptions::InvalidBunnyConnection
|
30
28
|
end
|
31
29
|
|
32
30
|
@channels = {}
|
@@ -34,7 +32,6 @@ class BunnyEvents
|
|
34
32
|
@initialized_exchanges = {}
|
35
33
|
|
36
34
|
@bunny_connection = bunny_connection
|
37
|
-
|
38
35
|
end
|
39
36
|
|
40
37
|
def connected?
|
@@ -43,14 +40,11 @@ class BunnyEvents
|
|
43
40
|
|
44
41
|
# Public message. message should be an instance of BaseMessage (or a class with BaseMessage included)
|
45
42
|
def publish(message, routing_key = nil)
|
46
|
-
|
47
43
|
unless message.class.included_modules.include?(BunnyEvent)
|
48
|
-
raise Exceptions::InvalidBunnyEvent
|
44
|
+
raise Exceptions::InvalidBunnyEvent
|
49
45
|
end
|
50
46
|
|
51
|
-
unless connected?
|
52
|
-
raise Exceptions::InvalidBunnyConnection.new
|
53
|
-
end
|
47
|
+
raise Exceptions::InvalidBunnyConnection unless connected?
|
54
48
|
|
55
49
|
# get the options defined by the message queue event class
|
56
50
|
opts = @@defaults.merge message.class.options
|
@@ -63,17 +57,15 @@ class BunnyEvents
|
|
63
57
|
channel = @channels[message.class.name]
|
64
58
|
|
65
59
|
# Ensure that the exchange, queue and binding creation is only performed once
|
66
|
-
if
|
60
|
+
if !@initialized_exchanges.key?(message.class.name) || opts[:always_create_when_publishing]
|
67
61
|
# If the event was sent with an exchange name, create and submit this to the exchange, otherwise, just use the default exchange
|
68
|
-
if !opts[:exchange].nil? && !opts[:exchange].empty?
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
62
|
+
x = if !opts[:exchange].nil? && !opts[:exchange].empty?
|
63
|
+
channel.exchange(opts[:exchange], opts[:exchange_opts] || {})
|
64
|
+
else
|
65
|
+
channel.default_exchange
|
66
|
+
end
|
73
67
|
# if the event was sent with queue definitions, ensure to create the bindings
|
74
|
-
|
75
|
-
handle_queue_definitions channel, x, opts[:queues]
|
76
|
-
end
|
68
|
+
handle_queue_definitions channel, x, opts[:queues] unless opts[:queues].nil?
|
77
69
|
|
78
70
|
# ensure this event's creation params are not processed again
|
79
71
|
@initialized_exchanges[message.class.name] ||= x
|
@@ -83,27 +75,28 @@ class BunnyEvents
|
|
83
75
|
|
84
76
|
# ensure exchange is not null
|
85
77
|
if x.nil? || !@bunny_connection.exchange_exists?(opts[:exchange])
|
86
|
-
raise Exceptions::InvalidExchange
|
78
|
+
raise Exceptions::InvalidExchange
|
87
79
|
end
|
88
80
|
|
89
81
|
# publish message along with the optional routing key
|
90
|
-
x.publish message.message, :
|
82
|
+
x.publish message.message, routing_key: routing_key || opts[:routing_key]
|
91
83
|
end
|
92
84
|
|
93
85
|
private
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
86
|
+
|
87
|
+
def handle_queue_definitions(channel, exchange, queues)
|
88
|
+
queues.each do |q, opts|
|
89
|
+
# Create this queue and bind, if the binding options are present
|
90
|
+
queue = channel.queue q.to_s, opts[:opts] || {}
|
91
|
+
|
92
|
+
# if ignore bind isn't set, set to nil
|
93
|
+
ignore_bind = opts[:ignore_bind] || false
|
94
|
+
|
95
|
+
# if we aren't ignoring the binding for this queue, check if it's already bound. We also shouldn't bind directly
|
96
|
+
# to the default queue
|
97
|
+
if !ignore_bind && !queue.bound_to?(exchange) && exchange.name != ''
|
98
|
+
queue.bind exchange, key: opts[:routing_key] || ''
|
107
99
|
end
|
108
100
|
end
|
109
|
-
end
|
101
|
+
end
|
102
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bunny_events
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dean Lovett
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -39,47 +39,47 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.17'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: bunny-mock
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '10.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '10.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
75
|
+
version: '3.0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
82
|
+
version: '3.0'
|
83
83
|
description: This gem allows the use of "Messages" to be defined and published very
|
84
84
|
easily, without your models/controllers having to worry about how messages are produced.
|
85
85
|
Supports AMQP
|
@@ -91,6 +91,7 @@ extra_rdoc_files: []
|
|
91
91
|
files:
|
92
92
|
- ".gitignore"
|
93
93
|
- ".rspec"
|
94
|
+
- ".rubocop.yml"
|
94
95
|
- ".travis.yml"
|
95
96
|
- Gemfile
|
96
97
|
- Gemfile.lock
|