bunny_exchanges_manager 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e65427261ca8ffce3f3c5a6f848e5a104ec410cc
4
- data.tar.gz: b308f7271cf13be7e4c8c562332cab2a8fd5df23
3
+ metadata.gz: d0d7437554c91e5213ef1ef3fb04231742f8d6c2
4
+ data.tar.gz: c6d113785c87550cde168031017a4a9458d6c06c
5
5
  SHA512:
6
- metadata.gz: cb2c99134815def9738a0abf645466bcc488fa44ee805e70d1ba1f47ad2c9e337f596d42e71b59633dd7365ecd0432cde1712ba0fd3db7f5122336724b7f0f4b
7
- data.tar.gz: 5d08b1a43a2530822b5f6723ceee410d6276100840d29229f67d28cab4b8f82d4c1c1945d2329b2cc3f596f89323a7d1697dd3ddaf115f6d5675de8e60d27c35
6
+ metadata.gz: f68af147380bac85e39d85c97380d6d043b296a59de75179fbcf6eba25c287809e79c0a025af18f50ca55e417dfd9beb8bf276f6a772d0e4125c50aec371ba12
7
+ data.tar.gz: 6dba6b53bdb6c0c9a682f1100abb55481fe0d63a64234fcf6e2b3daf8eff9ad05cfbadcd84c9147d33492ce77e11603080a5e25bedfa0381b98a0f390d754168
data/.gitignore CHANGED
@@ -20,3 +20,5 @@ tmp
20
20
  *.o
21
21
  *.a
22
22
  mkmf.log
23
+ /nbproject
24
+ .ruby-gemset
data/README.md CHANGED
@@ -26,7 +26,7 @@ By default the path to the configuration file is `config/exchanges.yml`, but you
26
26
  can provide a specific path with:
27
27
  ```ruby
28
28
  BunnyExchanges.configure do |config|
29
- config.path = "my/custom/path"
29
+ config.exchanges_path = "my/custom/path"
30
30
  end
31
31
  ```
32
32
 
@@ -55,6 +55,67 @@ To get a configured exchange:
55
55
  BunnyExchanges.get(:an_action) # => #<Bunny::Exchange:...>
56
56
  ```
57
57
 
58
+ Optionally, if you configured many connections, you can specify it like this:
59
+ ```ruby
60
+ BunnyExchanges.get(:an_action, connection_name: :another) # => #<Bunny::Exchange:...>
61
+ ```
62
+
63
+ ### Connections
64
+
65
+ The default connection config file path is `config/rabbitmq.yml`, although you
66
+ can provide as much connections as needed with:
67
+ ```ruby
68
+ BunnyExchanges.configure do |config|
69
+ config.connections = {
70
+ :default => "config/rabbitmq/default.yml",
71
+ :another => "config/rabbitmq/another.yml",
72
+ }
73
+ end
74
+ ```
75
+
76
+ The connection file needs to have the same parameters `Bunny.new` can accept,
77
+ for example:
78
+
79
+ ```yml
80
+ :host: localhost
81
+ :port: 5672
82
+ :user: guest
83
+ :pass: guest
84
+ :vhost: /
85
+ :threaded: true
86
+ :heartbeat: 2
87
+ ```
88
+
89
+ Or it can also have a connection definition for each environment of your app:
90
+
91
+ ```yml
92
+ development:
93
+ :host: localhost
94
+ :port: 5672
95
+ :user: guest
96
+ :pass: guest
97
+ :vhost: /
98
+ :threaded: true
99
+ :heartbeat: 2
100
+
101
+ test:
102
+ :host: localhost
103
+ :port: 5672
104
+ :user: guest
105
+ :pass: guest
106
+ :vhost: /
107
+ :threaded: true
108
+ :heartbeat: 2
109
+ ```
110
+
111
+ In case you want to use this feature, you must provide the environment in an initializer:
112
+
113
+ ```ruby
114
+ BunnyExchanges.configure do |config|
115
+ config.env = Rails.env
116
+ end
117
+ ```
118
+
58
119
  ### Pre-forking servers
59
120
 
60
121
  There are situations when you need to reconnect to your AMQP server.
@@ -6,7 +6,7 @@ require 'bunny_exchanges/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "bunny_exchanges_manager"
8
8
  spec.version = BunnyExchanges::VERSION
9
- spec.authors = ["jcabotc"]
9
+ spec.authors = ["jcabotc", "graimon"]
10
10
  spec.email = ["jcabot@gmail.com"]
11
11
  spec.summary = %q{A gem to initialize RabbitMQ exchanges using Bunny on ruby applications}
12
12
  spec.homepage = ""
@@ -21,12 +21,12 @@ module BunnyExchanges
21
21
  # Delegates `#get` to the current manager.
