forked from famoseagle/carrot
/
carrot.rb
95 lines (77 loc) · 1.9 KB
/
carrot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Carrot
module AMQP
HEADER = "AMQP".freeze
VERSION_MAJOR = 8
VERSION_MINOR = 0
PORT = 5672
end
end
$:.unshift File.expand_path(File.dirname(File.expand_path(__FILE__)))
require 'amqp/spec'
require 'amqp/buffer'
require 'amqp/exchange'
require 'amqp/frame'
require 'amqp/header'
require 'amqp/queue'
require 'amqp/server'
require 'amqp/protocol'
class Carrot
@logging = false
class << self
attr_accessor :logging
end
def self.logging?
@logging
end
class Error < StandardError; end
def initialize(opts = {})
@opts = opts
end
def queue(name, opts = {})
queues[name] ||= AMQP::Queue.new(self, name, opts)
end
def server
@server ||= AMQP::Server.new(@opts)
end
def stop
server.close
@server = nil
end
alias :reset :stop
def queues
@queues ||= {}
end
def direct(name = 'amq.direct', opts = {})
exchanges[name] ||= AMQP::Exchange.new(self, :direct, name, opts)
end
def topic(name = 'amq.topic', opts = {})
exchanges[name] ||= AMQP::Exchange.new(self, :topic, name, opts)
end
def headers(name = 'amq.match', opts = {})
exchanges[name] ||= AMQP::Exchange.new(self, :headers, name, opts)
end
def fanout(name = 'amq.fanout', opts = {})
exchanges[name] ||= AMQP::Exchange.new(self, :fanout, name, opts)
end
def exchanges
@exchanges ||= {}
end
private
def log(*args)
return unless Carrot.logging?
pp args
puts
end
end
#-- convenience wrapper (read: HACK) for thread-local Carrot object
class Carrot
def Carrot.default
#-- XXX clear this when connection is closed
Thread.current[:carrot] ||= Carrot.new
end
# Allows for calls to all Carrot instance methods. This implicitly calls
# Carrot.new so that a new channel is allocated for subsequent operations.
def Carrot.method_missing(meth, *args, &blk)
Carrot.default.__send__(meth, *args, &blk)
end
end