brightbox-warren 0.7 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,4 @@
1
+ v0.8. Gem works with rails
1
2
  v0.7. Added adapters
2
3
  v0.6. Added config file
3
4
  v0.5. Added filters
@@ -1,97 +1,101 @@
1
1
  require "rubygems"
2
2
  require "bunny"
3
3
 
4
- class Warren::Queue::BunnyAdapter < Warren::Queue
5
-
6
- #
7
- # Checks the connection details are correct for this adapter
8
- #
9
- def self.check_connection_details opts
10
- # Check they've passed in the stuff without a default on it
11
- unless opts.has_key?(:user) && opts.has_key?(:pass) && opts.has_key?(:vhost)
12
- raise Warren::Connection::InvalidConnectionDetails, "Missing a username, password or vhost."
13
- end
14
- true
15
- end
16
-
17
- #
18
- # Returns the default queue name or returns InvalidConnectionDetails
19
- # if no default queue is defined
20
- #
21
- def self.queue_name
22
- unless self.connection.options.has_key?(:default_queue)
23
- raise Warren::Connection::InvalidConnectionDetails, "Missing a default queue name."
24
- end
25
- self.connection.options[:default_queue]
26
- end
4
+ module Warren
5
+ class Queue
6
+ class BunnyAdapter < Queue
27
7
 
28
- #
29
- # Sends a message to a queue. If successfully sent it returns
30
- # true, unless callback block is passed (see below)
31
- #
32
- # Warren::Queue.publish(:queue_name, {:foo => "name"})
33
- #
34
- # Can also pass a block which is fired after the message
35
- # is sent. If a block is passed, then the return value of the block
36
- # is returned from this method.
37
- #
38
- # Warren::Queue.publish(:queue_name, {:foo => "name"}) { puts "foo" }
39
- #
40
- def self.publish queue_name, payload, &blk
41
- queue_name = self.queue_name if queue_name == :default
42
- # Create a message object if it isn't one already
43
- msg = Warren::MessageFilter.pack(payload)
8
+ #
9
+ # Checks the connection details are correct for this adapter
10
+ #
11
+ def self.check_connection_details opts
12
+ # Check they've passed in the stuff without a default on it
13
+ unless opts.has_key?(:user) && opts.has_key?(:pass) && opts.has_key?(:vhost)
14
+ raise Warren::Connection::InvalidConnectionDetails, "Missing a username, password or vhost."
15
+ end
16
+ true
17
+ end
44
18
 
45
- do_connect(queue_name, blk) do |queue|
46
- queue.publish msg.to_s
47
- end
19
+ #
20
+ # Returns the default queue name or returns InvalidConnectionDetails
21
+ # if no default queue is defined
22
+ #
23
+ def self.queue_name
24
+ unless self.connection.options.has_key?(:default_queue)
25
+ raise Warren::Connection::InvalidConnectionDetails, "Missing a default queue name."
26
+ end
27
+ self.connection.options[:default_queue]
28
+ end
48
29
 
49
- end
50
-
51
- #
52
- # Subscribes to a queue and runs the block
53
- # for each message received
54
- #
55
- # Warren::Queue.subscribe("example") {|msg| puts msg }
56
- #
57
- # Expects a block and raises NoBlockGiven if no block is given.
58
- #
59
- def self.subscribe queue_name, &block
60
- raise NoBlockGiven unless block_given?
61
- queue_name = self.queue_name if queue_name == :default
62
- # todo: check if its a valid queue?
63
- do_connect(queue_name) do |queue|
64
- msg = queue.pop
65
- return if msg == :queue_empty
66
- block.call(Warren::MessageFilter.unpack(msg))
67
- end
68
- end
30
+ #
31
+ # Sends a message to a queue. If successfully sent it returns
32
+ # true, unless callback block is passed (see below)
33
+ #
34
+ # Warren::Queue.publish(:queue_name, {:foo => "name"})
35
+ #
36
+ # Can also pass a block which is fired after the message
37
+ # is sent. If a block is passed, then the return value of the block
38
+ # is returned from this method.
39
+ #
40
+ # Warren::Queue.publish(:queue_name, {:foo => "name"}) { puts "foo" }
41
+ #
42
+ def self.publish queue_name, payload, &blk
43
+ queue_name = self.queue_name if queue_name == :default
44
+ # Create a message object if it isn't one already
45
+ msg = Warren::MessageFilter.pack(payload)
69
46
 
