-
Notifications
You must be signed in to change notification settings - Fork 2
/
collecta.rb
138 lines (119 loc) · 3.77 KB
/
collecta.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require 'xmpp4r'
require 'xmpp4r/pubsub'
require 'crack'
#Jabber::debug = true
module Jabber
module PubSub
class ServiceHelper
# options is a Hash { "key" => "val" }
# will be converted to <field var='key'><value>val</value></field>
def subscribe_to_with_options(node, options)
iq = basic_pubsub_query(:set)
sub = REXML::Element.new('subscribe')
sub.attributes['node'] = node
sub.attributes['jid'] = @stream.jid.strip.to_s
iq.pubsub.add(sub)
iq.pubsub.add(Jabber::PubSub::SubscriptionConfig.new(node, @stream.jid, options, nil))
res = nil
@stream.send_with_id(iq) do |reply|
pubsubanswer = reply.pubsub
if pubsubanswer.first_element('subscription')
res = PubSub::Subscription.import(pubsubanswer.first_element('subscription'))
end
end # @stream.send_with_id(iq)
res
end
end # class
end # module
end # module
module Collecta
class Client < Jabber::Client
attr_reader :node, :service
def initialize(apikey)
@apikey = apikey
@service = "search.collecta.com"
@node = "search"
super("guest.collecta.com/search")
end
# no params to connect() will force SRV lookup
def anonymous_connect
connect
if supports_anonymous?
auth_anonymous_sasl
else
raise ClientAuthenticationFailure.new, "ANONYMOUS SASL not supported"
end
send(Jabber::Presence.new.set_type(:available))
return self
end
def subscribe(query, notify = nil)
raise Jabber::ArgumentError, "Subscription needs some query" unless query
options = { "x-collecta#apikey" => @apikey, "x-collecta#query" => query }
options["x-collecta#notify"] = notify if notify
@pubsub = Jabber::PubSub::ServiceHelper.new(self, @service)
return @pubsub.subscribe_to_with_options(@node, options)
end
def unsubscribe
@pubsub.unsubscribe_from(@node) if @pubsub
end
end # class
# Encapsulated Collecta messages
# http://developer.collecta.com/XmppApi/RealTime/
class Payload
attr_reader :raw
def initialize(text)
begin
@raw = Crack::XML.parse(text.to_s)
@item = @raw['message']['event']['items']['item'] if @raw
self
rescue
return nil
end
end
def type
return :unknown unless @item
return :notify if @item['count']
:query
end
# to which query this message belongs
def meta
type = self.type
return nil if type == :unknown
return @raw['message']["headers"]["header"].to_s if type == :query
return @item['id'] if type == :notify
nil
end
# return entry like a hash
def entry
return nil unless @item and self.type == :query
@item['entry']
end
def category
return nil unless @item and self.type == :query
@item['entry']['category'].to_s
end
def title
return nil unless @item and self.type == :query
@item['entry']['title'].to_s
end
# Collecta will strip the <content> child from the payload if a <summary>
# child is provided.
def body
return nil unless @item
return @item['count'] if self.type == :notify
return @item['entry']['summary'] if @item['entry']['summary']
return @item['entry']['abstract']['p'] unless @item['entry']['content']
@item['entry']['content']
end
def abstract
return nil unless @item
return @item['count'] if self.type == :notify
@item['entry']['abstract']['p']
end
# return array of links
def links
return nil unless @item and self.type == :query and @item['entry']['link']
@item['entry']['link']
end
end # class
end # module