bunny_events 0.1.1

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: a636e6dfacc607d7b8c4c85de42c882b3e6486c38901ef0cf94c645ac7c47e87
4
+ data.tar.gz: b49ffde771cb39c7e2404603d4eb913154d1fcfdedae36b14fd400f71e2778af
5
+ SHA512:
6
+ metadata.gz: a96296e85bd6c27d9ebbd6d19c9f81a8da8b1307a97d0c0d0c03f8be939a6f34f7fdf40dcdf82f6871682916ba681e97bcf4bed20c32046c982f1825bec87a82
7
+ data.tar.gz: edbf92c0720199995875b668dbafec22f8a0b55782457625ff834ba8751eac45151a92a73e5b299d0df79a3e60827480eea3327ed07b011ff05bdc1a921eff8c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in message_queue_event.gemspec
6
+ gemspec
7
+
8
+ gem "bunny", ">= 2.14.0"
9
+ gem 'bunny-mock'
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bunny_events (0.1.1)
5
+ bunny (>= 2.14.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ amq-protocol (2.3.0)
11
+ bunny (2.14.2)
12
+ amq-protocol (~> 2.3, >= 2.3.0)
13
+ bunny-mock (1.7.0)
14
+ bunny (>= 1.7)
15
+ diff-lcs (1.3)
16
+ rake (10.5.0)
17
+ rspec (3.8.0)
18
+ rspec-core (~> 3.8.0)
19
+ rspec-expectations (~> 3.8.0)
20
+ rspec-mocks (~> 3.8.0)
21
+ rspec-core (3.8.2)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-expectations (3.8.4)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-mocks (3.8.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-support (3.8.2)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 1.17)
36
+ bunny (>= 2.14.0)
37
+ bunny-mock
38
+ bunny_events!
39
+ rake (~> 10.0)
40
+ rspec (~> 3.0)
41
+
42
+ BUNDLED WITH
43
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Dean
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,131 @@
1
+ # Bunny Events
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/message_queue_event.svg)](https://badge.fury.io/rb/message_queue_event)
4
+
5
+ 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
+
7
+ Rather than usin Bunny directly, this gem allows an application to define "Event Definitions" which can be defined and published
8
+ in a modular way. This ensures that when you are producing a message, your application logic shouldn't care about how the
9
+ message is produced (E.g. your controller shouldn't care or know anything about what exchange to publish a message to, only the BunnyEvent
10
+ that has been defined cares)
11
+
12
+ Current Features and limitation:
13
+
14
+ - Allows a bunny connection to initialise the system
15
+ - Allows the definition of abstract events to be used application-wide
16
+ - Customization of exchange and queue options when producing an event
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'bunny_events'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install bunny_events
33
+
34
+ ## Usage
35
+
36
+ ### Defining an event
37
+
38
+ To produce an event to the message queue, we must first define an event. In `app/events/my_test_event.rb`
39
+
40
+ ```ruby
41
+ class MyTestEvent
42
+ include BunnyEvent
43
+
44
+ # define the event options for queueing this event. Each event type can have different options.
45
+ event_options :exchange => "test_exchange",
46
+ :exchange_type => :fanout
47
+
48
+ # We can define what the message payload looks like here.
49
+ def initialize(msg)
50
+ @message = "My test message is #{msg}"
51
+ end
52
+ end
53
+
54
+ ```
55
+
56
+ ### Changing the message payload
57
+
58
+ We can change the payload in the `initialize` method, allowing us complete control over what data is used to create the message.
59
+
60
+ ```ruby
61
+ def initialize(user, page)
62
+ @message = {
63
+ :user_id => user.id,
64
+ :user_name => user.name,
65
+ :page => page.url,
66
+ :timestamp => Time.now.to_i
67
+ }.to_json
68
+ end
69
+ ```
70
+
71
+ This ensures complete control over how your application produces a message, enabling your application to utilise JSON, AVRO, or just plain old strings.
72
+
73
+ ### Publish event
74
+
75
+ Publishing the event requires the use of the BunnyEvents class
76
+
77
+ ```ruby
78
+ # Create event, passing in whatever data is needed
79
+ event = MyTestEvent.new "This is a test event"
80
+
81
+ # Use the BunnyEvents system to publish this event
82
+ BunnyEvents.publish event
83
+ ```
84
+
85
+ ### Full example with initialisation
86
+
87
+ ```ruby
88
+
89
+ # This is done once as part of the configuration step, usually in a rails initializer, or at the start of your application
90
+ BunnyEvents.init Bunny.new("amqp://rabbitmq:rabbitmq@rabbit1:5672").start
91
+
92
+ # Event definitions are defined in classes, in rails, we generally use app/messages
93
+ class MyTestEvent
94
+ include BunnyEvent
95
+
96
+ # define the event options for queueing this event. Each event type can have different options.
97
+ event_options :exchange => "test_exchange",
98
+ :exchange_type => :fanout,
99
+ :bindings => {
100
+ :queue_1 => {
101
+ :routing_key => ""
102
+ },
103
+ :queue_2 => {
104
+ :routing_key => ""
105
+ },
106
+ }
107
+
108
+ # We can define what the message payload looks like here.
109
+ def initialize(msg)
110
+ @message = "My test message is #{msg}"
111
+ end
112
+ end
113
+
114
+ # When we want to create a new instance of an event, we create and publish the object
115
+ event = MyTestEvent.new "test"
116
+ BunnyEvents.publish event
117
+ ```
118
+
119
+ ## Development
120
+
121
+ 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.
122
+
123
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
124
+
125
+ ## Contributing
126
+
127
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Nexus-Mods/message_queue_event.
128
+
129
+ ## License
130
+
131
+ 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,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bunny_events"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,32 @@
1
+ require 'bunny_events'
2
+
3
+ # Module that can be included into a ruby class to create a definition of a BunnyEvent. These events can then be published
4
+ # via the BunnyEvents system.
5
+ module BunnyEvent
6
+
7
+ attr_accessor :message
8
+
9
+ module ClassMethods
10
+
11
+ # Class method to allow the setting of event options in the message definitions
12
+ def event_options(options)
13
+ @options = options
14
+ end
15
+
16
+ # Class method for retreiving the data via the `MyMessage.class.options`
17
+ def options
18
+ @options
19
+ end
20
+ end
21
+
22
+ class << self
23
+ def included(base)
24
+ base.extend ClassMethods
25
+ end
26
+ end
27
+ end
28
+
29
+ module Exceptions
30
+ class InvalidBunnyConnection < StandardError; end
31
+ class InvalidBunnyEvent < StandardError; end
32
+ end
@@ -0,0 +1,3 @@
1
+ module BunnyEvent
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,84 @@
1
+ require 'bunny'
2
+
3
+ class BunnyEvents
4
+
5
+ class << self
6
+ # Class instance variables, for:
7
+ # - keeping track of all our active channels (one for each type of event)
8
+ # - our active connection to bunny (one for the whole application)
9
+ attr_accessor :channels, :bunny_connection
10
+ end
11
+
12
+ @@defaults = {
13
+ :exchange => "",
14
+ :exchange_type => :direct,
15
+ :routing_key => "message_queue_event"
16
+ }
17
+
18
+ # Initialise the BunnyEvents system by accepting a bunny connection.
19
+ #
20
+ # Example:
21
+ #
22
+ # This can also accept bunnymock for testing
23
+ # BunnyEvents.init BunnyMock.new.start
24
+ #
25
+ def self.init(bunny_connection)
26
+
27
+ # Ensure the bunny_connection is valid
28
+ if bunny_connection.nil? || !bunny_connection.respond_to?(:connected?)
29
+ raise Exceptions::InvalidBunnyConnection.new
30
+ end
31
+
32
+ @bunny_connection = bunny_connection
33
+
34
+ end
35
+
36
+ def self.connected?
37
+ @bunny_connection&.connected? || false
38
+ end
39
+
40
+ # Public message. message should be an instance of BaseMessage (or a class with BaseMessage included)
41
+ def self.publish(message)
42
+
43
+ unless message.class.included_modules.include?(BunnyEvent)
44
+ raise Exceptions::InvalidBunnyEvent.new
45
+ end
46
+
47
+ unless connected?
48
+ throw "Not connected"
49
+ end
50
+
51
+ # If there are no channels, or this message's key does not appear in our channel list, create a new channel
52
+ if @channels.nil?
53
+ @channels = {}
54
+ end
55
+
56
+ # get the options defined by the message queue event class
57
+ opts = @@defaults.merge message.class.options
58
+
59
+ # Use the class name to determine which channel to use
60
+ unless @channels.key?(message.class.name)
61
+ @channels[message.class.name] = @bunny_connection.create_channel
62
+ end
63
+
64
+ channel = @channels[message.class.name]
65
+
66
+ # If our message was sent with an exchange name, create and submit this to the exchange, otherwise, just use the default exchange
67
+ if !opts[:exchange].nil? && !opts[:exchange].empty?
68
+ x = channel.exchange(opts[:exchange], {:type => opts[:exchange_type] || :direct})
69
+ else
70
+ x = channel.default_exchange
71
+ end
72
+
73
+ # if your event was sent with queue bindings, ensure to create the queue and bindings
74
+ if !opts[:bindings].nil?
75
+ opts[:bindings].each do |q, binding|
76
+ queue = channel.queue q.to_s
77
+ queue.bind x, routing_key: binding[:routing_key]
78
+ end
79
+ end
80
+
81
+ x.publish message.message, :routing_key => opts[:routing_key]
82
+
83
+ end
84
+ end
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bunny_events/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bunny_events"
8
+ spec.version = BunnyEvent::VERSION
9
+ spec.authors = ["Dean Lovett"]
10
+ spec.email = ["dean.lovett@nexusmods.com"]
11
+
12
+ spec.summary = %q{A simple gem to define events for messages queues }
13
+ spec.description = %q{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}
14
+ spec.homepage = "https://www.nexusmods.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/Nexus-Mods"
23
+ spec.metadata["changelog_uri"] = "https://github.com/Nexus-Mods"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_dependency "bunny", ">= 2.14.0"
39
+
40
+ spec.add_development_dependency "bundler", "~> 1.17"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bunny_events
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dean Lovett
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-30 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: 2.14.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.14.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: This gem allows the use of "Messages" to be defined and published very
70
+ easily, without your models/controllers having to worry about how messages are produced.
71
+ Supports AMQP
72
+ email:
73
+ - dean.lovett@nexusmods.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/bunny_event.rb
89
+ - lib/bunny_events.rb
90
+ - lib/bunny_events/version.rb
91
+ - message_queue_event.gemspec
92
+ homepage: https://www.nexusmods.com
93
+ licenses:
94
+ - MIT
95
+ metadata:
96
+ homepage_uri: https://www.nexusmods.com
97
+ source_code_uri: https://github.com/Nexus-Mods
98
+ changelog_uri: https://github.com/Nexus-Mods
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A simple gem to define events for messages queues
118
+ test_files: []