cottontail 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Rudolf Schmidt
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.
@@ -0,0 +1,57 @@
1
+ # Cottontail
2
+
3
+ This library is a wrapper around the popular AMQP bunny gem. It is inspired by Sinatra to easily consume messages on different routing keys.
4
+
5
+ ## Installation
6
+
7
+ System wide:
8
+
9
+ ```console
10
+ gem install cottontail
11
+ ```
12
+
13
+ Or in your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'cottontail'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ When using this gem, you should already be familiar with RabbitMQ and, idealy, Bunny as it's a wrapper around it. Given we place our code into `worker.rb`, we can define a simple class like so:.
22
+
23
+ ```ruby
24
+ require 'cottontail'
25
+ require 'bunny'
26
+
27
+ class Worker < Cottontail::Base
28
+ # configure the Bunny client with the default connection (host: "localhost", port: 5672)
29
+ # and with logging.
30
+ set :client, :logging => true
31
+
32
+ # Declare default direct exchange which is bound to all queues of the type `topic`
33
+ set :exchange, "", :type => :topic
34
+
35
+ # Declare the `test` queue
36
+ set :queue, "test"
37
+
38
+
39
+ # Consume messages on the routing key: `message.received`.Within the provided block
40
+ # you have access to seleral methods. See Cottontail::Helpers for more details.
41
+ route "message.received" do
42
+ puts "This is the payload #{payload.inspect}"
43
+ end
44
+ end
45
+
46
+ # The following will start Cottontail right away. You need to be aware that it
47
+ # will enter the Bunny subscribe loop, so it will block the process.
48
+ Worker.run
49
+ ```
50
+
51
+ To run it, you may start it like the following code example. However, you should use a more sophisticated solution for daemonizing your processes in a production environment. See http://www.ruby-toolbox.com/categories/daemonizing for futher inspiration.
52
+
53
+ ```console
54
+ ruby worker.rb &
55
+ ```
56
+
57
+ Copyright &copy; 2012 Rudolf Schmidt, released under the MIT license
@@ -20,5 +20,5 @@ Gem::Specification.new do |s|
20
20
 
21
21
  # specify any dependencies here; for example:
22
22
  # s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
23
+ s.add_runtime_dependency "bunny"
24
24
  end
@@ -4,11 +4,30 @@ module Cottontail
4
4
  class RouteNotFound < StandardError; end
5
5
 
6
6
  module Helpers
7
- attr_reader :message, :last_error
8
-
7
+ # The last_error is accessable within an error block.
8
+ #
9
+ # last_error will be set once an exception was raise
10
+ attr_reader :last_error
11
+
12
+ # A RabbitMQ message retrieved by Bunny usually contains the following information:
13
+ # header [Qrack::Protocol::Header] The header of the message including size, properties, etc.
14
+ # payload [String] The message sent through RabbitMQ
15
+ # delivery_details [Hash] Includes the exchange, routing_key, etc
16
+ #
17
+ # This is the original message from the queue (not be confused with the payload sent via RabitMQ).
18
+ attr_reader :message
19
+
20
+ # Accessor to the message's payload
9
21
  def payload; message[:payload]; end
22
+
23
+ # Accessor to the message's header
24
+ def header; message[:header]; end
25
+
26
+ # Accessor to the message's delivery details
10
27
  def delivery_details; message[:delivery_details]; end
11
- def routing_key; message[:routing_key]; end
28
+
29
+ # Accessor to the delivery detail's routing key
30
+ def routing_key; delivery_details[:routing_key]; end
12
31
  end
13
32
 
14
33
  class Base
@@ -70,7 +89,7 @@ module Cottontail
70
89
  codes.each { |c| (@errors[c] ||= []) << compile!("error_#{c}", &block) }
71
90
  end
72
91
 
73
- # Route on class level
92
+ # Route on class level (handy for testing)
74
93
  #
75
94
  # @example
76
95
  # route! :payload => "some message", :routing_key => "v2.message.sent"
@@ -1,3 +1,3 @@
1
1
  module Cottontail
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cottontail
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rudolf Schmidt
@@ -11,8 +11,18 @@ bindir: bin
11
11
  cert_chain: []
12
12
 
13
13
  date: 2012-02-02 00:00:00 Z
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bunny
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
16
26
  description: Convenience wrapper around the AMQP Bunny gem to better handle routing_key specific messages
17
27
  email:
18
28
  executables: []
@@ -24,6 +34,8 @@ extra_rdoc_files: []
24
34
  files:
25
35
  - .gitignore
26
36
  - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
27
39
  - Rakefile
28
40
  - cottontail.gemspec
29
41
  - lib/cottontail.rb