ragnar 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,14 +14,21 @@ module Ragnar
14
14
  @exchange.publish(message, opts.merge(:routing_key => routing_key))
15
15
  end
16
16
 
17
- def subscribe name, opts={}, &block
18
- @channel.queue(queue_name(name)).bind(@exchange, :routing_key => name).subscribe(opts, &block)
19
- end
20
-
21
- private
22
-
23
- def queue_name name
24
- queue_prefix.nil? ? name : '%s.%s' % [queue_prefix, name]
17
+ # Takes a subscription key or queue/routing options
18
+ #
19
+ # exchange.subscribe('the.key') # => queue name and routing key are 'the.key'
20
+ # exchange.subscribe(:queue => 'my.queue', :routing_key => 'message.*.pattern')
21
+ #
22
+ def subscribe name, subscribe_opts={}, &block
23
+ if name.is_a?(Hash)
24
+ queue_name = name[:queue] or raise 'Invalid queue name'
25
+ routing_key = name[:routing_key]
26
+ else
27
+ raise 'Invalid queue name' if name.nil? or name.strip.empty?
28
+ queue_name = queue_prefix.nil? ? name : '%s.%s' % [queue_prefix, name]
29
+ routing_key = name
30
+ end
31
+ @channel.queue(queue_name).bind(@exchange, :routing_key => routing_key).subscribe(subscribe_opts, &block)
25
32
  end
26
33
 
27
34
  end
@@ -1,3 +1,3 @@
1
1
  module Ragnar
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -29,18 +29,18 @@ describe Ragnar::Exchange do
29
29
  end
30
30
 
31
31
  describe '#subscribe' do
32
+ let(:exch) { Ragnar::Exchange.new(:topic, 'name') }
33
+ let(:subscription_block) { Proc.new {|m| true } }
34
+
32
35
  it 'binds a queue to the channel and assigns the subscription to the queue' do
33
- exch = Ragnar::Exchange.new(:topic, 'name')
34
- subscription = Proc.new {|message| true }
35
36
  exch.channel.should_receive(:queue).with('the.event.name').and_return(queue = mock('queue'))
36
37
  queue.should_receive(:bind).with(exch.exchange, :routing_key => 'the.event.name').and_return(binding = mock('binding'))
37
- binding.should_receive(:subscribe).with(subscription)
38
- exch.subscribe('the.event.name', subscription)
38
+ binding.should_receive(:subscribe).with(subscription_block)
39
+ exch.subscribe('the.event.name', subscription_block)
39
40
  done
40
41
  end
41
42
 
42
43
  it 'uses the queue_prefix if present when setting the queue name' do
43
- exch = Ragnar::Exchange.new(:topic, 'name')
44
44
  exch.queue_prefix = :my_service
45
45
  exch.channel.should_receive(:queue).with('my_service.the.event.name').and_return(queue = mock('queue'))
46
46
  queue.should_receive(:bind).with(exch.exchange, :routing_key => 'the.event.name').and_return(binding = mock('binding'))
@@ -48,6 +48,33 @@ describe Ragnar::Exchange do
48
48
  exch.subscribe('the.event.name')
49
49
  done
50
50
  end
51
+
52
+ it 'accepts a hash for queue name and routing key' do
53
+ exch.queue_prefix = :my_service
54
+ exch.channel.should_receive(:queue).with('the.queue.name').and_return(queue = mock('queue'))
55
+ queue.should_receive(:bind).with(exch.exchange, :routing_key => 'the.event.#').and_return(binding = mock('binding'))
56
+ binding.should_receive(:subscribe)
57
+ exch.subscribe(:queue => 'the.queue.name', :routing_key => 'the.event.#')
58
+ done
59
+ end
60
+
61
+ it 'cannot subscribe if a name is not given' do
62
+ exch.queue_prefix = :my_service
63
+ exch.channel.should_not_receive(:queue)
64
+ expect {
65
+ exch.subscribe(nil)
66
+ }.should raise_error(/Invalid queue name/)
67
+ done
68
+ end
69
+
70
+ it 'uses' do
71
+ exch.queue_prefix = :my_service
72
+ exch.channel.should_not_receive(:queue)
73
+ expect {
74
+ exch.subscribe(nil)
75
+ }.should raise_error(/Invalid queue name/)
76
+ done
77
+ end
51
78
  end
52
79
 
53
80
  describe '#publish' do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - BJ Neislen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-05-05 00:00:00 -06:00
17
+ date: 2011-05-06 00:00:00 -06:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency