-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnotification_stanza.rb
More file actions
278 lines (233 loc) · 7.14 KB
/
notification_stanza.rb
File metadata and controls
278 lines (233 loc) · 7.14 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
##
# Repesents the items published by the firehoser (the feed entries).
# They have accessors for the following fields :
# - title
# - summary
# - link
# - published
# - unique_id
# - chunks (long entries might be notified in several chunks)
# - chunk (current chunk out of chunks)
#
# <item xmlns="http://jabber.org/protocol/pubsub" chunks="2" chunk="1">
# <entry xmlns="http://www.w3.org/2005/Atom" xml:lang="">
# <title>cool</title>
# <content type="text">cool</content>
# <id>tag:pubhubsubbub-example-app,2009:ahhwdWJzdWJodWJidWItZXhhbXBsZS1hcHByDQsSBUVudHJ5GMGOEAw</id>
# <published>2010-03-25T16:57:18Z</published>
# <link type="text/html" href="http://pubsubhubbub-example-app.appspot.com/ahhwdWJzdWJodWJidWItZXhhbXBsZS1hcHByDQsSBUVudHJ5GMGOEAw" title="" rel="alternate"/>
# </entry>
# </item>
require 'time'
class Link
def initialize(node)
@node = node
end
def title
@node["title"]
end
def href
@node["href"]
end
def rel
@node["rel"]
end
def type
@node["type"]
end
end
class Author
def initialize(node)
@node = node
end
def name
if !@name
if name = @node.at_xpath("./atom:name", {"atom" => "http://www.w3.org/2005/Atom"})
@name = name.text
end
end
end
def uri
if !@uri
if uri = @node.at_xpath("./atom:uri", {"atom" => "http://www.w3.org/2005/Atom"})
@uri = uri.text
end
end
end
def email
if !@email
if email = @node.at_xpath("./atom:email", {"atom" => "http://www.w3.org/2005/Atom"})
@email = email.text
end
end
end
end
class Category
def initialize(node)
@node = node
end
def term
@node["term"]
end
end
class Location
def initialize(node)
@node = node
end
def point
@point ||= @node.text
end
def lat
@lat ||= point.split().first.to_f
end
def lon
@lon ||= point.split().last.to_f
end
end
class Item
def initialize(node)
@node = node
end
def title
@title ||= @node.at_xpath("./atom:entry/atom:title", {"atom" => "http://www.w3.org/2005/Atom"}).text
end
def summary
if !@summary
if summary = @node.at_xpath("./atom:entry/atom:summary", {"atom" => "http://www.w3.org/2005/Atom"})
@summary = summary.text
end
end
@summary
end
def content
if !@content
if content = @node.at_xpath("./atom:entry/atom:content", {"atom" => "http://www.w3.org/2005/Atom"})
@content = content.text
end
end
@content
end
def unique_id
@unique_id ||= @node.at_xpath("./atom:entry/atom:id", {"atom" => "http://www.w3.org/2005/Atom"}).text
end
def published
if !@published
if published = @node.at_xpath("./atom:entry/atom:published", {"atom" => "http://www.w3.org/2005/Atom"}).text
@published = Time.parse(published)
end
end
@published
end
def chunks
@node["chunks"].to_i
end
def chunk
@node["chunk"].to_i
end
def links
if !@links
@links = []
@node.xpath("./atom:entry/atom:link", {"atom" => "http://www.w3.org/2005/Atom"}).each do |node|
@links.push(Link.new(node))
end
end
@links
end
def authors
if !@authors
@authors = []
@node.xpath("./atom:entry/atom:author", {"atom" => "http://www.w3.org/2005/Atom"}).each do |node|
@authors.push(Author.new(node))
end
end
@authors
end
def categories
if !@categories
@categories = []
@node.xpath("./atom:entry/atom:category", {"atom" => "http://www.w3.org/2005/Atom"}).each do |node|
@categories.push(Category.new(node))
end
end
@categories
end
def locations
if !@locations
@locations = []
@node.xpath("./atom:entry/georss:point", {"atom" => "http://www.w3.org/2005/Atom", "georss" => "http://www.georss.org/georss"}).each do |node|
@locations.push(Location.new(node))
end
end
@locations
end
end
##
# Notification : sent every time a feed has been fetched. It has the following methods:
# - message_status : a simple message that gives information about the last fetch
# - http_status : status of the http response
# - feed_url : url of the feed
# - next_fetch : Time when the feed will be fetched again (this is purely informative and it might change)
# - items : array of new items detected (might be empty)
#
#
# <message xmlns="jabber:client" from="firehoser.superfeedr.com" to="julien@superfeedr.com">
# <event xmlns="http://jabber.org/protocol/pubsub#event">
# <status xmlns="http://superfeedr.com/xmpp-pubsub-ext" feed="http://pubsubhubbub-example-app.appspot.com/feed">
# <http code="200">25002 bytes fetched in 0.73s for 1 new entries.</http>
# <next_fetch>2010-03-25T17:06:30+00:00</next_fetch>
# <title>PubSubHubBub example app</title>
# </status>
# <items node="http://pubsubhubbub-example-app.appspot.com/feed">
# <item xmlns="http://jabber.org/protocol/pubsub" chunks="1" chunk="1">
# <entry xmlns="http://www.w3.org/2005/Atom" xml:lang="" xmlns:xml="http://www.w3.org/XML/1998/namespace">
# <title>cool</title>
# <content type="text">cool</content>
# <id>tag:pubhubsubbub-example-app,2009:ahhwdWJzdWJodWJidWItZXhhbXBsZS1hcHByDQsSBUVudHJ5GMGOEAw</id>
# <published>2010-03-25T16:57:18Z</published>
# <link type="text/html" href="http://pubsubhubbub-example-app.appspot.com/ahhwdWJzdWJodWJidWItZXhhbXBsZS1hcHByDQsSBUVudHJ5GMGOEAw" title="" rel="alternate"/>
# </entry>
# <entry xmlns="http://www.w3.org/2005/Atom" xml:lang="" xmlns:xml="http://www.w3.org/XML/1998/namespace">
# <title>great</title>
# <content type="text">great</content>
# <id>tag:pubhubsubbub-example-app,2009:ahhwdWJzdWJodWJidWItZXhhbXBsZS1hcHByDQsSBUVudHJ5GMGOEAx</id>
# <published>2010-03-25T16:57:19Z</published>
# <link type="text/html" href="http://pubsubhubbub-example-app.appspot.com/ahhwdWJzdWJodWJidWItZXhhbXBsZS1hcHByDQsSBUVudHJ5GMGOEAx" title="" rel="alternate"/>
# </entry>
# </item>
# </items>
# </event>
# </message>
class NotificationStanza < Skates::Base::Stanza
XMLNS = {
"ps" => "http://jabber.org/protocol/pubsub#event",
"ps2" => "http://jabber.org/protocol/pubsub",
"sf" => "http://superfeedr.com/xmpp-pubsub-ext" } unless defined? XMLNS
def next_fetch
if !@next_fetch
time = @node.at_xpath("./ps:event/sf:status/sf:next_fetch", XMLNS).text
@next_fetch = Time.parse(time)
end
@next_fetch
end
def http_status
@http_status ||= @node.at_xpath("./ps:event/sf:status/sf:http/@code", XMLNS).text.to_i
end
def feed_url
@feed_url ||= @node.at_xpath("./ps:event/sf:status/@feed", XMLNS).text
end
def message_status
@message_status ||= @node.at_xpath("./ps:event/sf:status/sf:http", XMLNS).text
end
def title
@title ||= @node.at_xpath("./ps:event/sf:status/sf:title", XMLNS).text
end
def entries
if !@entries
@entries = []
@node.xpath("./ps:event/ps:items/ps2:item", XMLNS).each do |node|
@entries.push(Item.new(node))
end
end
@entries
end
end