celldee-bunny 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/examples/simple.rb CHANGED
@@ -1,3 +1,11 @@
1
+ # simple.rb
2
+
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
1
9
  $:.unshift File.dirname(__FILE__) + '/../lib'
2
10
 
3
11
  require 'bunny'
@@ -15,16 +23,13 @@ end
15
23
  # declare a queue
16
24
  q = b.queue('test1')
17
25
 
18
- # create a direct exchange
19
- exch = b.exchange(:direct, 'test_ex')
20
-
21
- # bind the queue to the exchange
22
- q.bind(exch)
23
-
24
- # publish a message to the exchange
25
- exch.publish('Hello everybody!')
26
+ # publish a message to the queue
27
+ q.publish('Hello everybody!')
26
28
 
27
29
  # get message from the queue
28
30
  msg = q.pop
29
31
 
30
- puts 'This is the message: ' + msg + "\n\n"
32
+ puts 'This is the message: ' + msg + "\n\n"
33
+
34
+ # close the client connection
35
+ b.stop
data/lib/amqp/client.rb CHANGED
@@ -59,13 +59,13 @@ module AMQP
59
59
  send_frame(
60
60
  Protocol::Channel::Close.new(:reply_code => 200, :reply_text => 'bye', :method_id => 0, :class_id => 0)
61
61
  )
62
- puts "Error closing channel #{channel}" unless next_method.is_a?(Protocol::Channel::CloseOk)
62
+ raise ProtocolError, "Error closing channel #{channel}" unless next_method.is_a?(Protocol::Channel::CloseOk)
63
63
 
64
64
  self.channel = 0
65
65
  send_frame(
66
66
  Protocol::Connection::Close.new(:reply_code => 200, :reply_text => 'Goodbye', :class_id => 0, :method_id => 0)
67
67
  )
68
- puts "Error closing connection" unless next_method.is_a?(Protocol::Connection::CloseOk)
68
+ raise ProtocolError, "Error closing connection" unless next_method.is_a?(Protocol::Connection::CloseOk)
69
69
 
70
70
  close_socket
71
71
  end
@@ -82,7 +82,7 @@ module AMQP
82
82
  @channel = 0
83
83
  write(HEADER)
84
84
  write([1, 1, VERSION_MAJOR, VERSION_MINOR].pack('C4'))
85
- raise ProtocolError, 'bad start connection' unless next_method.is_a?(Protocol::Connection::Start)
85
+ raise ProtocolError, 'Connection initiation failed' unless next_method.is_a?(Protocol::Connection::Start)
86
86
 
87
87
  send_frame(
88
88
  Protocol::Connection::StartOk.new(
@@ -92,8 +92,11 @@ module AMQP
92
92
  'en_US'
93
93
  )
94
94
  )
95
+
96
+ method = next_method
97
+ raise ProtocolError, "Connection failed - user: #{@user}, pass: #{@pass}" if method.nil?
95
98
 
96
- if next_method.is_a?(Protocol::Connection::Tune)
99
+ if method.is_a?(Protocol::Connection::Tune)
97
100
  send_frame(
98
101
  Protocol::Connection::TuneOk.new( :channel_max => 0, :frame_max => 131072, :heartbeat => 0)
99
102
  )
@@ -114,6 +117,9 @@ module AMQP
114
117
  method = next_method
115
118
  raise ProtocolError, 'Access denied' unless method.is_a?(Protocol::Access::RequestOk)
116
119
  self.ticket = method.ticket
120
+
121
+ # return status
122
+ status
117
123
  end
118
124
 
119
125
  private
data/lib/bunny/queue.rb CHANGED
@@ -35,11 +35,7 @@
35
35
  msg = client.next_payload
36
36
  raise 'unexpected length' if msg.length < header.size
37
37
 
38
- if hdr
39
- {:header => header, :payload => msg}
40
- else
41
- msg
42
- end
38
+ hdr ? {:header => header, :payload => msg} : msg
43
39
 
44
40
  end
45
41
 
data/spec/bunny_spec.rb CHANGED
@@ -1,5 +1,11 @@
1
1
  # bunny_spec.rb
2
2
 
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. @b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
3
9
  require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib bunny]))
4
10
 
5
11
  describe Bunny do
@@ -1,5 +1,11 @@
1
1
  # exchange_spec.rb
2
2
 
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. @b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
3
9
  require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib bunny]))
4
10
 
5
11
  describe Exchange do
@@ -13,7 +19,7 @@ describe Exchange do
13
19
  @b.stop
14
20
  end
15
21
 
16
- it "should be able to be created as a direct exchange" do
22
+ it "should be able to be instantiated as a direct exchange" do
17
23
  exch = @b.exchange(:direct, 'direct_exchange')
18
24
  exch.should be_an_instance_of Exchange
19
25
  exch.name.should == 'direct_exchange'
@@ -21,7 +27,7 @@ describe Exchange do
21
27
  @b.exchanges.has_key?('direct_exchange').should be true
