amqp-boilerplate 0.0.2 → 0.0.3

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.
data/CHANGELOG ADDED
@@ -0,0 +1,14 @@
1
+ = version 0.0.3
2
+
3
+ * [FEATURE] Added amqp_exchange macro to Consumer for selecting the exchange
4
+ to bind to.
5
+
6
+ = version 0.0.2
7
+
8
+ * [FEATURE] AMQP::boilerplate.boot for booting up within an EventLoop (i.e.
9
+ when using Passenger)
10
+
11
+ = version 0.0.1
12
+
13
+ * [FEATURE] Added class for setting up an AMQP Consumer
14
+ * [FEATURE] Added module for creating an AMQP Producer
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ludo van den Boom, http://cubicphuse.nl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = amqp-boilerplate
2
+
3
+ amqp-boilerplate is a set of helper classes and modules to be used with the
4
+ Ruby amqp gem.
5
+
6
+ == Install
7
+
8
+ gem install amqp-boilerplate
9
+
10
+ == Configuration
11
+
12
+ See {AMQP::Boilerplate.configure} for configuration options.
13
+
14
+ == Usage
15
+
16
+ amqp-boilerplate provides the AMQP::Boilerplate::Producer module for creating
17
+ message producers, and the AMQP::Boilerplate::Consumer class for setting up a
18
+ message consumer.
19
+
20
+ === Producers
21
+
22
+ The following sample code shows a basic producer.
23
+
24
+ class MyProducer
25
+ extend AMQP::Boilerplate::Producer
26
+
27
+ amqp :routing_key => "hello.world"
28
+ amqp_message :message
29
+
30
+ def message
31
+ "Look! I am a string that will be posted to the exchange."
32
+ end
33
+ end
34
+
35
+ For more information please refer to {AMQP::Boilerplate::Producer}
36
+
37
+ === Consumers
38
+
39
+ The following sample code shows a basic consumer.
40
+
41
+ class MyConsumer < AMQP::Boilerplate::Consumer
42
+ amqp_queue "hello.world"
43
+
44
+ def handle_message(payload, metadata)
45
+ puts "Received message: #{payload}"
46
+ end
47
+ end
48
+
49
+ For more information please refer to {AMQP::Boilerplate::Consumer}
50
+
51
+ == License
52
+
53
+ amqp-boilerplate is released under the MIT license.
@@ -1,7 +1,23 @@
1
1
  module AMQP
2
2
  module Boilerplate
3
+ # @example Basic consumer
4
+ # class MyConsumer < AMQP::Boilerplate::Consumer
5
+ # amqp_queue "hello.world"
6
+ #
7
+ # def handle_message(payload, metadata)
8
+ # puts "Received message: #{payload}"
9
+ # end
10
+ # end
3
11
  class Consumer
4
12
  class << self
13
+ # Macro for selecting exchange to bind to
14
+ #
15
+ # @param [String] name Exchange name
16
+ # @return [void]
17
+ def amqp_exchange(name)
18
+ @exchange_name = name
19
+ end
20
+
5
21
  def amqp_queue(name=AMQ::Protocol::EMPTY_STRING, options={})
6
22
  @queue_name = name
7
23
  @queue_options = options
@@ -19,6 +35,7 @@ module AMQP
19
35
  channel.on_error(&consumer.method(:handle_channel_error))
20
36
 
21
37
  queue = channel.queue(@queue_name, @queue_options)
38
+ queue.bind(@exchange_name) if @exchange_name
22
39
  queue.subscribe(@subscription_options, &consumer.method(:handle_message))
23
40
 
24
41
  AMQP::Boilerplate.logger.info("[#{self.name}.start] Started consumer '#{self.name}'")
@@ -1,5 +1,5 @@
1
1
  module AMQP
2
2
  module Boilerplate
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -90,5 +90,32 @@ describe AMQP::Boilerplate::Consumer do
90
90
  @queue.should_receive(:subscribe).with(:ack => true)