70
- private
47
+ do_connect(queue_name, blk) do |queue|
48
+ queue.publish msg.to_s
49
+ end
71
50
 
72
- #
73
- # Connects and does the stuff its told to!
74
- #
75
- def self.do_connect queue_name, callback = nil, &block
76
- # Open a connection
77
- b = Bunny.new(connection_details)
78
- b.start
79
- # Create the queue
80
- q = b.queue(queue_name)
81
- # Run the code on the queue
82
- block.call(q)
83
- # And stop
84
- b.stop
85
- # Returns the block return value or true
86
- callback.nil? ? true : callback.call
87
- end
88
-
89
- def self.connection_details
90
- {
91
- :user => self.connection.options[:user],
92
- :pass => self.connection.options[:pass],
93
- :vhost => self.connection.options[:vhost]
94
- }
95
- end
51
+ end
52
+
53
+ #
54
+ # Subscribes to a queue and runs the block
55
+ # for each message received
56
+ #
57
+ # Warren::Queue.subscribe("example") {|msg| puts msg }
58
+ #
59
+ # Expects a block and raises NoBlockGiven if no block is given.
60
+ #
61
+ def self.subscribe queue_name, &block
62
+ raise NoBlockGiven unless block_given?
63
+ queue_name = self.queue_name if queue_name == :default
64
+ # todo: check if its a valid queue?
65
+ do_connect(queue_name) do |queue|
66
+ msg = queue.pop
67
+ return if msg == :queue_empty
68
+ block.call(Warren::MessageFilter.unpack(msg))
69
+ end
70
+ end
71
+
72
+ private
96
73
 
97
- end
74
+ #
75
+ # Connects and does the stuff its told to!
76
+ #
77
+ def self.do_connect queue_name, callback = nil, &block
78
+ # Open a connection
79
+ b = Bunny.new(connection_details)
80
+ b.start
81
+ # Create the queue
82
+ q = b.queue(queue_name)
83
+ # Run the code on the queue
84
+ block.call(q)
85
+ # And stop
86
+ b.stop
87
+ # Returns the block return value or true
88
+ callback.nil? ? true : callback.call
89
+ end
90
+
91
+ def self.connection_details
92
+ {
93
+ :user => self.connection.options[:user],
94
+ :pass => self.connection.options[:pass],
95
+ :vhost => self.connection.options[:vhost]
96
+ }
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -17,9 +17,9 @@ module Warren
17
17
  #
18
18
  def initialize params = nil
19
19
  if params.nil? || !params.is_a?(Hash)
20
- file ||= WARREN_ROOT << "/config" << "/warren.yml"
20
+ file ||= "#{WARREN_ROOT}/config/warren.yml"
21
21
  raise InvalidConnectionDetails, "Config file not found: #{file}" unless File.exists?(file)
22
- opts = YAML.load(file)
22
+ opts = YAML.load_file(file)
23
23
  end
24
24
  opts ||= params
25
25
 
data/lib/warren/queue.rb CHANGED
@@ -1,69 +1,71 @@
1
- class Warren::Queue
2
- @@connection = nil
3
- @@adapter = nil
1
+ module Warren
2
+ class Queue
3
+ @@connection = nil
4
+ @@adapter = nil
4
5
 
5
- #
6
- # Raised if no connection has been defined yet.
7
- #
8
- NoConnectionDetails = Class.new(Exception)
6
+ #
7
+ # Raised if no connection has been defined yet.
8
+ #
9
+ NoConnectionDetails = Class.new(Exception)
9
10
 
10
- #
11
- # Raised if a block is expected by the method but none is given.
12
- #
13
- NoBlockGiven = Class.new(Exception)
14
-
15
- #
16
- # Raised if an adapter isn't set
17
- #
18
- NoAdapterSet = Class.new(Exception)
11
+ #
12
+ # Raised if a block is expected by the method but none is given.
13
+ #
14
+ NoBlockGiven = Class.new(Exception)
19
15
 
20
- #
21
- # Sets the current connection
22
- #
23
- def self.connection= conn
24
- @@connection = (conn.is_a?(Warren::Connection) ? conn : Warren::Connection.new(conn) )
25
- end
26
-
27
- #
28
- # Returns the current connection details
29
- #
30
- def self.connection
31
- @@connection ||= Warren::Connection.new
32
- end
16
+ #
17
+ # Raised if an adapter isn't set
18
+ #
19
+ NoAdapterSet = Class.new(Exception)
33
20
 