22
28
  end
23
29
 
24
- it "should be able to be created as a topic exchange" do
30
+ it "should be able to be instantiated as a topic exchange" do
25
31
  exch = @b.exchange(:topic, 'topic_exchange')
26
32
  exch.should be_an_instance_of Exchange
27
33
  exch.name.should == 'topic_exchange'
@@ -29,7 +35,7 @@ describe Exchange do
29
35
  @b.exchanges.has_key?('topic_exchange').should be true
30
36
  end
31
37
 
32
- it "should be able to be created as a fanout exchange" do
38
+ it "should be able to be instantiated as a fanout exchange" do
33
39
  exch = @b.exchange(:fanout, 'fanout_exchange')
34
40
  exch.should be_an_instance_of Exchange
35
41
  exch.name.should == 'fanout_exchange'
data/spec/queue_spec.rb CHANGED
@@ -1,5 +1,11 @@
1
1
  # queue_spec.rb
2
2
 
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. @b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
3
9
  require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib bunny]))
4
10
 
5
11
  describe Bunny do
@@ -18,11 +24,21 @@ describe Bunny do
18
24
  q.publish('This is a test message')
19
25
  q.message_count.should == 1
20
26
  end
27
+
28
+ it "should be able to pop a message complete with header" do
29
+ q = @b.queue('test1')
30
+ msg = q.pop(:header => true)
31
+ msg.should be_an_instance_of Hash
32
+ msg[:header].should be_an_instance_of AMQP::Protocol::Header
33
+ msg[:payload].should == 'This is a test message'
34
+ q.message_count.should == 0
35
+ end
21
36
 
22
- it "should be able to pop a message" do
37
+ it "should be able to pop a message and just get the payload" do
23
38
  q = @b.queue('test1')
39
+ q.publish('This is another test message')
24
40
  msg = q.pop
25
- msg.should == 'This is a test message'
41
+ msg.should == 'This is another test message'
26
42
  q.message_count.should == 0
27
43
  end
28
44
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: celldee-bunny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Duncan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-21 00:00:00 -07:00
12
+ date: 2009-04-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -30,7 +30,6 @@ files:
30
30
  - lib/bunny/exchange.rb
31
31
  - lib/amqp/frame.rb
32
32
  - lib/bunny/header.rb
33
- - lib/bunny/logger.rb
34
33
  - lib/amqp/protocol.rb
35
34
  - lib/bunny/queue.rb
36
35
  - lib/amqp/client.rb
data/lib/bunny/logger.rb DELETED
@@ -1,89 +0,0 @@
1
- class MQ
2
- class Logger
3
- def initialize *args, &block
4
- opts = args.pop if args.last.is_a? Hash
5
- opts ||= {}
6
-
7
- printer(block) if block
8
-
9
- @prop = opts
10
- @tags = ([:timestamp] + args).uniq
11
- end
12
-
13
- attr_reader :prop
14
- alias :base :prop
15
-
16
- def log severity, *args
17
- opts = args.pop if args.last.is_a? Hash and args.size != 1
18
- opts ||= {}
19
- opts = @prop.clone.update(opts)
20
-
21
- data = args.shift
22
-
23
- data = {:type => :exception,
24
- :name => data.class.to_s.intern,
25
- :backtrace => data.backtrace,
26
- :message => data.message} if data.is_a? Exception
27
-
28
- (@tags + args).each do |tag|
29
- tag = tag.to_sym
30
- case tag
31
- when :timestamp
32
- opts.update :timestamp => Time.now
33
- when :hostname
34
- @hostname ||= { :hostname => `hostname`.strip }
35
- opts.update @hostname
36
- when :process
37
- @process_id ||= { :process_id => Process.pid,
38
- :process_name => $0,
39
- :process_parent_id => Process.ppid,
40
- :thread_id => Thread.current.object_id }
41
- opts.update :process => @process_id
42
- else
43
- (opts[:tags] ||= []) << tag
44
- end
45
- end
46
-
47
- opts.update(:severity => severity,
48
- :msg => data)
49
-
50
- print(opts)
51
- unless Logger.disabled?
52
- MQ.fanout('logging', :durable => true).publish Marshal.dump(opts)
53
- end
54
-
55
- opts
56
- end
57
- alias :method_missing :log
58
-
59
- def print data = nil, &block
60
- if block
61
- @printer = block
62
- elsif data.is_a? Proc
63
- @printer = data
64
- elsif data
65
- (pr = @printer || self.class.printer) and pr.call(data)
66
- else
67
- @printer
68
- end
69
- end
70
- alias :printer :print
71
-
72
- def self.printer &block
73
- @printer = block if block
74
- @printer
75
- end
76
-
77
- def self.disabled?
78
- !!@disabled
79
- end
80
-
81
- def self.enable
82
- @disabled = false
83
- end
84
-
85
- def self.disable
86
- @disabled = true
87
- end
88
- end
89
- end