91
91
  BarConsumer.start
92
92
  end
93
+
94
+ describe "when an exchange name has been provided" do
95
+ before(:each) do
96
+ @exchange_name = "amq.fanout"
97
+ BarConsumer.amqp_exchange(@exchange_name)
98
+ end
99
+
100
+ after(:each) do
101
+ BarConsumer.amqp_exchange(nil)
102
+ end
103
+
104
+ it "should bind to the exchange" do
105
+ @queue.should_receive(:bind).with(@exchange_name)
106
+ BarConsumer.start
107
+ end
108
+ end
109
+
110
+ describe "when no exchange name provided" do
111
+ before(:each) do
112
+ BarConsumer.amqp_exchange(nil)
113
+ end
114
+
115
+ it "should not explicitly bind to an exchange" do
116
+ @queue.should_not_receive(:bind)
117
+ BarConsumer.start
118
+ end
119
+ end
93
120
  end
94
121
  end
@@ -6,9 +6,15 @@ describe AMQP::Boilerplate do
6
6
  AMQP::Boilerplate.stub(:logger).and_return(mock.as_null_object)
7
7
 
8
8
  EventMachine.stub(:next_tick).and_yield
9
+ AMQP::Utilities::EventLoopHelper.stub(:run).and_yield
10
+
11
+ AMQP.stub(:connection).and_return(mock(AMQP::Session))
12
+ AMQP::Channel.stub(:new).and_return(mock(AMQP::Channel))
9
13
 
10
- AMQP::Boilerplate.stub(:start_consumers)
11
14
  AMQP::Boilerplate.stub(:load_consumers)
15
+ AMQP::Boilerplate.stub(:start_consumers)
16
+ AMQP::Boilerplate.stub(:start)
17
+ AMQP::Boilerplate.stub(:sleep)
12
18
  end
13
19
 
14
20
  it "should load all consumers" do
@@ -26,6 +32,18 @@ describe AMQP::Boilerplate do
26
32
  AMQP::Boilerplate.boot
27
33
  end
28
34
 
35
+ it "should sleep for a while" do
36
+ AMQP::Boilerplate.should_receive(:sleep).with(0.25)
37
+ AMQP::Boilerplate.boot
38
+ end
39
+
40
+ describe "wen not using passenger" do
41
+ it "should use built-in EventLoopHelper" do
42
+ AMQP::Utilities::EventLoopHelper.should_receive(:run)
43
+ AMQP::Boilerplate.boot
44
+ end
45
+ end
46
+
29
47
  describe "when using passenger" do
30
48
  before(:each) do
31
49
  PhusionPassenger = Class.new
@@ -82,6 +100,10 @@ describe AMQP::Boilerplate do
82
100
  end
83
101
 
84
102
  describe ".start" do
103
+ before(:each) do
104
+ AMQP.stub(:start)
105
+ end
106
+
85
107
  it "should start AMQP" do
86
108
  AMQP.should_receive(:start)
87
109
  AMQP::Boilerplate.start
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amqp-boilerplate
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Patrick Baselier
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-09-13 00:00:00 +02:00
19
+ date: 2011-09-15 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -92,8 +92,10 @@ extra_rdoc_files: []
92
92
  files:
93
93
  - .gitignore
94
94
  - .rspec
95
+ - CHANGELOG
95
96
  - Gemfile
96
- - README
97
+ - LICENSE
98
+ - README.rdoc
97
99
  - Rakefile
98
100
  - amqp-boilerplate.gemspec
99
101
  - lib/amqp-boilerplate.rb
data/README DELETED
@@ -1,18 +0,0 @@
1
- = AMQP Boilterplate
2
-
3
- == Getting started
4
-
5
- === Install RabbitMQ
6
-
7
- === Install the gem
8
-
9
- ==== Rails 3.x
10
- gem "amqp-boilerplate", "~> 0.1.0"
11
-
12
- === Create a producer
13
-
14
- === Create a consumer
15
-
16
- == License
17
-
18
-