-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollecta_jack.rb
64 lines (55 loc) · 1.85 KB
/
collecta_jack.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
require 'switchboard'
require 'switchboard/helpers/pubsub'
require 'collecta'
#Jabber::debug = true
module Switchboard
class AnonymousClient < Client
protected
def auth!
client.auth_anonymous_sasl
@roster = Jabber::Roster::Helper.new(client)
rescue Jabber::ClientAuthenticationFailure => e
puts "Could not authenticate as #{settings["jid"]}"
shutdown(false)
exit 1
end
end
class CollectaClient < AnonymousClient
DEFAULTS = {
"jid" => "guest.collecta.com",
"resource" => "search",
"pubsub.server" => "search.collecta.com",
"pubsub.node" => "search",
}
def initialize(apikey, query = nil, notify = nil, debug = false)
settings = {
"debug" => debug,
"collecta.apikey" => apikey,
"collecta.query" => query,
"collecta.notify" => notify
}
super(DEFAULTS.merge(settings), true)
plug!(CollectaJack)
end
end
end
class CollectaJack
def self.connect(switchboard, settings)
unless settings["pubsub.server"] and settings["pubsub.node"] and settings["collecta.apikey"]
puts "Needed PubSub server, PubSub node and a Collecta API key"
return false
end
switchboard.plug!(AutoAcceptJack, NotifyJack, PubSubJack)
switchboard.hook(:collecta_message)
switchboard.on_startup do
@pubsub = Jabber::PubSub::ServiceHelper.new(client, settings["pubsub.server"])
options = { "x-collecta#apikey" => settings["collecta.apikey"] }
options["x-collecta#query"] = settings["collecta.query"] if settings["collecta.query"]
options["x-collecta#notify"] = settings["collecta.notify"] if settings["collecta.notify"]
@pubsub.subscribe_to_with_options(settings["pubsub.node"], options)
client.add_message_callback do |msg|
on(:collecta_message, msg)
end
end
end
end