brightbox-warren 0.8 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/Manifest +0 -1
- data/lib/warren/adapters/bunny_adapter.rb +1 -9
- data/lib/warren/connection.rb +98 -17
- data/readme.rdoc +9 -1
- data/spec/spec_helper.rb +11 -0
- data/spec/warren/connection_spec.rb +96 -63
- data/warren.gemspec +2 -2
- metadata +2 -2
data/CHANGELOG
CHANGED
data/Manifest
CHANGED
@@ -76,7 +76,7 @@ module Warren
|
|
76
76
|
#
|
77
77
|
def self.do_connect queue_name, callback = nil, &block
|
78
78
|
# Open a connection
|
79
|
-
b = Bunny.new(
|
79
|
+
b = Bunny.new(self.connection.options)
|
80
80
|
b.start
|
81
81
|
# Create the queue
|
82
82
|
q = b.queue(queue_name)
|
@@ -88,14 +88,6 @@ module Warren
|
|
88
88
|
callback.nil? ? true : callback.call
|
89
89
|
end
|
90
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
91
|
end
|
100
92
|
end
|
101
93
|
end
|
data/lib/warren/connection.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "yaml"
|
2
2
|
module Warren
|
3
3
|
class Connection
|
4
|
-
|
4
|
+
|
5
5
|
attr_reader :options
|
6
6
|
|
7
7
|
#
|
@@ -9,23 +9,22 @@ module Warren
|
|
9
9
|
# WARREN_ROOT/config/warren.yml or specify the file to read as an
|
10
10
|
# argument or by passing a hash of connection details in.
|
11
11
|
#
|
12
|
+
# Warren::Connection.new # reads WARREN_ROOT/config/warren.yml
|
13
|
+
# Warren::Connection.new("file.yml") # reads file.yml
|
14
|
+
# Warren::Connection.new({"foo" => "bar"}) # uses the hash
|
15
|
+
#
|
12
16
|
# Reads WARREN_ENV out of the yaml'd hash (just like ActiveRecord)
|
13
17
|
# "development" by default (and RAILS_ENV if running under rails)
|
14
18
|
#
|
15
19
|
# Raises InvalidConnectionDetails if no params are found for the current
|
16
20
|
# environment.
|
17
21
|
#
|
22
|
+
# todo: fail nicely if the env isn't defined
|
23
|
+
#
|
18
24
|
def initialize params = nil
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
opts = YAML.load_file(file)
|
23
|
-
end
|
24
|
-
opts ||= params
|
25
|
-
|
26
|
-
opts = symbolize_keys(opts[WARREN_ENV])
|
27
|
-
check_connection_details(opts)
|
28
|
-
@options = opts
|
25
|
+
get_connection_details(params)
|
26
|
+
# Make the details publically accessible
|
27
|
+
@options = @opts
|
29
28
|
end
|
30
29
|
|
31
30
|
#
|
@@ -36,23 +35,105 @@ module Warren
|
|
36
35
|
|
37
36
|
private
|
38
37
|
|
38
|
+
#
|
39
|
+
# Short version: This is the brain of this class and sets everything up.
|
40
|
+
#
|
41
|
+
# Long version: Works out where the connection details need to come from
|
42
|
+
# (params or yml) and parses them from there. Then figures out if the details
|
43
|
+
# are there for the current env and raises a nice error if there aren't any
|
44
|
+
# detail for the current env. Then we symblize all the keys and get the adapter
|
45
|
+
# to check its happy with the connection details we have.
|
46
|
+
#
|
47
|
+
def get_connection_details params
|
48
|
+
case params
|
49
|
+
when NilClass
|
50
|
+
# Read from config/warren.yml
|
51
|
+
read_config_file
|
52
|
+
|
53
|
+
when String
|
54
|
+
# See if it exists as a file
|
55
|
+
parse_config_file(params)
|
56
|
+
|
57
|
+
when Hash
|
58
|
+
# Use it as-is
|
59
|
+
@opts = params
|
60
|
+
|
61
|
+
else
|
62
|
+
# See if it responds to :read
|
63
|
+
if respond_to?(:read)
|
64
|
+
# Parse it as yaml
|
65
|
+
parse_config(params)
|
66
|
+
else
|
67
|
+
# Have no idea what to do with it
|
68
|
+
raise InvalidConnectionDetails, "Don't know what to do with the params passed. Please pass a hash, filename or nothing."
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Make sure the hash keys are symbols
|
73
|
+
@opts = symbolize_keys(@opts)
|
74
|
+
# Parse out the current environment
|
75
|
+
parse_environment
|
76
|
+
# Call the adapter to figure out if the details are alright
|
77
|
+
check_connection_details
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
# Reads in either config/warren.yml or the passed argument as a YAML file
|
82
|
+
#
|
83
|
+
def read_config_file filename=nil
|
84
|
+
filename ||= "#{WARREN_ROOT}/config/warren.yml"
|
85
|
+
parse_config_file(filename)
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
# Parses the config file into a hash from yaml.
|
90
|
+
#
|
91
|
+
def parse_config_file filename
|
92
|
+
if File.exists?(filename)
|
93
|
+
@opts = YAML.load_file(filename)
|
94
|
+
else
|
95
|
+
raise InvalidConnectionDetails, "File not found: '#{filename}'"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Parses the object using YAML#load
|
101
|
+
#
|
102
|
+
def parse_config obj
|
103
|
+
@opts = YAML.load(obj)
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
# Modifies the hash into just the details for the
|
108
|
+
# current environment, or raises InvalidConnectionDetails
|
109
|
+
#
|
110
|
+
def parse_environment
|
111
|
+
# keys are symbolified already
|
112
|
+
unless @opts.has_key?(WARREN_ENV.to_sym)
|
113
|
+
raise InvalidConnectionDetails, "No details for current environment '#{WARREN_ENV}'"
|
114
|
+
end
|
115
|
+
@opts = @opts[WARREN_ENV.to_sym]
|
116
|
+
end
|
117
|
+
|
39
118
|
#
|
40
119
|
# Changes all keys into symbols
|
41
120
|
#
|
42
121
|
def symbolize_keys(hash)
|
43
122
|
hash.each do |key, value|
|
44
123
|
hash.delete(key)
|
45
|
-
|
124
|
+
# Make it recursive
|
125
|
+
hash[key.to_sym] = (value.is_a?(Hash) ? symbolize_keys(value) : value)
|
46
126
|
end
|
127
|
+
hash
|
47
128
|
end
|
48
|
-
|
49
|
-
#
|
129
|
+
|
130
|
+
#
|
50
131
|
# Calls the adapter to check the connection details
|
51
132
|
# Returns true or raises InvalidConnectionDetails
|
52
|
-
#
|
53
|
-
def check_connection_details
|
133
|
+
#
|
134
|
+
def check_connection_details
|
54
135
|
return true unless Warren::Queue.adapter.respond_to?(:check_connection_details)
|
55
|
-
Warren::Queue.adapter.send(:check_connection_details,
|
136
|
+
Warren::Queue.adapter.send(:check_connection_details, @opts)
|
56
137
|
end
|
57
138
|
|
58
139
|
end
|
data/readme.rdoc
CHANGED
@@ -27,10 +27,18 @@ Add this to your environment.rb
|
|
27
27
|
|
28
28
|
config.gem "brightbox-warren", :lib => "warren", :version => ">= 0.8"
|
29
29
|
|
30
|
+
Add the config into config/warren.yml with the details for each environment. Works just the same as database.yml:
|
31
|
+
|
32
|
+
development:
|
33
|
+
user: rabbit
|
34
|
+
pass: carrots53
|
35
|
+
host: rabbit.warren
|
36
|
+
logging: false
|
37
|
+
|
30
38
|
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
39
|
|
32
40
|
require "warren/adapters/bunny_adapter"
|
33
41
|
|
34
42
|
== License
|
35
43
|
|
36
|
-
Licensed under the MIT license. See LICENSE for more details.
|
44
|
+
Licensed under the MIT license. See LICENSE for more details.
|
data/spec/spec_helper.rb
CHANGED
@@ -1 +1,12 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__) + "/../lib/warren")
|
2
|
+
|
3
|
+
# Hacky and I hate it, but needed to
|
4
|
+
# change constants in tests
|
5
|
+
# from http://is.gd/12JVp
|
6
|
+
def silently(&block)
|
7
|
+
warn_level = $VERBOSE
|
8
|
+
$VERBOSE = nil
|
9
|
+
result = block.call
|
10
|
+
$VERBOSE = warn_level
|
11
|
+
result
|
12
|
+
end
|
@@ -1,67 +1,100 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../spec_helper'
|
2
2
|
|
3
3
|
describe Warren::Connection do
|
4
|
-
|
4
|
+
|
5
5
|
before(:each) do
|
6
|
-
@file = stub 'io', :read => yaml_data
|
7
6
|
setup_adapter
|
8
7
|
end
|
9
|
-
|
10
|
-
it "should read from
|
11
|
-
|
8
|
+
|
9
|
+
it "should read from warren.yml" do
|
10
|
+
setup_config_file
|
12
11
|
|
13
12
|
Warren::Connection.new
|
14
13
|
end
|
15
14
|
|
16
|
-
it "should
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
:pass => "password",
|
22
|
-
:logging => false
|
23
|
-
}
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should symbolize keys in a hash" do
|
27
|
-
conn = Warren::Connection.new(@file)
|
28
|
-
hash = {"one" => "two", "three" => "four", :five => "six"}
|
29
|
-
conn.send(:symbolize_keys, hash).should == {
|
30
|
-
:one => "two",
|
31
|
-
:three => "four",
|
32
|
-
:five => "six"
|
33
|
-
}
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should raise if no adapter set to check against" do
|
37
|
-
Warren::Queue.adapter = nil
|
38
|
-
lambda {
|
39
|
-
Warren::Connection.new(@file)
|
40
|
-
}.should raise_error(Warren::Queue::NoAdapterSet)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should successfully check against adapter" do
|
44
|
-
_adapter = stub 'queue', :check_connection_details => true
|
45
|
-
|
46
|
-
Warren::Connection.new(@file)
|
15
|
+
it "should read from an arbitrary file" do
|
16
|
+
File.should_receive(:exists?).with("my_awesome_config.yml").and_return(true)
|
17
|
+
YAML.should_receive(:load_file).with("my_awesome_config.yml").and_return({"development" => {}})
|
18
|
+
|
19
|
+
Warren::Connection.new("my_awesome_config.yml")
|
47
20
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
21
|
+
|
22
|
+
describe "parsing config with file" do
|
23
|
+
|
24
|
+
before do
|
25
|
+
setup_config_file
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should parse the right config out" do
|
29
|
+
conn = Warren::Connection.new
|
30
|
+
WARREN_ENV.should == "development"
|
31
|
+
conn.instance_variable_get("@options").should == {
|
32
|
+
:user => "rspec",
|
33
|
+
:pass => "password",
|
34
|
+
:host => "localhost",
|
35
|
+
:logging => false,
|
36
|
+
}
|
37
|
+
end
|
51
38
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
39
|
+
it "should raise if no details for current environment" do
|
40
|
+
silently { WARREN_ENV = "rspec" }
|
41
|
+
|
42
|
+
lambda { Warren::Connection.new }.
|
43
|
+
should raise_error(
|
44
|
+
Warren::Connection::InvalidConnectionDetails,
|
45
|
+
"No details for current environment 'rspec'"
|
46
|
+
)
|
47
|
+
|
48
|
+
silently { WARREN_ENV = "development" }
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should symbolize keys in a hash" do
|
52
|
+
conn = Warren::Connection.new
|
53
|
+
hash = {"one" => "two", "three" => {"four" => 7}, :five => "six"}
|
54
|
+
conn.send(:symbolize_keys, hash).should == {
|
55
|
+
:one => "two",
|
56
|
+
:three => {:four => 7},
|
57
|
+
:five => "six"
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should raise if no adapter set to check against" do
|
62
|
+
Warren::Queue.adapter = nil
|
63
|
+
lambda { Warren::Connection.new }.
|
64
|
+
should raise_error(Warren::Queue::NoAdapterSet)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should successfully check against adapter" do
|
68
|
+
_adapter = mock 'queue', :check_connection_details => true
|
69
|
+
_adapter.should_receive(:check_connection_details)
|
70
|
+
Warren::Queue.adapter = _adapter
|
71
|
+
|
72
|
+
Warren::Connection.new
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should raise errors for missing connection details" do
|
76
|
+
_adapter = stub 'queue', :check_connection_details => ["one", "two"]
|
77
|
+
|
78
|
+
Warren::Connection.new
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should raise errors for other prerequisits in the adapter" do
|
82
|
+
Adapter = Class.new(Warren::Queue) do
|
83
|
+
def self.check_connection_details params
|
84
|
+
raise Warren::Connection::InvalidConnectionDetails, "Missing prerequisites"
|
85
|
+
end
|
59
86
|
end
|
87
|
+
|
88
|
+
lambda { Warren::Connection.new }.
|
89
|
+
should raise_error(Warren::Connection::InvalidConnectionDetails, "Missing prerequisites")
|
60
90
|
end
|
61
91
|
|
62
|
-
|
63
|
-
|
64
|
-
|
92
|
+
end
|
93
|
+
|
94
|
+
def setup_config_file
|
95
|
+
config_file = "#{File.dirname($0)}/config/warren.yml"
|
96
|
+
File.should_receive(:exists?).with(config_file).and_return(true)
|
97
|
+
YAML.should_receive(:load_file).with(config_file).and_return(yaml_data)
|
65
98
|
end
|
66
99
|
|
67
100
|
def setup_adapter
|
@@ -70,20 +103,20 @@ describe Warren::Connection do
|
|
70
103
|
end
|
71
104
|
|
72
105
|
def yaml_data
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
test
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
106
|
+
{
|
107
|
+
"development" => {
|
108
|
+
"host" => "localhost",
|
109
|
+
"user" => "rspec",
|
110
|
+
"pass" => "password",
|
111
|
+
"logging" => false
|
112
|
+
},
|
113
|
+
"test" => {
|
114
|
+
"host" => "localhost-test",
|
115
|
+
"user" => "rspec-test",
|
116
|
+
"pass" => "password-test",
|
117
|
+
"logging" => true
|
118
|
+
}
|
119
|
+
}
|
87
120
|
end
|
88
121
|
|
89
122
|
end
|
data/warren.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{warren}
|
5
|
-
s.version = "0.8"
|
5
|
+
s.version = "0.8.2"
|
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"]
|
9
|
-
s.date = %q{2009-06-
|
9
|
+
s.date = %q{2009-06-15}
|
10
10
|
s.description = %q{Library for pushing messages onto and off RabbitMQ queues}
|
11
11
|
s.email = %q{support@brightbox.co.uk}
|
12
12
|
s.extra_rdoc_files = ["CHANGELOG", "lib/warren/adapters/amqp_adapter.rb", "lib/warren/adapters/bunny_adapter.rb", "lib/warren/connection.rb", "lib/warren/filters/shared_secret.rb", "lib/warren/filters/yaml.rb", "lib/warren/message_filter.rb", "lib/warren/queue.rb", "lib/warren.rb", "LICENSE", "tasks/rdoc.rake", "tasks/rspec.rake"]
|
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:
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Caius Durling, David Smalley
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-15 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|