ragnar 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 opts={}
14
- unless EM.reactor_running?
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?
@@ -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
- @exchange.publish(message, opts.merge(:routing_key => routing_key))
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
- @channel.queue(queue_name).bind(@exchange, :routing_key => routing_key).subscribe(subscribe_opts, &block)
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
@@ -1,3 +1,3 @@
1
1
  module Ragnar
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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 'runs eventmachine and creates an AMQP connection' do
15
- connection = Ragnar::Connector.connect
16
- delayed(0.3) {
17
- EM.reactor_running?.should be_true
18
- connection.should be_connected
19
- done
20
- }
21
- end
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
 
@@ -6,6 +6,7 @@ describe Ragnar::Exchange do
6
6
  include EventedSpec::SpecHelper
7
7
  include EventedSpec::AMQPSpec
8
8
 
9
+
9
10
  describe '.new' do
10
11
  it 'creates a channel and exchange' do
11
12
  opts = {:durable => true}
data/spec/spec_helper.rb CHANGED
@@ -2,5 +2,11 @@ require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'rspec'
4
4
  require 'evented-spec'
5
+ require 'eventmachine'
6
+
7
+ unless EM.reactor_running?
8
+ Thread.new { EventMachine.run }
9
+ sleep 0.5
10
+ end
5
11
 
6
12
  $: << File.expand_path('../lib', File.dirname(__FILE__))
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ragnar
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
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-07-20 00:00:00 Z
13
+ date: 2011-08-15 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: amqp