-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledsign_legacy_text_feeder
executable file
·73 lines (66 loc) · 2.18 KB
/
ledsign_legacy_text_feeder
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
#!/usr/bin/ruby
# LED Sign Text Feeder
# Feeds text from the old notification exchange to the LED Sign.
# Input: Exchange 'notification.text'
# Output: LED sign via LED Sign Text Renderer HTTP API
# TODO:
# This is legacy and should be eliminated! Right now this still exists so
# that !sign on IRC doesn't go into notification.general which would feed back
# into IRC and be annoying. We should fix this with routing keys and eliminate
# this tool and the old notification.text queue entirely.
require 'rubygems'
require 'amqp'
require 'net/http'
require 'cgi'
require 'inifile'
cfg = IniFile.load(File.expand_path("~/.labamqprc"))
amqp_host = cfg['broker']['BrokerHost']
amqp_user = cfg['ledsign']['BrokerUsername']
amqp_pass = cfg['ledsign']['BrokerPassword']
class Hash
def stringify_keys
inject({}) do |h, (key,value)|
h[key.to_s] = value
h
end
end
def stringify_keys!
keys.each do |key|
unless key.class.to_s == "String" # See ActiveSupport for why this is needed!
self[key.to_s] = self[key]
delete(key)
end
end
self
end
end
# Turns a hash into ?var=baz&bam=boo
# From https://github.com/lisa/reve/blob/master/lib/reve.rb
def format_url_request(opts)
req = "?"
opts.stringify_keys!
opts.keys.sort.each do |key|
req += "#{CGI.escape(key.to_s)}=#{CGI.escape(opts[key].to_s)}&" if opts[key]
end
req.chop # We are lazy and append a & to each pair even if it's the last one. FIXME: Don't do this.
end
AMQP.logging = true
AMQP.start(:host => amqp_host, :user => amqp_user, :pass => amqp_pass) do
amq = AMQP::Channel.new
exchange = amq.fanout('notification.text', :durable => true, :auto_delete => false);
queue = amq.queue('notification.text.ledsign', :durable => false, :auto_delete => true);
queue.bind(exchange);
queue.subscribe do |header, msg|
get_data = {
:Action => 'ShowMessage',
:Version => '2009-02-03',
:Message => msg
}
get_data.stringify_keys!
query_string = format_url_request(get_data)
p query_string
request = Net::HTTP::Get.new("/signservice/SignService" + query_string)
http = Net::HTTP.new("localhost", 8080)
http.start { |http| http.request(request) }
end
end