22
22
  # Returns the required exchange.
23
23
  #
24
- # @param [Symbol, String] the action name
24
+ # @param [Symbol, Hash] the action name and the connection_name
25
25
  # @return [Bunny::Exchange] the required exchange.
26
26
  # @raise [BunnyExchanges::UndefinedExchange] when the required example is not defined.
27
27
  # @see Tenant.using
28
- def self.get action
29
- manager.get(action)
28
+ def self.get action, connection_name: :default
29
+ manager.get(action, connection_name)
30
30
  end
31
31
 
32
32
  # Removes the current manager and starts a new one with the same configuration.
@@ -11,10 +11,10 @@ module BunnyExchanges
11
11
  # @param [String] a path.
12
12
  attr_writer :exchanges_path
13
13
 
14
- # Sets the path of the rabbitmq configuration file.
14
+ # Sets the hash path of the rabbitmq configuration file.
15
15
  #
16
- # @param [String] a path.
17
- attr_writer :rabbitmq_path
16
+ # @param [Hash] {:connection_name => "path_to_config.yml"}.
17
+ attr_writer :connections
18
18
 
19
19
  # Sets the ENV. If it is set, the rabbitmq configuration
20
20
  # for that env is taken, otherwise it gets the rabbitmq config file
@@ -26,7 +26,8 @@ module BunnyExchanges
26
26
  # Constructor.
27
27
  def initialize
28
28
  @exchanges_path = DEFAULT_EXCHANGES_PATH
29
- @rabbitmq_path = DEFAULT_RABBITMQ_PATH
29
+ @connections = {default: DEFAULT_RABBITMQ_PATH}
30
+ @configs = {}
30
31
  end
31
32
 
32
33
  # Loads the configuration YAML file contents.
@@ -39,20 +40,20 @@ module BunnyExchanges
39
40
  # Loads the configuration YAML file contents for the environment if given.
40
41
  #
41
42
  # @return [Hash] the rabbitmq configuration.
42
- def rabbitmq
43
- @rabbitmq ||= if env
44
- rabbitmq_contents.fetch(env)
45
- else
46
- rabbitmq_contents
47
- end
43
+ def connection_config connection_name
44
+ @configs[connection_name] ||= if env
45
+ rabbitmq_contents(connection_name).fetch(env)
46
+ else
47
+ rabbitmq_contents connection_name
48
+ end
48
49
  end
49
50
 
50
51
  private
51
52
 
52
- def rabbitmq_contents
53
- YAML.load_file(rabbitmq_path)
53
+ def rabbitmq_contents connection_name
54
+ YAML.load_file(connections[connection_name])
54
55
  end
55
56
 
56
- attr_reader :env, :exchanges_path, :rabbitmq_path
57
+ attr_reader :env, :exchanges_path, :connections
57
58
  end
58
59
  end
@@ -12,18 +12,19 @@ module BunnyExchanges
12
12
  def initialize config = BunnyExchanges.configuration
13
13
  @config = config
14
14
  @exchanges = {}
15
+ @channels = {}
15
16
  end
16
17
 
17
18
  # Gets or builds the required exchange.
18
19
  #
19
- # @param [Symbol] the action name.
20
+ # @param [Symbol, Symbol] the action name and the connection name
20
21
  # @return [Bunny::Exchange] the required bunny exchange.
21
22
  # @raise [BunnyExchanges::Manager::UndefinedExchange] when the exchange is not defined.
22
- def get action
23
+ def get action, connection_name
23
24
  exchanges[action.to_sym] ||= begin
24
25
  name, options = params_for(action)
25
26
 
26
- channel.exchange(name, options)
27
+ channel_for(connection_name).exchange(name, options)
27
28
  end
28
29
  end
29
30
 
@@ -48,9 +49,9 @@ module BunnyExchanges
48
49
  config.exchanges.fetch(action.to_s) { raise_undefined(action) }
49
50
  end
50
51
 
51
- def channel
52
- @channel ||= begin
53
- conn = Bunny.new(config.rabbitmq)
52
+ def channel_for connection_name
53
+ @channels[connection_name] ||= begin
54
+ conn = Bunny.new(config.connection_config(connection_name))
54
55
  conn.start
55
56
 
56
57
  conn.create_channel
@@ -1,3 +1,3 @@
1
1
  module BunnyExchanges
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -38,7 +38,7 @@ RSpec.describe BunnyExchanges::Configuration do
38
38
  end
39
39
  end
40
40
 
41
- describe '#rabbitmq_path= and #rabbitmq' do
41
+ describe '#connections and #connection_config' do
42
42
  let :config_file do
43
43
  File.expand_path("../configuration/test_rabbitmq_configuration.yml" ,__FILE__)
44
44
  end
@@ -52,9 +52,9 @@ RSpec.describe BunnyExchanges::Configuration do
52
52
  end
53
53
 
54
54
  it 'parses the rabbitmq config from a yaml file' do
55
- subject.rabbitmq_path = config_file
55
+ subject.connections = {default: config_file}
56
56
 
57
- expect(subject.rabbitmq).to eq expected_rabbitmq
57
+ expect(subject.connection_config(:default)).to eq expected_rabbitmq
58
58
  end
59
59
  end
60
60
 
@@ -62,10 +62,10 @@ RSpec.describe BunnyExchanges::Configuration do
62
62
  let(:expected_rabbitmq) { { "host" => "test_host" } }
63
63
 
64
64
  it 'parses the rabbitmq config from a yaml file' do
65
- subject.rabbitmq_path = config_file
66
- subject.env = "test"
65
+ subject.connections = {default: config_file}
66
+ subject.env = "test"
67
67
 
68
- expect(subject.rabbitmq).to eq expected_rabbitmq
68
+ expect(subject.connection_config(:default)).to eq expected_rabbitmq
69
69
  end
70
70
  end
71
71
  end
@@ -21,16 +21,18 @@ RSpec.describe BunnyExchanges::Manager do
21
21
  end
22
22
 
23
23
  let :config do
24
- double 'config', :exchanges => exchanges_config,
25
- :rabbitmq => {}
24
+ double 'config', :exchanges => exchanges_config
26
25
  end
27
26
 
28
27
  subject { described_class.new config }
29
28
 
30
29
  describe '#get' do
31
30
  context 'with a defined exchange' do
31
+
32
32
  it 'builds and caches the exchange' do
33
- exchange = subject.get(:action_1)
33
+ expect(config).to receive(:connection_config).with(:default).and_return({})
34
+
35
+ exchange = subject.get(:action_1, :default)
34
36
 
35
37
  expect(exchange).to be_a Bunny::Exchange
36
38
 
@@ -40,17 +42,33 @@ RSpec.describe BunnyExchanges::Manager do
40
42
  expect(exchange.auto_delete?).to eq true
41
43
  expect(exchange.arguments ).to eq "one" => 1, "two" => "second"
42
44
 
43
- exchange_again = subject.get(:action_1)
45
+ exchange_again = subject.get(:action_1, :default)
44
46
  expect(exchange.object_id).to eq exchange_again.object_id
45
47
  end
46
48
  end
47
49
 
48
50
  context 'with an undefined exchange' do
49
51
  it 'raises error' do
50
- expect{ subject.get(:action_3) }.
52
+ expect{ subject.get(:action_3, :default) }.
51
53
  to raise_error BunnyExchanges::UndefinedExchange
52
54
  end
53
55
  end
56
+
57
+ context 'with another connection' do
58
+ it 'asks for the another connection' do
59
+ expect(config).to receive(:connection_config).with(:another).and_return({})
60
+
61
+ exchange = subject.get(:action_1, :another)
62
+
63
+ expect(exchange).to be_a Bunny::Exchange
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '#get with another connection' do
69
+ before do
70
+ expect(config).to receive(:connection_config).with(:default).and_return({})
71
+ end
54
72
  end
55
73
 
56
74
  end
@@ -5,7 +5,7 @@ RSpec.describe "configure and use" do
5
5
  it 'works properly' do
6
6
  BunnyExchanges.configure do |config|
7
7
  config.exchanges_path = File.expand_path("../test_exchanges_config.yml", __FILE__)
8
- config.rabbitmq_path = File.expand_path("../test_rabbitmq_config.yml", __FILE__)
8
+ config.connections = {default: File.expand_path("../test_rabbitmq_config.yml", __FILE__)}
9
9
  config.env = "test"
10
10
  end
11
11
 
metadata CHANGED
@@ -1,83 +1,84 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunny_exchanges_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jcabotc
8
+ - graimon
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2016-05-13 00:00:00.000000000 Z
12
+ date: 2016-07-07 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bunny
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - '>='
18
+ - - ">="
18
19
  - !ruby/object:Gem::Version
19
20
  version: '0'
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
- - - '>='
25
+ - - ">="
25
26
  - !ruby/object:Gem::Version
26
27
  version: '0'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: bundler
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - ~>
32
+ - - "~>"
32
33
  - !ruby/object:Gem::Version
33
34
  version: '1.6'
34
35
  type: :development
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - ~>
39
+ - - "~>"
39
40
  - !ruby/object:Gem::Version
40
41
  version: '1.6'
41
42
  - !ruby/object:Gem::Dependency
42
43
  name: rspec
43
44
  requirement: !ruby/object:Gem::Requirement
44
45
  requirements:
45
- - - '>='
46
+ - - ">="
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  type: :development
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
52
- - - '>='
53
+ - - ">="
53
54
  - !ruby/object:Gem::Version
54
55
  version: '0'
55
56
  - !ruby/object:Gem::Dependency
56
57
  name: pry
57
58
  requirement: !ruby/object:Gem::Requirement
58
59
  requirements:
59
- - - '>='
60
+ - - ">="
60
61
  - !ruby/object:Gem::Version
61
62
  version: '0'
62
63
  type: :development
63
64
  prerelease: false
64
65
  version_requirements: !ruby/object:Gem::Requirement
65
66
  requirements:
66
- - - '>='
67
+ - - ">="
67
68
  - !ruby/object:Gem::Version
68
69
  version: '0'
69
70
  - !ruby/object:Gem::Dependency
70
71
  name: rake
71
72
  requirement: !ruby/object:Gem::Requirement
72
73
  requirements:
73
- - - '>='
74
+ - - ">="
74
75
  - !ruby/object:Gem::Version
75
76
  version: '0'
76
77
  type: :development
77
78
  prerelease: false
78
79
  version_requirements: !ruby/object:Gem::Requirement
79
80
  requirements:
80
- - - '>='
81
+ - - ">="
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  description:
@@ -87,8 +88,8 @@ executables: []
87
88
  extensions: []
88
89
  extra_rdoc_files: []
89
90
  files:
90
- - .gitignore
91
- - .rspec
91
+ - ".gitignore"
92
+ - ".rspec"
92
93
  - Gemfile
93
94
  - LICENSE.txt
94
95
  - README.md
@@ -102,7 +103,7 @@ files:
102
103
  - spec/bunny_exchanges/configuration/test_rabbitmq_configuration.yml
103
104
  - spec/bunny_exchanges/configuration_spec.rb
104
105
  - spec/bunny_exchanges/manager_spec.rb
105
- - spec/integration/configure_and_use.rb
106
+ - spec/integration/configure_and_use_spec.rb
106
107
  - spec/integration/test_exchanges_config.yml
107
108
  - spec/integration/test_rabbitmq_config.yml
108
109
  - spec/spec_helper.rb
@@ -116,17 +117,17 @@ require_paths:
116
117
  - lib
117
118
  required_ruby_version: !ruby/object:Gem::Requirement
118
119
  requirements:
119
- - - '>='
120
+ - - ">="
120
121
  - !ruby/object:Gem::Version
121
122
  version: '0'
122
123
  required_rubygems_version: !ruby/object:Gem::Requirement
123
124
  requirements:
124
- - - '>='
125
+ - - ">="
125
126
  - !ruby/object:Gem::Version
126
127
  version: '0'
127
128
  requirements: []
128
129
  rubyforge_project:
129
- rubygems_version: 2.2.2
130
+ rubygems_version: 2.4.5.1
130
131
  signing_key:
131
132
  specification_version: 4
132
133
  summary: A gem to initialize RabbitMQ exchanges using Bunny on ruby applications
@@ -135,7 +136,7 @@ test_files:
135
136
  - spec/bunny_exchanges/configuration/test_rabbitmq_configuration.yml
136
137
  - spec/bunny_exchanges/configuration_spec.rb
137
138
  - spec/bunny_exchanges/manager_spec.rb
138
- - spec/integration/configure_and_use.rb
139
+ - spec/integration/configure_and_use_spec.rb
139
140
  - spec/integration/test_exchanges_config.yml
140
141
  - spec/integration/test_rabbitmq_config.yml
141
142
  - spec/spec_helper.rb