evrone-common-amqp 0.1.1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 684e39904cedd622f5262a9694ca8e1d3276c7ca
4
- data.tar.gz: 2e1261f6115f45a611626d9b91574df740abdffb
3
+ metadata.gz: c24324f4e19411cb06a578695a44e8f495b1a9b8
4
+ data.tar.gz: e91da1a141c2eb6581e8a7f377186312a6b71488
5
5
  SHA512:
6
- metadata.gz: 94932a2a0f44f280fd53a134116cb56285616192e5663244b08de71322f4ccd427abba3a42a5b7638e9f32b58c3f4aa6a9e6f01257b639d36bd3cf8c123d5beb
7
- data.tar.gz: 73391330d411391d4ceb039617724e12d49cffd916990274ff885552fc82ddfbfffba55aaab7927f80b79ddc7d86f2ac03a7a0a7f24c93f0cab6460114c3f8e9
6
+ metadata.gz: deb6175551f9b6569783539f33ad9c4836bf70db5dded7a1d6d8c2ddf38ab709c78d68e7e1aeb11848d7eec135d378adf07d1b2df3bf877d858aa3f9586b06d2
7
+ data.tar.gz: 77e583d9cc31af0ada5f625a1e3c51eac418ee3f6569d0ee66f2bddd03a4971fe5b981c02f1f4bc633ea04a29684b7367a61cff01556e76b2b064336cb3f1977
@@ -8,9 +8,7 @@ module Evrone
8
8
 
9
9
  attr_accessor :url, :default_exchange_options, :default_queue_options,
10
10
  :default_publish_options, :default_exchange_type, :logger, :pool_timeout,
11
- :heartbeat, :spawn_attempts, :content_type
12
-
13
- attr_reader :publishing_builder, :recieving_builder, :subscribing_builder
11
+ :heartbeat, :spawn_attempts, :content_type, :callbacks
14
12
 
15
13
  def initialize
16
14
  reset!
@@ -20,16 +18,12 @@ module Evrone
20
18
  Common::AMQP::Formatter
21
19
  end
22
20
 
23
- def publishing(&block)
24
- @publishing_builder = Common::Rack::Builder.new(&block)
25
- end
26
-
27
- def recieving(&block)
28
- @recieving_builder = Common::Rack::Builder.new(&block)
29
- end
30
-
31
- def subscribing(&block)
32
- @subscribing_builder = Common::Rack::Builder.new(&block)
21
+ %w{ before after }.each do |p|
22
+ %w{ subscribe publish recieve }.each do |m|
23
+ define_method "#{p}_#{m}" do |&callback|
24
+ callbacks["#{p}_#{m}".to_sym] = callback
25
+ end
26
+ end
33
27
  end
34
28
 
35
29
  def default_exchange_name
@@ -51,6 +45,8 @@ module Evrone
51
45
 
52
46
  @content_type = 'application/json'
53
47
 
