This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
/
atom.rb
234 lines (190 loc) · 5.99 KB
/
atom.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
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
#
# Copyright (c) 2006 Martin Traverso
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
require 'time'
require File.dirname(__FILE__) + '/xmlmapping'
module Atom
NAMESPACE = 'http://www.w3.org/2005/Atom'
XHTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'
class Text < String
attr_reader :mime_type
def initialize(element)
type = element.attribute('type', NAMESPACE)
@mime_type =
case type ? type.value : nil
when 'text' then 'text/plain'
when 'html' then 'text/html'
when 'xhtml' then 'text/xhtml'
when nil then 'text/plain'
else
raise ArgumentError, "Unknown type: #{type.value}"
end
value =
case @mime_type
when 'text/plain', 'text/html'
element.texts.map(&:value).join
when 'text/xhtml'
# TODO: resolve relative uris
REXML::XPath.first(element, 'xhtml:div', 'xhtml' => XHTML_NAMESPACE).children.to_s
end
super value
end
end
class Content
attr_reader :mime_type, :src, :value
def initialize(element)
type = element.attribute('type', NAMESPACE)
src = element.attribute('src', NAMESPACE)
if src.nil?
@mime_type = if type.nil?
'text/plain'
else
case type.value
when 'text': 'text/plain'
when 'html': 'text/html'
when 'xhtml': 'text/xhtml'
else type.value
end
end
@value = case @mime_type
when 'text/plain', 'text/html': element.texts.map { |t| t.value }.join
when 'text/xhtml':
REXML::XPath.first(element, 'xhtml:div', 'xhtml' => XHTML_NAMESPACE).children.to_s
when /\+xml$|\/xml$/: REXML::XPath.first(element).children.to_s
else element.texts.join.strip.unpack("m")[0]
end
else
@src = src.value
if !type.nil?
@mime_type = type.value
end
@value = nil
end
end
end
class Person
include XMLMapping
namespace NAMESPACE
has_one :name
has_one :email
has_one :uri
def to_s
if !email.nil?
"#{name} (#{email})"
else
name
end
end
end
class Generator
include XMLMapping
namespace NAMESPACE
has_attribute :uri
has_attribute :version
text :name
def to_s
name
end
end
class Link
include XMLMapping
namespace NAMESPACE
has_attribute :href
has_attribute :rel, :default => 'alternate'
has_attribute :type
has_attribute :hreflang
has_attribute :title
has_attribute :length
def to_s
href
end
end
class Category
include XMLMapping
namespace NAMESPACE
has_attribute :term
has_attribute :scheme
has_attribute :label
def to_s
term
end
end
class Source
include XMLMapping
namespace NAMESPACE
has_one :id
has_many :authors, :name => 'author', :type => Person
has_many :categories, :name => 'category', :type => Category
has_one :generator, :type => Generator
has_one :icon
has_many :links, :name => 'link', :type => Link
has_one :logo
has_one :rights, :type => Text
has_one :subtitle, :type => Text
has_one :title, :type => Text
has_one :updated, :transform => lambda { |t| Time.iso8601(t) }
has_many :contributors, :name => 'contributor', :type => Person
end
class Entry
include XMLMapping
namespace NAMESPACE
has_one :id
has_one :title, :type => Text
has_one :summary, :type => Text
has_many :authors, :name => 'author', :type => Person
has_many :contributors, :name => 'contributor', :type => Person
has_one :published, :transform => lambda { |t| Time.iso8601(t) }
has_one :updated, :transform => lambda { |t| Time.iso8601(t) }
has_many :links, :name => 'link', :type => Link
has_many :categories, :name => 'category', :type => Category
has_one :content, :type => Content
has_one :source, :type => Source
has_one :rights, :type => Text
has_many :extended_elements, :name => :any, :namespace => :any, :type => :raw
end
class Feed
include XMLMapping
namespace NAMESPACE
has_one :id
has_one :title, :type => Text
has_one :subtitle, :type => Text
has_many :authors, :name => 'author', :type => Person
has_many :contributors, :name => 'contributor', :type => Person
has_many :entries, :name => 'entry', :type => Entry
has_one :generator, :type => Generator
has_many :links, :name => 'link', :type => Link
has_one :updated, :transform => lambda { |t| Time.iso8601(t) }
has_one :rights, :type => Text
has_one :icon
has_one :logo
has_many :categories, :name => 'category', :type => Category
end
end
if $0 == __FILE__
require 'net/http'
require 'uri'
str = Net::HTTP::get(URI::parse('http://blog.ning.com/atom.xml'))
feed = Atom::Feed.new(str)
feed.entries.each { |entry|
puts "'#{entry.title}' by #{entry.authors[0].name} on #{entry.published.strftime('%m/%d/%Y')}"
}
end