ragnar 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,30 +1,30 @@
1
1
  module Ragnar
2
2
  class Exchange
3
-
3
+
4
4
  attr_reader :exchange, :channel, :type, :name, :options
5
5
  attr_accessor :queue_prefix
6
-
6
+
7
7
  def initialize type, name, opts={}
8
8
  @type, @name, @options = type, name, opts
9
9
  end
10
-
10
+
11
11
  def publish routing_key, message, opts={}
12
12
  EM.schedule do
13
13
  Ragnar::Connector.connect unless Ragnar::Connector.connected?
14
-
14
+
15
15
  connection = Ragnar::Connector.connection
16
16
  channel = AMQP::Channel.new(connection)
17
17
  exchange = channel.__send__(@type, @name, @options)
18
-
18
+
19
19
  channel.queue(@name).bind(exchange, opts.merge(:routing_key => routing_key))
20
20
  exchange.publish(message, opts.merge(:routing_key => routing_key))
21
21
  end
22
22
  end
23
-
23
+
24
24
  # Takes a subscription key or queue/routing options
25
25
  #
26
26
  # exchange.subscribe('the.key') # => queue name and routing key are 'the.key'
27
- # exchange.subscribe(:queue => 'my.queue', :routing_key => 'message.*.pattern')
27
+ # exchange.subscribe(:queue => 'my.queue', :routing_key => 'message.*.pattern')
28
28
  #
29
29
  def subscribe name, subscribe_opts={}, &block
30
30
  if name.is_a?(Hash)
@@ -35,19 +35,16 @@ module Ragnar
35
35
  queue_name = queue_prefix.nil? ? name : '%s.%s' % [queue_prefix, name]
36
36
  routing_key = name
37
37
  end
38
-
38
+
39
39
  EM.schedule do
40
40
  Ragnar::Connector.connect unless Ragnar::Connector.connected?
41
-
41
+
42
42
  connection = Ragnar::Connector.connection
43
43
  channel = AMQP::Channel.new(connection)
44
44
  exchange = channel.__send__(@type, @name, @options)
45
-
45
+
46
46
  channel.queue(queue_name).bind(exchange, :routing_key => routing_key).subscribe(subscribe_opts, &block)
47
47
  end
48
48
  end
49
-
50
-
51
-
52
49
  end
53
50
  end
@@ -13,14 +13,18 @@ module Ragnar
13
13
  }
14
14
  end
15
15
 
16
- # Publish to a topic exchange. Defaults to 'events'
17
- def self.publish(message, route, exchange, exchange_type='topic')
16
+ # Note this method does not bind a queue to an exchange, therefore it's
17
+ # required that the queue/exchange are already bound before calling this
18
+ # method
19
+ def self.publish(message, route, exchange, opts={})
20
+ # delete the exchange type from the options or set it to topic
21
+ exchange_type = opts.delete(:exchange_type) { 'topic' }
18
22
  @publish_mutex ||= ::Mutex.new
19
23
 
20
24
  @publish_mutex.synchronize do
21
25
  ::Bunny.run(options) do |bunny|
22
26
  exchange = bunny.exchange(exchange, :type => exchange_type)
23
- exchange.publish(message, :key => route)
27
+ exchange.publish(message, opts.merge(:key => route))
24
28
  end
25
29
  end
26
30
  end
@@ -1,3 +1,3 @@
1
1
  module Ragnar
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/ragnar.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency 'amqp', '~>0.7.1'
22
+ s.add_dependency 'amqp'
23
23
  s.add_dependency 'bunny'
24
24
  s.add_development_dependency 'rspec'
25
25
  s.add_development_dependency 'evented-spec', '~>0.4.1'
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'ragnar/connector'
2
+ require 'ragnar.rb'
3
3
 
4
4
  describe Ragnar::Connector do
5
5
  include EventedSpec::SpecHelper
@@ -10,6 +10,8 @@ describe Ragnar::Connector do
10
10
 
11
11
  before(:each) do
12
12
  Ragnar::Connector.connection = nil
13
+ Ragnar::Connector.host = nil
14
+ Ragnar::Connector.port = nil
13
15
  end
14
16
 
15
17
  describe '.connect' do
@@ -24,10 +26,6 @@ describe Ragnar::Connector do
24
26
  end
