bunny_exchanges_manager 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
+ SHA1:
3
+ metadata.gz: faca8ad660523c11f6eaac9717e1dcbc6fdc8c3b
4
+ data.tar.gz: 6d83b196164c1d72d18eb74566c4ef2e75d4be69
5
+ SHA512:
6
+ metadata.gz: 857cba4b80f663d1a3e7eb344456de7cea463b78a5135249247edf3b75706ef39877c83bb89db5eb13f65526da047e0e9f71d582d5c8b8e2ad38edaa0f546c21
7
+ data.tar.gz: 672c90b51c55a8da17838b9023e8f3a40c0f0c0353b90d34e4740bcd1377990859d6d6019e8a262040d211578498390806a2571b9fff6194649124d9a0592856
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bunny_exchanges_manager.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 jcabotc
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # BunnyExchangesManager
2
+
3
+ This gem provides a way to create and keep RabbitMQ exchanges using Bunny,
4
+ and keep them to be used.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'bunny_exchanges_manager'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install bunny_exchanges_manager
19
+
20
+ ## Usage
21
+
22
+ To use `BunnyExchanges` you must define the configuration of each exchange in the
23
+ YAML config file.
24
+
25
+ By default the path to the configuration file is `config/exchanges.yml`, but you
26
+ can provide a specific path with:
27
+ ```ruby
28
+ BunnyExchanges.configure do |config|
29
+ config.path = "my/custom/path"
30
+ end
31
+ ```
32
+
33
+ ### The configuration file
34
+
35
+ In the configuration file you have to name and define all your exchanges:
36
+ ```yml
37
+ an_action:
38
+ name: the.exchange.name
39
+ type: fanout
40
+ durable: false
41
+ auto_delete: true
42
+ arguments:
43
+ one_argument: 1
44
+ another_argument: "2"
45
+
46
+ another_action:
47
+ name: another.exchange.name
48
+ type: topic
49
+ ```
50
+
51
+ ### Get exchanges
52
+
53
+ To get a configured exchange:
54
+ ```ruby
55
+ BunnyExchanges.get(:an_action) # => #<Bunny::Exchange:...>
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/[my-github-username]/bunny_exchanges_manager/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bunny_exchanges/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bunny_exchanges_manager"
8
+ spec.version = BunnyExchanges::VERSION
9
+ spec.authors = ["jcabotc"]
10
+ spec.email = ["jcabot@gmail.com"]
11
+ spec.summary = %q{A gem to initialize RabbitMQ exchanges using Bunny on ruby applications}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "bunny"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'yaml'
2
+
3
+ module BunnyExchanges
4
+ # It stores the exchanges configuration.
5
+ class Configuration
6
+ DEFAULT_EXCHANGES_PATH = "config/exchanges.yml"
7
+ DEFAULT_RABBITMQ_PATH = "config/rabbitmq.yml"
8
+
9
+ # Sets the path of the exchanges configuration file.
10
+ #
11
+ # @param [String] a path.
12
+ attr_writer :exchanges_path
13
+
14
+ # Sets the path of the rabbitmq configuration file.
15
+ #
16
+ # @param [String] a path.
17
+ attr_writer :rabbitmq_path
18
+
19
+ # Sets the ENV. If it is set, the rabbitmq configuration
20
+ # for that env is taken, otherwise it gets the rabbitmq config file
21
+ # contents directly.
22
+ #
23
+ # @param [String] the env.
24
+ attr_writer :env
25
+
26
+ # Constructor.
27
+ def initialize
28
+ @exchanges_path = DEFAULT_EXCHANGES_PATH
29
+ @rabbitmq_path = DEFAULT_RABBITMQ_PATH
30
+ end
31
+
32
+ # Loads the configuration YAML file contents.
33
+ #
34
+ # @return [Hash] the exchanges configuration.
35
+ def exchanges
36
+ @exchanges ||= YAML.load_file(exchanges_path)
37
+ end
38
+
39
+ # Loads the configuration YAML file contents for the environment if given.
40
+ #
41
+ # @return [Hash] the rabbitmq configuration.
42
+ def rabbitmq
43
+ @rabbitmq ||= if env
44
+ rabbitmq_contents.fetch(env)
45
+ else
46
+ rabbitmq_contents
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def rabbitmq_contents
53
+ YAML.load_file(rabbitmq_path)
54
+ end
55
+
56
+ attr_reader :env, :exchanges_path, :rabbitmq_path
57
+ end
58
+ end
@@ -0,0 +1,65 @@
1
+ require 'bunny'
2
+
3
+ module BunnyExchanges
4
+ # The required exchange is not defined.
5
+ UndefinedExchange = Class.new(StandardError)
6
+
7
+ # Class keeps all configured exchanges through the same connection and channel.
8
+ class Manager
9
+ # Constructor.
10
+ #
11
+ # @param [BunnyExchanges::Configuration] the exchanges configuration.
12
+ def initialize config = BunnyExchanges.configuration
13
+ @config = config
14
+ @exchanges = {}
15
+ end
16
+
17
+ # Gets or builds the required exchange.
18
+ #
19
+ # @param [Symbol] the action name.
20
+ # @return [Bunny::Exchange] the required bunny exchange.
21
+ # @raise [BunnyExchanges::Manager::UndefinedExchange] when the exchange is not defined.
22
+ def get action
23
+ exchanges[action.to_sym] ||= begin
24
+ name, options = params_for(action)
25
+
26
+ channel.exchange(name, options)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :exchanges, :config
33
+
34
+ def params_for action
35
+ options = options_for(action)
36
+ name = options.delete(:name)
37
+
38
+ [name, options]
39
+ end
40
+
41
+ def options_for action
42
+ config_for(action).reduce({}) do |hash, (option, value)|
43
+ hash.merge! option.to_sym => value
44
+ end
45
+ end
46
+
47
+ def config_for action
48
+ config.exchanges.fetch(action.to_s) { raise_undefined(action) }
49
+ end
50
+
51
+ def channel
52
+ @channel ||= begin
53
+ conn = Bunny.new(config.rabbitmq)
54
+ conn.start
55
+
56
+ conn.create_channel
57
+ end
58
+ end
59
+
60
+ def raise_undefined action
61
+ raise BunnyExchanges::UndefinedExchange,
62
+ "No exchange is defined for action #{action}."
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module BunnyExchanges
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,39 @@
1
+ require "bunny_exchanges/version"
2
+
3
+ require "bunny_exchanges/configuration"
4
+ require "bunny_exchanges/manager"
5
+
6
+ module BunnyExchanges
7
+ # BunnyExchanges configuration
8
+ #
9
+ # @return [Configuration] the current configuration
10
+ def self.configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ # A helper to configure BunnyExchanges
15
+ #
16
+ # @yield [configuration] Configures tenantify
17
+ def self.configure
18
+ yield configuration
19
+ end
20
+
21
+ # Delegates `#get` to the current manager.
22
+ # Returns the required exchange.
23
+ #
24
+ # @param [Symbol, String] the action name
25
+ # @return [Bunny::Exchange] the required exchange.
26
+ # @raise [BunnyExchanges::UndefinedExchange] when the required example is not defined.
27
+ # @see Tenant.using
28
+ def self.get action
29
+ manager.get(action)
30
+ end
31
+
32
+ # The current instance of {BunnyExchanges::Manager}.
33
+ #
34
+ # @return [BunnyExchanges::Manager] the manager with the current configuration.
35
+ # @see Tenant.using
36
+ def self.manager
37
+ @manager ||= Manager.new(configuration)
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ action_1:
2
+ name: an.exchange.name
3
+ type: fanout
4
+ durable: true
5
+ auto_delete: false
6
+ arguments:
7
+ one: 1
8
+ two: second
9
+ action_2:
10
+ name: the.name
11
+ action_3:
12
+ name: another
13
+ type: topic
@@ -0,0 +1,5 @@
1
+ development:
2
+ host: development_host
3
+
4
+ test:
5
+ host: test_host
@@ -0,0 +1,73 @@
1
+ require 'bunny_exchanges/configuration'
2
+
3
+ RSpec.describe BunnyExchanges::Configuration do
4
+
5
+ subject { described_class.new }
6
+
7
+ describe '#exchanges_path= and #exchanges' do
8
+ let :config_file do
9
+ File.expand_path("../configuration/test_exchanges_configuration.yml" ,__FILE__)
10
+ end
11
+
12
+ let :expected_exchanges do
13
+ {
14
+ "action_1" => {
15
+ "name" => "an.exchange.name",
16
+ "type" => "fanout",
17
+ "durable" => true,
18
+ "auto_delete" => false,
19
+ "arguments" => {
20
+ "one" => 1,
21
+ "two" => "second"
22
+ }
23
+ },
24
+ "action_2" => {
25
+ "name" => "the.name"
26
+ },
27
+ "action_3" => {
28
+ "name" => "another",
29
+ "type" => "topic"
30
+ }
31
+ }
32
+ end
33
+
34
+ it 'parses the exchanges config from a yaml file' do
35
+ subject.exchanges_path = config_file
36
+
37
+ expect(subject.exchanges).to eq expected_exchanges
38
+ end
39
+ end
40
+
41
+ describe '#rabbitmq_path= and #rabbitmq' do
42
+ let :config_file do
43
+ File.expand_path("../configuration/test_rabbitmq_configuration.yml" ,__FILE__)
44
+ end
45
+
46
+ context 'without environment' do
47
+ let :expected_rabbitmq do
48
+ {
49
+ "development" => {"host" => "development_host"},
50
+ "test" => {"host" => "test_host"}
51
+ }
52
+ end
53
+
54
+ it 'parses the rabbitmq config from a yaml file' do
55
+ subject.rabbitmq_path = config_file
56
+
57
+ expect(subject.rabbitmq).to eq expected_rabbitmq
58
+ end
59
+ end
60
+
61
+ context 'with environment' do
62
+ let(:expected_rabbitmq) { { "host" => "test_host" } }
63
+
64
+ it 'parses the rabbitmq config from a yaml file' do
65
+ subject.rabbitmq_path = config_file
66
+ subject.env = "test"
67
+
68
+ expect(subject.rabbitmq).to eq expected_rabbitmq
69
+ end
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,56 @@
1
+ require 'bunny_exchanges/manager'
2
+
3
+ RSpec.describe BunnyExchanges::Manager do
4
+
5
+ let :exchanges_config do
6
+ {
7
+ "action_1" => {
8
+ "name" => "an.exchange.name",
9
+ "type" => "fanout",
10
+ "durable" => false,
11
+ "auto_delete" => true,
12
+ "arguments" => {
13
+ "one" => 1,
14
+ "two" => "second"
15
+ }
16
+ },
17
+ "action_2" => {
18
+ "name" => "the.name"
19
+ }
20
+ }
21
+ end
22
+
23
+ let :config do
24
+ double 'config', :exchanges => exchanges_config,
25
+ :rabbitmq => {}
26
+ end
27
+
28
+ subject { described_class.new config }
29
+
30
+ describe '#get' do
31
+ context 'with a defined exchange' do
32
+ it 'builds and caches the exchange' do
33
+ exchange = subject.get(:action_1)
34
+
35
+ expect(exchange).to be_a Bunny::Exchange
36
+
37
+ expect(exchange.name ).to eq "an.exchange.name"
38
+ expect(exchange.type ).to eq "fanout"
39
+ expect(exchange.durable? ).to eq false
40
+ expect(exchange.auto_delete?).to eq true
41
+ expect(exchange.arguments ).to eq "one" => 1, "two" => "second"
42
+
43
+ exchange_again = subject.get(:action_1)
44
+ expect(exchange.object_id).to eq exchange_again.object_id
45
+ end
46
+ end
47
+
48
+ context 'with an undefined exchange' do
49
+ it 'raises error' do
50
+ expect{ subject.get(:action_3) }.
51
+ to raise_error BunnyExchanges::UndefinedExchange
52
+ end
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,20 @@
1
+ require 'bunny_exchanges'
2
+
3
+ RSpec.describe "configure and use" do
4
+
5
+ it 'works properly' do
6
+ BunnyExchanges.configure do |config|
7
+ config.exchanges_path = File.expand_path("../test_exchanges_config.yml", __FILE__)
8
+ config.rabbitmq_path = File.expand_path("../test_rabbitmq_config.yml", __FILE__)
9
+ config.env = "test"
10
+ end
11
+
12
+ exchange = BunnyExchanges.get(:the_action)
13
+
14
+ expect(exchange).to be_a Bunny::Exchange
15
+
16
+ expect(exchange.name).to eq "a.real.exchange"
17
+ expect(exchange.type).to eq "fanout"
18
+ end
19
+
20
+ end
@@ -0,0 +1,8 @@
1
+ the_action:
2
+ name: a.real.exchange
3
+ type: fanout
4
+
5
+ another_action:
6
+ name: another.exchange
7
+ type: topic
8
+ durable: true
@@ -0,0 +1,5 @@
1
+ development:
2
+ host: localhost
3
+
4
+ test:
5
+ host: localhost
@@ -0,0 +1,18 @@
1
+ require 'pry'
2
+
3
+ RSpec.configure do |config|
4
+
5
+ config.expect_with :rspec do |expectations|
6
+ # Best error messages on chained expectations
7
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
8
+ end
9
+
10
+ config.mock_with :rspec do |mocks|
11
+ # Prevents you from stubbing a method that does not exist on a real object
12
+ mocks.verify_partial_doubles = true
13
+ end
14
+
15
+ config.disable_monkey_patching!
16
+ config.order = :random
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bunny_exchanges_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jcabotc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-25 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
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - jcabot@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bunny_exchanges_manager.gemspec
97
+ - lib/bunny_exchanges.rb
98
+ - lib/bunny_exchanges/configuration.rb
99
+ - lib/bunny_exchanges/manager.rb
100
+ - lib/bunny_exchanges/version.rb
101
+ - spec/bunny_exchanges/configuration/test_exchanges_configuration.yml
102
+ - spec/bunny_exchanges/configuration/test_rabbitmq_configuration.yml
103
+ - spec/bunny_exchanges/configuration_spec.rb
104
+ - spec/bunny_exchanges/manager_spec.rb
105
+ - spec/integration/configure_and_use.rb
106
+ - spec/integration/test_exchanges_config.yml
107
+ - spec/integration/test_rabbitmq_config.yml
108
+ - spec/spec_helper.rb
109
+ homepage: ''
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.2.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: A gem to initialize RabbitMQ exchanges using Bunny on ruby applications
133
+ test_files:
134
+ - spec/bunny_exchanges/configuration/test_exchanges_configuration.yml
135
+ - spec/bunny_exchanges/configuration/test_rabbitmq_configuration.yml
136
+ - spec/bunny_exchanges/configuration_spec.rb
137
+ - spec/bunny_exchanges/manager_spec.rb
138
+ - spec/integration/configure_and_use.rb
139
+ - spec/integration/test_exchanges_config.yml
140
+ - spec/integration/test_rabbitmq_config.yml
141
+ - spec/spec_helper.rb