48
+ @callbacks = {}
49
+
54
50
  @default_exchange_options = {
55
51
  durable: true,
56
52
  auto_delete: false
@@ -13,8 +13,8 @@ module Evrone
13
13
 
14
14
  x = declare_exchange
15
15
 
16
- with_middleware :publishing, message: message, exchange: x do |opts|
17
- m = serialize_message opts[:message], options[:content_type]
16
+ run_callbacks(:publish, message: message, exchange: x) do
17
+ m = serialize_message message, options[:content_type]
18
18
  x.publish m, options
19
19
  end
20
20
 
@@ -10,7 +10,7 @@ module Evrone
10
10
  x = declare_exchange
11
11
  q = declare_queue
12
12
 
13
- with_middleware(:subscribing, exchange: x, queue: q) do |_|
13
+ run_callbacks(:subscribe, exchange: x, queue: q) do
14
14
  debug "subscribing to #{q.name}:#{x.name} using #{bind_options.inspect}"
15
15
  q.bind(x, bind_options)
16
16
  debug "successfuly subscribed to #{q.name}:#{x.name}"
@@ -47,11 +47,11 @@ module Evrone
47
47
  def run_instance(delivery_info, properties, payload)
48
48
  payload = deserialize_message properties, payload
49
49
 
50
- with_middleware(:recieving, payload: payload) do |opts|
50
+ run_callbacks :recieve, payload: payload do
51
51
  new.tap do |inst|
52
52
  inst.properties = properties
53
53
  inst.delivery_info = delivery_info
54
- end.perform opts[:payload]
54
+ end.perform payload
55
55
  end
56
56
  end
57
57
 
@@ -35,7 +35,7 @@ module Evrone
35
35
  include Common::AMQP::Consumer::Publish
36
36
  include Common::AMQP::Consumer::Subscribe
37
37
  include Common::AMQP::Logger
38
- include Common::AMQP::WithMiddleware
38
+ include Common::AMQP::Callbacks
39
39
 
40
40
  def shutdown?
41
41
  Common::AMQP.shutdown?
@@ -0,0 +1,25 @@
1
+ module Evrone
2
+ module Common
3
+ module AMQP
4
+ module Callbacks
5
+
6
+ def run_callbacks(name, *args)
7
+ before = "before_#{name}".to_sym
8
+ after = "after_#{name}".to_sym
9
+ if f = Common::AMQP.config.callbacks[before]
10
+ f.call(*args)
11
+ end
12
+
13
+ rs = yield if block_given?
14
+
15
+ if f = Common::AMQP.config.callbacks[after]
16
+ f.call(*args)
17
+ end
18
+
19
+ rs
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,7 +1,7 @@
1
1
  module Evrone
2
2
  module Common
3
3
  module AMQP
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
7
7
  end
@@ -15,7 +15,7 @@ module Evrone
15
15
  end
16
16
 
17
17
  autoload :Logger, File.expand_path("../amqp/mixins/logger", __FILE__)
18
- autoload :WithMiddleware, File.expand_path("../amqp/mixins/with_middleware", __FILE__)
18
+ autoload :Callbacks, File.expand_path("../amqp/mixins/callbacks", __FILE__)
19
19
 
20
20
  extend self
21
21
 
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Evrone::Common::AMQP::Config do
4
+ let(:config) { described_class.new }
5
+
6
+ context '(callbacks)' do
7
+ %w{ before after }.each do |p|
8
+ %w{ subscribe recieve publish }.each do |m|
9
+ name = "#{p}_#{m}"
10
+ it name do
11
+ config.public_send name do |value|
12
+ value
13
+ end
14
+
15
+ val = config.callbacks[name.to_sym].call("value")
16
+ expect(val).to eq 'value'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Evrone::Common::AMQP::Callbacks do
4
+ let(:output) { [] }
5
+ let(:object) { Object.new.extend described_class }
6
+
7
+ before do
8
+ Evrone::Common::AMQP.config.reset!
9
+ Evrone::Common::AMQP.configure do |c|
10
+ c.before_publish { |v| output << "before:#{v}" }
11
+ c.after_publish { |v| output << "after:#{v}" }
12
+ end
13
+ end
14
+
15
+ after { Evrone::Common::AMQP.config.reset! }
16
+
17
+ context 'run_callbacks' do
18
+ it "should be success" do
19
+ object.run_callbacks :publish, "call" do
20
+ output << "call"
21
+ end
22
+ expect(output).to eq %w{ before:call call after:call }
23
+ end
24
+ end
25
+ end
26
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evrone-common-amqp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Galinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-30 00:00:00.000000000 Z
11
+ date: 2013-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -120,17 +120,18 @@ files:
120
120
  - lib/evrone/common/amqp/consumer/publish.rb
121
121
  - lib/evrone/common/amqp/consumer/subscribe.rb
122
122
  - lib/evrone/common/amqp/formatter.rb
123
+ - lib/evrone/common/amqp/mixins/callbacks.rb
123
124
  - lib/evrone/common/amqp/mixins/logger.rb
124
- - lib/evrone/common/amqp/mixins/with_middleware.rb
125
125
  - lib/evrone/common/amqp/session.rb
126
126
  - lib/evrone/common/amqp/supervisor/threaded.rb
127
127
  - lib/evrone/common/amqp/testing.rb
128
128
  - lib/evrone/common/amqp/version.rb
129
129
  - spec/integration/multi_threaded_spec.rb
130
130
  - spec/integration/threaded_supervisor_spec.rb
131
+ - spec/lib/amqp/config_spec.rb
131
132
  - spec/lib/amqp/consumer_spec.rb
132
133
  - spec/lib/amqp/formatter_spec.rb
133
- - spec/lib/amqp/mixins/with_middleware_spec.rb
134
+ - spec/lib/amqp/mixins/callbacks_spec.rb
134
135
  - spec/lib/amqp/session_spec.rb
135
136
  - spec/lib/amqp/supervisor/threaded_spec.rb
136
137
  - spec/lib/amqp_spec.rb
@@ -164,9 +165,10 @@ summary: Common amqp code
164
165
  test_files:
165
166
  - spec/integration/multi_threaded_spec.rb
166
167
  - spec/integration/threaded_supervisor_spec.rb
168
+ - spec/lib/amqp/config_spec.rb
167
169
  - spec/lib/amqp/consumer_spec.rb
168
170
  - spec/lib/amqp/formatter_spec.rb
169
- - spec/lib/amqp/mixins/with_middleware_spec.rb
171
+ - spec/lib/amqp/mixins/callbacks_spec.rb
170
172
  - spec/lib/amqp/session_spec.rb
171
173
  - spec/lib/amqp/supervisor/threaded_spec.rb
172
174
  - spec/lib/amqp_spec.rb
@@ -1,16 +0,0 @@
1
- module Evrone
2
- module Common
3
- module AMQP
4
- module WithMiddleware
5
- def with_middleware(name, env, &block)
6
- builder = Common::AMQP.config.public_send("#{name}_builder")
7
- if builder
8
- builder.to_app(block).call env
9
- else
10
- yield env
11
- end
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,32 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Evrone::Common::AMQP::WithMiddleware do
4
- let(:object) { Object.new.extend described_class }
5
-
6
- Foo = Struct.new(:app, :id) do
7
- def call(env)
8
- env << "called"
9
- app.call env
10
- end
11
- end
12
-
13
- before { Evrone::Common::AMQP.config.reset! }
14
- after { Evrone::Common::AMQP.config.reset! }
15
-
16
- context 'with_middleware' do
17
- it "should be successfull" do
18
- Evrone::Common::AMQP.configure do |c|
19
- c.publishing do
20
- use Foo, :id
21
- end
22
- end
23
-
24
- collected = ""
25
- object.with_middleware(:publishing, "") do |env|
26
- collected << env
27
- end
28
- expect(collected).to eq 'called'
29
- end
30
- end
31
- end
32
-