25
27
 
26
28
  context 'deprecated configuration methods' do
27
- before(:each) do
28
- Ragnar::Connector.host = 'localhost'
29
- Ragnar::Connector.port = '5762'
30
- end
31
29
 
32
30
  it 'can set host and port and have a connection' do
33
31
  AMQP.stub(:connect)
@@ -5,13 +5,13 @@ require 'ragnar/exchange'
5
5
  describe Ragnar::Exchange do
6
6
  include EventedSpec::SpecHelper
7
7
  include EventedSpec::AMQPSpec
8
-
9
-
8
+
9
+
10
10
  describe '.new' do
11
11
  it 'creates a channel and exchange' do
12
12
  opts = {:durable => true}
13
13
  name = 'exch_name'
14
-
14
+
15
15
  Ragnar::Connector.should_receive(:connection).and_return(connection = mock('connection'))
16
16
  AMQP::Channel.should_receive(:new).with(connection).and_return(channel = mock('channel'))
17
17
  channel.should_receive(:topic).with(name, opts)
@@ -19,7 +19,7 @@ describe Ragnar::Exchange do
19
19
  done
20
20
  end
21
21
  end
22
-
22
+
23
23
  describe '#queue_prefix' do
24
24
  it 'sets a temporary queue prefix' do
25
25
  exch = Ragnar::Exchange.new(:topic, 'name')
@@ -28,11 +28,11 @@ describe Ragnar::Exchange do
28
28
  done
29
29
  end
30
30
  end
31
-
31
+
32
32
  describe '#subscribe' do
33
33
  let(:exch) { Ragnar::Exchange.new(:topic, 'name') }
34
34
  let(:subscription_block) { Proc.new {|m| true } }
35
-
35
+
36
36
  it 'binds a queue to the channel and assigns the subscription to the queue' do
37
37
  exch.channel.should_receive(:queue).with('the.event.name').and_return(queue = mock('queue'))
38
38
  queue.should_receive(:bind).with(exch.exchange, :routing_key => 'the.event.name').and_return(binding = mock('binding'))
@@ -40,7 +40,7 @@ describe Ragnar::Exchange do
40
40
  exch.subscribe('the.event.name', subscription_block)
41
41
  done
42
42
  end
43
-
43
+
44
44
  it 'uses the queue_prefix if present when setting the queue name' do
45
45
  exch.queue_prefix = :my_service
46
46
  exch.channel.should_receive(:queue).with('my_service.the.event.name').and_return(queue = mock('queue'))
@@ -49,7 +49,7 @@ describe Ragnar::Exchange do
49
49
  exch.subscribe('the.event.name')
50
50
  done
51
51
  end
52
-
52
+
53
53
  it 'accepts a hash for queue name and routing key' do
54
54
  exch.queue_prefix = :my_service
55
55
  exch.channel.should_receive(:queue).with('the.queue.name').and_return(queue = mock('queue'))
@@ -58,7 +58,7 @@ describe Ragnar::Exchange do
58
58
  exch.subscribe(:queue => 'the.queue.name', :routing_key => 'the.event.#')
59
59
  done
60
60
  end
61
-
61
+
62
62
  it 'cannot subscribe if a name is not given' do
63
63
  exch.queue_prefix = :my_service
64
64
  exch.channel.should_not_receive(:queue)
@@ -67,7 +67,7 @@ describe Ragnar::Exchange do
67
67
  }.should raise_error(/Invalid queue name/)
68
68
  done
69
69
  end
70
-
70
+
71
71
  it 'uses' do
72
72
  exch.queue_prefix = :my_service
73
73
  exch.channel.should_not_receive(:queue)
@@ -77,7 +77,7 @@ describe Ragnar::Exchange do
77
77
  done
78
78
  end
79
79
  end
80
-
80
+
81
81
  describe '#publish' do
82
82
  it 'publishes messages to the exchange' do
83
83
  exch = Ragnar::Exchange.new(:topic, 'name')
@@ -87,5 +87,5 @@ describe Ragnar::Exchange do
87
87
  done
88
88
  end
89
89
  end
90
-
91
- end
90
+
91
+ end
@@ -22,12 +22,19 @@ describe Ragnar::SimpleQueue do
22
22
 
