ragnar 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ragnar/connector.rb +7 -6
- data/lib/ragnar/exchange.rb +22 -4
- data/lib/ragnar/version.rb +1 -1
- data/spec/lib/ragnar/connector_spec.rb +11 -13
- data/spec/lib/ragnar/exchange_spec.rb +1 -0
- data/spec/spec_helper.rb +6 -0
- metadata +2 -2
data/lib/ragnar/connector.rb
CHANGED
@@ -8,14 +8,15 @@ module Ragnar
|
|
8
8
|
|
9
9
|
# Store the connection for later retrieval
|
10
10
|
attr_accessor :connection
|
11
|
+
attr_accessor :host
|
12
|
+
attr_accessor :port
|
13
|
+
|
14
|
+
@host = 'localhost'
|
15
|
+
@port = '5762'
|
11
16
|
|
12
17
|
# Pass connection options through to AMQP
|
13
|
-
def connect
|
14
|
-
|
15
|
-
Thread.new { EventMachine.run }
|
16
|
-
sleep 0.5
|
17
|
-
end
|
18
|
-
@connection = AMQP.connect(opts)
|
18
|
+
def connect
|
19
|
+
@connection = AMQP.connect({host: @host, port: @port})
|
19
20
|
end
|
20
21
|
|
21
22
|
def connected?
|
data/lib/ragnar/exchange.rb
CHANGED
@@ -6,12 +6,19 @@ module Ragnar
|
|
6
6
|
|
7
7
|
def initialize type, name, opts={}
|
8
8
|
@type, @name, @options = type, name, opts
|
9
|
-
@channel = AMQP::Channel.new(Ragnar::Connector.connection)
|
10
|
-
@exchange = @channel.__send__(@type, @name, @options)
|
11
9
|
end
|
12
10
|
|
13
11
|
def publish routing_key, message, opts={}
|
14
|
-
|
12
|
+
EM.schedule do
|
13
|
+
Ragnar::Connector.connect unless Ragnar::Connector.connected?
|
14
|
+
|
15
|
+
connection = Ragnar::Connector.connection
|
16
|
+
channel = AMQP::Channel.new(connection)
|
17
|
+
exchange = channel.__send__(@type, @name, @options)
|
18
|
+
|
19
|
+
channel.queue(@name).bind(exchange, opts.merge(routing_key: routing_key))
|
20
|
+
exchange.publish(message, opts.merge(:routing_key => routing_key))
|
21
|
+
end
|
15
22
|
end
|
16
23
|
|
17
24
|
# Takes a subscription key or queue/routing options
|
@@ -28,8 +35,19 @@ module Ragnar
|
|
28
35
|
queue_name = queue_prefix.nil? ? name : '%s.%s' % [queue_prefix, name]
|
29
36
|
routing_key = name
|
30
37
|
end
|
31
|
-
|
38
|
+
|
39
|
+
EM.schedule do
|
40
|
+
Ragnar::Connector.connect unless Ragnar::Connector.connected?
|
41
|
+
|
42
|
+
connection = Ragnar::Connector.connection
|
43
|
+
channel = AMQP::Channel.new(connection)
|
44
|
+
exchange = channel.__send__(@type, @name, @options)
|
45
|
+
|
46
|
+
channel.queue(queue_name).bind(exchange, :routing_key => routing_key).subscribe(subscribe_opts, &block)
|
47
|
+
end
|
32
48
|
end
|
33
49
|
|
50
|
+
|
51
|
+
|
34
52
|
end
|
35
53
|
end
|
data/lib/ragnar/version.rb
CHANGED
@@ -7,23 +7,21 @@ describe Ragnar::Connector do
|
|
7
7
|
|
8
8
|
before(:each) do
|
9
9
|
Ragnar::Connector.connection = nil
|
10
|
+
Ragnar::Connector.host = 'localhost'
|
11
|
+
Ragnar::Connector.port = '5762'
|
10
12
|
end
|
11
13
|
|
12
14
|
describe '.connect' do
|
13
15
|
|
14
|
-
it '
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
it 'accepts an options hash and passes it through' do
|
24
|
-
opts = {:host => 'localhost', :port => '5762'}
|
25
|
-
AMQP.should_receive(:connect).with(opts)
|
26
|
-
Ragnar::Connector.connect(opts)
|
16
|
+
it 'can set host and port and have a connection' do
|
17
|
+
host = 'test.md.com'
|
18
|
+
port = '5763'
|
19
|
+
|
20
|
+
Ragnar::Connector.host = host
|
21
|
+
Ragnar::Connector.port = port
|
22
|
+
|
23
|
+
AMQP.should_receive(:connect).with({host: host, port: port})
|
24
|
+
Ragnar::Connector.connect
|
27
25
|
done
|
28
26
|
end
|
29
27
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: ragnar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- BJ Neislen
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-08-15 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: amqp
|