34
- #
35
- # Sets the adapter when this class is subclassed.
36
- #
37
- def self.inherited klass
38
- @@adapter = klass
39
- end
40
-
41
- #
42
- # Sets the adapter manually
43
- #
44
- def self.adapter= klass
45
- @@adapter = klass
46
- end
21
+ #
22
+ # Sets the current connection
23
+ #
24
+ def self.connection= conn
25
+ @@connection = (conn.is_a?(Warren::Connection) ? conn : Warren::Connection.new(conn) )
26
+ end
47
27
 
48
- #
49
- # Returns the current adapter or raises NoAdapterSet exception
50
- #
51
- def self.adapter
52
- @@adapter || raise(NoAdapterSet)
53
- end
28
+ #
29
+ # Returns the current connection details
30
+ #
31
+ def self.connection
32
+ @@connection ||= Warren::Connection.new
33
+ end
54
34
 
55
- #
56
- # Publishes the message to the queue
57
- #
58
- def self.publish *args, &blk
59
- self.adapter.publish(*args, &blk)
60
- end
61
-
62
- #
63
- # Sends the subscribe message to the adapter class
64
- #
65
- def self.subscribe *args, &blk
66
- self.adapter.subscribe(*args, &blk)
67
- end
35
+ #
36
+ # Sets the adapter when this class is subclassed.
37
+ #
38
+ def self.inherited klass
39
+ @@adapter = klass
40
+ end
41
+
42
+ #
43
+ # Sets the adapter manually
44
+ #
45
+ def self.adapter= klass
46
+ @@adapter = klass
47
+ end
48
+
49
+ #
50
+ # Returns the current adapter or raises NoAdapterSet exception
51
+ #
52
+ def self.adapter
53
+ @@adapter || raise(NoAdapterSet)
54
+ end
68
55
 
69
- end
56
+ #
57
+ # Publishes the message to the queue
58
+ #
59
+ def self.publish *args, &blk
60
+ self.adapter.publish(*args, &blk)
61
+ end
62
+
63
+ #
64
+ # Sends the subscribe message to the adapter class
65
+ #
66
+ def self.subscribe *args, &blk
67
+ self.adapter.subscribe(*args, &blk)
68
+ end
69
+
70
+ end
71
+ end
data/lib/warren.rb CHANGED
@@ -14,10 +14,10 @@ module Warren
14
14
  end
15
15
 
16
16
  WARREN_ENV = (defined?(RAILS_ENV) ? RAILS_ENV : "development") unless defined?(WARREN_ENV)
17
- WARREN_ROOT = File.dirname($0)
18
- WARREN_LIB_ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
17
+ WARREN_ROOT = (defined?(RAILS_ROOT) ? RAILS_ROOT : File.dirname($0)) unless defined?(WARREN_ROOT)
18
+ WARREN_LIB_ROOT = File.expand_path(File.dirname(__FILE__))
19
19
 
20
20
  # Require everything in the lib folder
21
- Dir["#{WARREN_LIB_ROOT}/lib/warren/*.rb"].each do |file|
21
+ Dir["#{WARREN_LIB_ROOT}/warren/*.rb"].each do |file|
22
22
  require file
23
23
  end
data/readme.rdoc CHANGED
@@ -21,6 +21,16 @@ Start by looking at examples/ to see how to use it, and then lib/warren/adapters
21
21
 
22
22
  # See examples/ for more
23
23
 
24
+ == Rails
25
+
26
+ Add this to your environment.rb
27
+
28
+ config.gem "brightbox-warren", :lib => "warren", :version => ">= 0.8"
29
+
30
+ And then in an initializer file (or bottom of environment.rb) require the adapter you want to use (for rabbitmq I suggest bunny - amqp uses eventmachine and was giving me issues under passenger.) And then any filters you want to use.
31
+
32
+ require "warren/adapters/bunny_adapter"
33
+
24
34
  == License
25
35
 
26
36
  Licensed under the MIT license. See LICENSE for more details.
data/warren.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{warren}
5
- s.version = "0.7"
5
+ s.version = "0.8"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Caius Durling, David Smalley"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brightbox-warren
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.7"
4
+ version: "0.8"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caius Durling, David Smalley