23
23
  before(:each) { ::Bunny.stub(:run).and_yield(bunny_mock) }
24
24
 
25
- it %{publishes a message} do
25
+ it 'publishes a message' do
26
26
  message = 'publish me'
27
27
  route = 'to.the.batcave'
28
28
  bunny_exchange.should_receive(:publish).with(message, {:key => route})
29
29
  described_class.publish(message, route, 'event_exchange')
30
30
  end
31
31
 
32
+ it 'sends all options on to bunny' do
33
+ message = 'publish me'
34
+ route = 'to.the.batcave'
35
+ options = {:priority => 10, :bad => 'option'}
36
+ bunny_exchange.should_receive(:publish).with(message, options.merge(:key => route))
37
+ described_class.publish(message, route, 'event_exchange', options)
38
+ end
32
39
  end
33
40
  end
@@ -4,7 +4,7 @@ require 'ragnar'
4
4
  describe Ragnar do
5
5
  include EventedSpec::SpecHelper
6
6
  include EventedSpec::AMQPSpec
7
-
7
+
8
8
  describe '.exchange' do
9
9
  context 'when the exchange does not exist' do
10
10
  it 'creates a new exchange' do
@@ -14,7 +14,7 @@ describe Ragnar do
14
14
  done
15
15
  end
16
16
  end
17
-
17
+
18
18
  context 'when the exchange already has been created locally' do
19
19
  it 'creates a new exchange' do
20
20
  exch = Ragnar.exchange(:topic, 'exch_name')
@@ -22,19 +22,19 @@ describe Ragnar do
22
22
  done
23
23
  end
24
24
  end
25
-
25
+
26
26
  context 'when passed a block with embedded subscriptions' do
27
27
  it 'registers the subscriptions with the exchange' do
28
28
  subscriber = Proc.new{|message| true }
29
29
  exchange = Ragnar::Exchange.new(:topic, 'events')
30
30
  Ragnar.should_receive(:exchange).and_yield(exchange)
31
-
31
+
32
32
  exchange.should_receive(:queue_prefix=).with(:my_service)
33
33
  exchange.should_receive(:subscribe).with('the.message.route.1', &subscriber)
34
34
  exchange.should_receive(:subscribe).with('the.message.route.2', &subscriber)
35
35
  exchange.should_receive(:subscribe).with('the.message.route.3', &subscriber)
36
36
  exchange.should_receive(:subscribe).with('the.message.route.4', &subscriber)
37
-
37
+
38
38
  Ragnar.exchange(:topic, 'events') do |x|
39
39
  x.queue_prefix = :my_service
40
40
  x.subscribe('the.message.route.1', &subscriber)
@@ -42,12 +42,12 @@ describe Ragnar do
42
42
  x.subscribe('the.message.route.3', &subscriber)
43
43
  x.subscribe('the.message.route.4', &subscriber)
44
44
  end
45
-
45
+
46
46
  done
47
47
  end
48
48
  end
49
49
  end
50
-
50
+
51
51
  end
52
52
 
53
53
  describe Ragnar::Config do
@@ -120,4 +120,4 @@ describe Ragnar::Config do
120
120
  end
121
121
  end
122
122
  end
123
-
123
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ragnar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-30 00:00:00.000000000Z
12
+ date: 2012-06-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
16
- requirement: &2157572600 !ruby/object:Gem::Requirement
16
+ requirement: &2153569340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.7.1
21
+ version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2157572600
24
+ version_requirements: *2153569340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bunny
27
- requirement: &2157572160 !ruby/object:Gem::Requirement
27
+ requirement: &2153568860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2157572160
35
+ version_requirements: *2153568860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2157571700 !ruby/object:Gem::Requirement
38
+ requirement: &2153566140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2157571700
46
+ version_requirements: *2153566140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: evented-spec
49
- requirement: &2157571200 !ruby/object:Gem::Requirement
49
+ requirement: &2153564580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.4.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2157571200
57
+ version_requirements: *2153564580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: pry
60
- requirement: &2157570780 !ruby/object:Gem::Requirement
60
+ requirement: &2153563000 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2157570780
68
+ version_requirements: *2153563000
69
69
  description: Provide top-level pub/sub methods with RabbitMQ (AMQP) for interacting
70
70
  with a larger service ecosystem
71
71
  email: