-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.rb
72 lines (60 loc) · 1.68 KB
/
server.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
module Humpty
class Server
class ConfigurationException < Error; end
include HTTParty
attr_reader :id
def initialize(id)
@id = id
self.class.base_uri "#{configuration["alice"]["base_url"]}:#{configuration["alice"]["port"]}"
end
def queues
requested_attributes = %w(name durable auto_delete messages_ready messages_unacknowledged messages_uncommitted messages acks_uncommitted consumers transactions memory)
get("/queues/root/#{requested_attributes.join('/')}")["queues"].sort {|a,b| a["name"] <=> b["name"]}
end
def bindings
get("/bindings")["bindings"]
end
def control
get("/control")
end
def queue(name)
bunny.queue(name)
end
def self.configurations
begin
YAML.load_file('config/config.yml')
rescue Errno::ENOENT
puts "Please create the config.yml file in the config directory"
exit 1
end
end
def exchanges
get("/exchanges")["exchanges"].sort {|a,b| a["name"] <=> b["name"]}
end
def configuration
@configuration ||= begin
config = self.class.configurations[self.id]
raise ConfigurationException.new("Server #{id} not configured") unless config
config
end
end
def to_s
self.id
end
private
[:get, :post].each do |verb|
eval <<-EVAL
def #{verb}(url, opts = {})
self.class.#{verb}(url, opts)
end
EVAL
end
def bunny
@bunny ||= begin
b = Bunny.new(:host => configuration["rabbitmq"]["host"])
b.start
b
end
end
end
end