vpim 0.658 → 0.695

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ $-w = true
7
7
  $:.unshift File.dirname(__FILE__) + "/../lib"
8
8
 
9
9
 
10
- pp [__LINE__, $:, $"]
10
+ #pp [__LINE__, $:, $"]
11
11
 
12
12
  require 'test/unit'
13
13
 
File without changes
File without changes
File without changes
@@ -9,6 +9,9 @@ module Enumerable
9
9
  def first
10
10
  find{true}
11
11
  end
12
+ def last
13
+ inject{|memo, o| o}
14
+ end
12
15
  end
13
16
 
14
17
  include Vpim
@@ -400,6 +403,25 @@ __
400
403
 
401
404
  end
402
405
 
406
+ def test_location
407
+ cal = Icalendar.decode(<<__).first
408
+ BEGIN:VCALENDAR
409
+ BEGIN:VEVENT
410
+ LOCATION:bien located
411
+ END:VEVENT
412
+ BEGIN:VTODO
413
+ LOCATION:
414
+ END:VTODO
415
+ BEGIN:VEVENT
416
+ END:VEVENT
417
+ END:VCALENDAR
418
+ __
419
+ assert_equal(cal.events.first.location, "bien located")
420
+ assert_equal(cal.todos.first.location, "")
421
+ assert_equal(cal.events.last.location, nil)
422
+ end
423
+
424
+
403
425
  def test_event_maker_w_rrule
404
426
  vc = Icalendar.create2 do |vc|
405
427
  vc.add_event do |m|
@@ -1,23 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'vpim/repo'
4
- require 'vpim/agent/calendars'
5
- require 'test/unit'
6
-
7
- require 'pp'
8
-
9
- module Enumerable
10
- def count
11
- self.inject(0){|i,_| i + 1}
12
- end
13
- end
4
+ require 'test/common'
14
5
 
15
6
  class TestRepo < Test::Unit::TestCase
16
7
  Apple3 = Vpim::Repo::Apple3
17
8
  Directory = Vpim::Repo::Directory
18
- Agent = Vpim::Agent
19
- Path = Agent::Path
20
- NotFound = Agent::NotFound
9
+ Uri = Vpim::Repo::Uri
21
10
 
22
11
  def setup
23
12
  @testdir = Dir.getwd + "/test" #File.dirname($0) doesn't work with rcov :-(
@@ -61,97 +50,60 @@ class TestRepo < Test::Unit::TestCase
61
50
  _test_each(repo, 1)
62
51
  end
63
52
 
64
- def assert_is_text_calendar(text)
65
- lines = text.split("\n")
66
- lines = lines.first, lines.last
67
- assert_equal("BEGIN:VCALENDAR", lines.first.upcase, lines)
68
- assert_equal("END:VCALENDAR", lines.last.upcase, lines)
69
- end
70
-
71
- def test_agent_calendars
72
- repo = Apple3.new(@caldir)
73
- rest = Agent::Calendars.new(repo)
74
-
75
- out1, form = rest.get(Path.new("http://host/here", "/here"))
76
- assert_equal("text/html", form)
77
- #puts(out1)
53
+ def test_uri
54
+ caldata = open('test/calendars/weather.calendar/Events/1205042405-0-0.ics').read
78
55
 
79
- out1, form = rest.get(Path.new("http://host/here/weather%2fLeavenworth", "/here"))
80
- assert_equal("text/html", form)
81
- #puts(out1)
56
+ server = data_on_port(caldata, 9876)
57
+ begin
58
+ c = Uri::Calendar.new("http://localhost:9876")
59
+ assert_equal(caldata, c.encode)
82
60
 
83
- out2, form = rest.get(Path.new("http://host/here/weather%2fLeavenworth/calendar", "/here"))
84
- assert_equal("text/calendar", form)
85
- assert_is_text_calendar(out2)
61
+ repo = Uri.new("http://localhost:9876")
86
62
 
87
- #assert_equal(out1, out2)
63
+ assert_equal(1, repo.count)
88
64
 
89
- assert_raise(Vpim::Agent::NotFound) do
90
- rest.get(Path.new("http://host/here/weather%2fLeavenworth/an_unknown_protocol", "/here"))
91
- end
92
- assert_raise(Vpim::Agent::NotFound) do
93
- rest.get(Path.new("http://host/here/no_such_calendar", "/here"))
65
+ _test_each(repo, 1)
66
+ ensure
67
+ server.kill
94
68
  end
95
-
96
- assert_equal(["","/","/"], Vpim::Agent::Path.split_path("/%2F/%2F"))
97
- assert_equal(["/","/"], Vpim::Agent::Path.split_path("%2F/%2F"))
98
- assert_equal(["calendars", "weather/Leavenworth"],
99
- Vpim::Agent::Path.split_path("calendars/weather%2FLeavenworth"))
100
- end
101
-
102
- def test_agent_calendar_atom
103
- repo = Apple3.new(@caldir)
104
- rest = Agent::Calendars.new(repo)
105
-
106
- out, form = rest.get(Path.new("http://host/here/weather%2fLeavenworth/atom", "/here"))
107
- assert_equal("application/atom+xml", form)
108
- #pp out
109
- #assert_is_atom(out)
110
69
  end
111
70
 
112
- def _test_path_shift(url, shifts)
113
- # last shift should be a nil
114
- shifts << nil
115
-
116
- # presence or absence of a trailing / should not affect shifting
117
- ["", "/"].each do |trailer|
118
- path = Path.new(url + trailer)
119
- shifts.each do |_|
120
- assert_equal(_, path.shift)
121
- end
71
+ def test_uri_invalid
72
+ assert_raises(ArgumentError) do
73
+ Uri.new("url")
74
+ end
75
+ assert_raises(ArgumentError) do
76
+ c = Uri::Calendar.new("url://localhost")
77
+ end
78
+ assert_raises(ArgumentError) do
79
+ c = Uri::Calendar.new("https://localhost")
122
80
  end
123
81
  end
124
82
 
125
- def test_path_shift
126
- _test_path_shift("http://host.ex", [])
127
- _test_path_shift("http://host.ex/a", ["a"])
128
- _test_path_shift("http://host.ex/a/b", ["a", "b"])
129
- _test_path_shift("http://host.ex/a/b/c", ["a", "b", "c"])
130
- end
131
-
132
- def _test_path_prefix(base, parts, shifts, prefix)
133
- path = Path.new(base+parts.join("/"))
134
- shifts.times{ path.shift }
135
- assert_equal(prefix, path.prefix)
136
- end
137
-
138
- def test_path_prefix
139
- _test_path_prefix("http://host.ex/", [], 0, "/")
140
- _test_path_prefix("http://host.ex/", ["a"], 0, "/")
141
- _test_path_prefix("http://host.ex/", ["a"], 1, "/")
142
- _test_path_prefix("http://host.ex/", ["a"], 2, "/a/")
143
- _test_path_prefix("http://host.ex/", ["a"], 3, "/a/")
144
- _test_path_prefix("http://host.ex/", ["a", "b"], 0, "/")
145
- _test_path_prefix("http://host.ex/", ["a", "b"], 1, "/")
146
- _test_path_prefix("http://host.ex/", ["a", "b"], 2, "/a/")
147
- _test_path_prefix("http://host.ex/", ["a", "b"], 3, "/a/b/")
83
+ def test_uri_unreachable
84
+ assert_raises(SocketError) do
85
+ r = Uri.new("http://example.example")
86
+ c = r.find{true}
87
+ c.events{}
88
+ end
89
+ assert_raises(Errno::ECONNREFUSED) do
90
+ r = Uri.new("http://rubyforge.org:81")
91
+ c = r.find{true}
92
+ c.events{}
93
+ end
94
+ assert_raises(StandardError, Vpim::InvalidEncodingError) do
95
+ r = Uri.new("http://rubyforge.org/lua-rocks-my-world")
96
+ c = r.find{true}
97
+ c.events{}
98
+ end
148
99
  end
149
100
 
150
- def test_atomize
151
- repo = Apple3.new(@caldir)
152
- cal = repo.find{true}
153
- a = Vpim::Agent::Atomize.new(cal)
154
- assert( a.get(Path.new("http://example.com/path")))
101
+ def test_uri_unparseable
102
+ assert_raises(Vpim::InvalidEncodingError) do
103
+ r = Uri.new("http://example.com:80")
104
+ c = r.find{true}
105
+ c.events{}
106
+ end
155
107
  end
156
108
 
157
109
  end
File without changes
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vpim
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.658"
4
+ version: "0.695"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Roberts
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-14 00:00:00 -07:00
12
+ date: 2009-03-01 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,14 +26,12 @@ extra_rdoc_files:
26
26
  - COPYING
27
27
  - samples/README.mutt
28
28
  files:
29
- - lib/atom/pub.rb
30
- - lib/atom/version.rb
31
- - lib/atom/xml/parser.rb
32
- - lib/atom.rb
33
- - lib/plist/generator.rb
34
- - lib/plist/parser.rb
35
- - lib/plist.rb
36
29
  - lib/vpim/address.rb
30
+ - lib/vpim/agent/app.rb
31
+ - lib/vpim/agent/atomize.rb
32
+ - lib/vpim/agent/calendars.rb
33
+ - lib/vpim/agent/main.rb
34
+ - lib/vpim/agent/scraps.rb
37
35
  - lib/vpim/attachment.rb
38
36
  - lib/vpim/date.rb
39
37
  - lib/vpim/dirinfo.rb
@@ -81,6 +79,9 @@ files:
81
79
  - samples/vcf-lines.rb
82
80
  - samples/vcf-to-ics.rb
83
81
  - samples/vcf-to-mutt.rb
82
+ - test/test_agent_app.rb
83
+ - test/test_agent_atomize.rb
84
+ - test/test_agent_calendars.rb
84
85
  - test/test_all.rb
85
86
  - test/test_date.rb
86
87
  - test/test_dur.rb
@@ -115,11 +116,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  requirements: []
116
117
 
117
118
  rubyforge_project: vpim
118
- rubygems_version: 1.0.1
119
+ rubygems_version: 1.3.1
119
120
  signing_key:
120
121
  specification_version: 2
121
122
  summary: iCalendar and vCard support for ruby
122
123
  test_files:
124
+ - test/test_agent_app.rb
125
+ - test/test_agent_atomize.rb
126
+ - test/test_agent_calendars.rb
123
127
  - test/test_all.rb
124
128
  - test/test_date.rb
125
129
  - test/test_dur.rb
@@ -1,728 +0,0 @@
1
- # Copyright (c) 2008 The Kaphan Foundation
2
- #
3
- # For licensing information see LICENSE.txt.
4
- =begin License.txt
5
- Copyright (c) 2008 Peerworks
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining
8
- a copy of this software and associated documentation files (the
9
- "Software"), to deal in the Software without restriction, including
10
- without limitation the rights to use, copy, modify, merge, publish,
11
- distribute, sublicense, and/or sell copies of the Software, and to
12
- permit persons to whom the Software is furnished to do so, subject to
13
- the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be
16
- included in all copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
- =end
26
-
27
- require 'forwardable'
28
- require 'delegate'
29
- require 'rubygems'
30
- require 'xml/libxml'
31
- require 'atom/xml/parser.rb'
32
-
33
- module Atom # :nodoc:
34
- NAMESPACE = 'http://www.w3.org/2005/Atom' unless defined?(NAMESPACE)
35
- module Pub
36
- NAMESPACE = 'http://www.w3.org/2007/app'
37
- end
38
- # Raised when a Parsing Error occurs.
39
- class ParseError < StandardError; end
40
- # Raised when a Serialization Error occurs.
41
- class SerializationError < StandardError; end
42
-
43
- # Provides support for reading and writing simple extensions as defined by the Atom Syndication Format.
44
- #
45
- # A Simple extension is an element from a non-atom namespace that has no attributes and only contains
46
- # text content. It is interpreted as a key-value pair when the namespace and the localname of the
47
- # extension make up the key. Since in XML you can have many instances of an element, the values are
48
- # represented as an array of strings, so to manipulate the values manipulate the array returned by
49
- # +[ns, localname]+.
50
- #
51
- module SimpleExtensions
52
- attr_reader :simple_extensions
53
-
54
- # Gets a simple extension value for a given namespace and local name.
55
- #
56
- # +ns+:: The namespace.
57
- # +localname+:: The local name of the extension element.
58
- #
59
- def [](ns, localname)
60
- if !defined?(@simple_extensions) || @simple_extensions.nil?
61
- @simple_extensions = {}
62
- end
63
-
64
- key = "{#{ns},#{localname}}"
65
- (@simple_extensions[key] or @simple_extensions[key] = ValueProxy.new)
66
- end
67
-
68
- class ValueProxy < DelegateClass(Array)
69
- attr_accessor :as_attribute
70
- def initialize
71
- super([])
72
- @as_attribute = false
73
- end
74
- end
75
- end
76
-
77
- # Represents a Generator as defined by the Atom Syndication Format specification.
78
- #
79
- # The generator identifies an agent or engine used to a produce a feed.
80
- #
81
- # See also http://www.atomenabled.org/developers/syndication/atom-format-spec.php#element.generator
82
- class Generator
83
- include Xml::Parseable
84
-
85
- attr_accessor :name
86
- attribute :uri, :version
87
-
88
- # Initialize a new Generator.
89
- #
90
- # +xml+:: An XML::Reader object.
91
- #
92
- def initialize(o = nil)
93
- case o
94
- when XML::Reader
95
- @name = o.read_string.strip
96
- parse(o, :once => true)
97
- when Hash
98
- o.each do |k, v|
99
- self.send("#{k.to_s}=", v)
100
- end
101
- end
102
-
103
- yield(self) if block_given?
104
- end
105
- end
106
-
107
- # Represents a Category as defined by the Atom Syndication Format specification.
108
- #
109
- #
110
- class Category
111
- include Atom::Xml::Parseable
112
- include SimpleExtensions
113
- attribute :label, :scheme, :term
114
-
115
- def initialize(o = nil)
116
- case o
117
- when XML::Reader
118
- parse(o, :once => true)
119
- when Hash
120
- o.each do |k, v|
121
- self.send("#{k.to_s}=", v)
122
- end
123
- end
124
-
125
- yield(self) if block_given?
126
- end
127
- end
128
-
129
- # Represents a Person as defined by the Atom Syndication Format specification.
130
- #
131
- # A Person is used for all author and contributor attributes.
132
- #
133
- # See also http://www.atomenabled.org/developers/syndication/atom-format-spec.php#atomPersonConstruct
134
- #
135
- class Person
136
- include Xml::Parseable
137
- element :name, :uri, :email
138
-
139
- # Initialize a new person.
140
- #
141
- # +o+:: An XML::Reader object or a hash. Valid hash keys are +:name+, +:uri+ and +:email+.
142
- def initialize(o = {})
143
- case o
144
- when XML::Reader
145
- o.read
146
- parse(o)
147
- when Hash
148
- o.each do |k, v|
149
- self.send("#{k.to_s}=", v)
150
- end
151
- end
152
- end
153
-
154
- def inspect
155
- "<Atom::Person name:'#{name}' uri:'#{uri}' email:'#{email}"
156
- end
157
- end
158
-
159
- class Content # :nodoc:
160
- def self.parse(xml)
161
- case xml['type']
162
- when "xhtml"
163
- Xhtml.new(xml)
164
- when "html"
165
- Html.new(xml)
166
- else
167
- Text.new(xml)
168
- end
169
- end
170
-
171
- # This is the base class for all content within an atom document.
172
- #
173
- # Content can be Text, Html or Xhtml.
174
- #
175
- # A Content object can be treated as a String with type and xml_lang
176
- # attributes.
177
- #
178
- # For a thorough discussion of atom content see
179
- # http://www.atomenabled.org/developers/syndication/atom-format-spec.php#element.content
180
- class Base < DelegateClass(String)
181
- include Xml::Parseable
182
-
183
- def initialize(c)
184
- __setobj__(c)
185
- end
186
-
187
- def ==(o)
188
- if o.is_a?(self.class)
189
- self.type == o.type &&
190
- self.xml_lang == o.xml_lang &&
191
- self.to_s == o.to_s
192
- elsif o.is_a?(String)
193
- self.to_s == o
194
- end
195
- end
196
-
197
- protected
198
- def set_content(c) # :nodoc:
199
- __setobj__(c)
200
- end
201
- end
202
-
203
- # Text content within an Atom document.
204
- class Text < Base
205
- attribute :type, :'xml:lang'
206
- def initialize(xml)
207
- super(xml.read_string)
208
- parse(xml, :once => true)
209
- end
210
-
211
- def to_xml(nodeonly = true, name = 'content', namespace = nil, namespace_map = Atom::Xml::NamespaceMap.new)
212
- node = XML::Node.new("#{namespace_map.get(Atom::NAMESPACE)}:#{name}")
213
- node << self.to_s
214
- node
215
- end
216
- end
217
-
218
- # Html content within an Atom document.
219
- class Html < Base
220
- attribute :type, :'xml:lang'
221
- # Creates a new Content::Html.
222
- #
223
- # +o+:: An XML::Reader or a HTML string.
224
- #
225
- def initialize(o)
226
- case o
227
- when XML::Reader
228
- super(o.read_string.gsub(/\s+/, ' ').strip)
229
- parse(o, :once => true)
230
- when String
231
- super(o)
232
- @type = 'html'
233
- end
234
- end
235
-
236
- def to_xml(nodeonly = true, name = 'content', namespace = nil, namespace_map = Atom::Xml::NamespaceMap.new) # :nodoc:
237
- require 'iconv'
238
- # Convert from utf-8 to utf-8 as a way of making sure the content is UTF-8.
239
- #
240
- # This is a pretty crappy way to do it but if we don't check libxml just
241
- # fails silently and outputs the content element without any content. At
242
- # least checking here and raising an exception gives the caller a chance
243
- # to try and recitfy the situation.
244
- #
245
- begin
246
- node = XML::Node.new("#{namespace_map.get(Atom::NAMESPACE)}:#{name}")
247
- node << Iconv.iconv('utf-8', 'utf-8', self.to_s, namespace_map = nil)
248
- node['type'] = 'html'
249
- node['xml:lang'] = self.xml_lang
250
- node
251
- rescue Iconv::IllegalSequence => e
252
- raise SerializationError, "Content must be converted to UTF-8 before attempting to serialize to XML: #{e.message}."
253
- end
254
- end
255
- end
256
-
257
- # XHTML content within an Atom document.
258
- class Xhtml < Base
259
- XHTML = 'http://www.w3.org/1999/xhtml'
260
- attribute :type, :'xml:lang'
261
-
262
- def initialize(xml)
263
- super("")
264
- parse(xml, :once => true)
265
- starting_depth = xml.depth
266
-
267
- # Get the next element - should be a div according to the atom spec
268
- while xml.read == 1 && xml.node_type != XML::Reader::TYPE_ELEMENT; end
269
-
270
- if xml.local_name == 'div' && xml.namespace_uri == XHTML
271
- set_content(xml.read_inner_xml.strip.gsub(/\s+/, ' '))
272
- else
273
- set_content(xml.read_outer_xml)
274
- end
275
-
276
- # get back to the end of the element we were created with
277
- while xml.read == 1 && xml.depth > starting_depth; end
278
- end
279
-
280
- def to_xml(nodeonly = true, name = 'content', namespace = nil, namespace_map = Atom::Xml::NamespaceMap.new)
281
- node = XML::Node.new("#{namespace_map.get(Atom::NAMESPACE)}:#{name}")
282
- node['type'] = 'xhtml'
283
- node['xml:lang'] = self.xml_lang
284
-
285
- div = XML::Node.new('div')
286
- div['xmlns'] = XHTML
287
-
288
- p = XML::Parser.string(to_s)
289
- content = p.parse.root.copy(true)
290
- div << content
291
-
292
- node << div
293
- node
294
- end
295
- end
296
- end
297
-
298
- # Represents a Source as defined by the Atom Syndication Format specification.
299
- #
300
- # See also http://www.atomenabled.org/developers/syndication/atom-format-spec.php#element.source
301
- class Source
302
- extend Forwardable
303
- def_delegators :@links, :alternate, :self, :alternates, :enclosures
304
- include Xml::Parseable
305
-
306
- element :id
307
- element :updated, :class => Time, :content_only => true
308
- element :title, :subtitle, :class => Content
309
- elements :authors, :contributors, :class => Person
310
- elements :links
311
-
312
- def initialize(o = nil)
313
- @authors, @contributors, @links = [], [], Links.new
314
-
315
- case o
316
- when XML::Reader
317
- unless current_node_is?(o, 'source', NAMESPACE)
318
- raise ArgumentError, "Invalid node for atom:source - #{o.name}(#{o.namespace})"
319
- end
320
-
321
- o.read
322
- parse(o)
323
- when Hash
324
- o.each do |k, v|
325
- self.send("#{k.to_s}=", v)
326
- end
327
- end
328
-
329
- yield(self) if block_given?
330
- end
331
- end
332
-
333
- # Represents a Feed as defined by the Atom Syndication Format specification.
334
- #
335
- # A feed is the top level element in an atom document. It is a container for feed level
336
- # metadata and for each entry in the feed.
337
- #
338
- # This supports pagination as defined in RFC 5005, see http://www.ietf.org/rfc/rfc5005.txt
339
- #
340
- # == Parsing
341
- #
342
- # A feed can be parsed using the Feed.load_feed method. This method accepts a String containing
343
- # a valid atom document, an IO object, or an URI to a valid atom document. For example:
344
- #
345
- # # Using a File
346
- # feed = Feed.load_feed(File.open("/path/to/myfeed.atom"))
347
- #
348
- # # Using a URL
349
- # feed = Feed.load_feed(URI.parse("http://example.org/afeed.atom"))
350
- #
351
- # == Encoding
352
- #
353
- # A feed can be converted to XML using, the to_xml method that returns a valid atom document in a String.
354
- #
355
- # == Attributes
356
- #
357
- # A feed has the following attributes:
358
- #
359
- # +id+:: A unique id for the feed.
360
- # +updated+:: The Time the feed was updated.
361
- # +title+:: The title of the feed.
362
- # +subtitle+:: The subtitle of the feed.
363
- # +authors+:: An array of Atom::Person objects that are authors of this feed.
364
- # +contributors+:: An array of Atom::Person objects that are contributors to this feed.
365
- # +generator+:: A Atom::Generator.
366
- # +rights+:: A string describing the rights associated with this feed.
367
- # +entries+:: An array of Atom::Entry objects.
368
- # +links+:: An array of Atom:Link objects. (This is actually an Atom::Links array which is an Array with some sugar).
369
- #
370
- # == References
371
- # See also http://www.atomenabled.org/developers/syndication/atom-format-spec.php#element.feed
372
- #
373
- class Feed
374
- include Xml::Parseable
375
- include SimpleExtensions
376
- extend Forwardable
377
- def_delegators :@links, :alternate, :self, :via, :first_page, :last_page, :next_page, :prev_page
378
-
379
- loadable!
380
-
381
- namespace Atom::NAMESPACE
382
- element :id, :rights
383
- element :generator, :class => Generator
384
- element :title, :subtitle, :class => Content
385
- element :updated, :class => Time, :content_only => true
386
- elements :links
387
- elements :authors, :contributors, :class => Person
388
- elements :entries
389
-
390
- # Initialize a Feed.
391
- #
392
- # This will also yield itself, so a feed can be constructed like this:
393
- #
394
- # feed = Feed.new do |feed|
395
- # feed.title = "My Cool feed"
396
- # end
397
- #
398
- # +o+:: An XML Reader or a Hash of attributes.
399
- #
400
- def initialize(o = {})
401
- @links, @entries, @authors, @contributors = Links.new, [], [], []
402
-
403
- case o
404
- when XML::Reader
405
- if next_node_is?(o, 'feed', Atom::NAMESPACE)
406
- o.read
407
- parse(o)
408
- else
409
- raise ArgumentError, "XML document was missing atom:feed: #{o.read_outer_xml}"
410
- end
411
- when Hash
412
- o.each do |k, v|
413
- self.send("#{k.to_s}=", v)
414
- end
415
- end
416
-
417
- yield(self) if block_given?
418
- end
419
-
420
- # Return true if this is the first feed in a paginated set.
421
- def first?
422
- links.self == links.first_page
423
- end
424
-
425
- # Returns true if this is the last feed in a paginated set.
426
- def last?
427
- links.self == links.last_page
428
- end
429
-
430
- # Reloads the feed by fetching the self uri.
431
- def reload!
432
- if links.self
433
- Feed.load_feed(URI.parse(links.self.href))
434
- end
435
- end
436
-
437
- # Iterates over each entry in the feed.
438
- #
439
- # ==== Options
440
- #
441
- # +paginate+:: If true and the feed supports pagination this will fetch each page of the feed.
442
- # +since+:: If a Time object is provided each_entry will iterate over all entries that were updated since that time.
443
- #
444
- def each_entry(options = {}, &block)
445
- if options[:paginate]
446
- since_reached = false
447
- feed = self
448
- loop do
449
- feed.entries.each do |entry|
450
- if options[:since] && entry.updated && options[:since] > entry.updated
451
- since_reached = true
452
- break
453
- else
454
- block.call(entry)
455
- end
456
- end
457
-
458
- if since_reached || feed.next_page.nil?
459
- break
460
- else feed.next_page
461
- feed = feed.next_page.fetch
462
- end
463
- end
464
- else
465
- self.entries.each(&block)
466
- end
467
- end
468
- end
469
-
470
- # Represents an Entry as defined by the Atom Syndication Format specification.
471
- #
472
- # An Entry represents an individual entry within a Feed.
473
- #
474
- # == Parsing
475
- #
476
- # An Entry can be parsed using the Entry.load_entry method. This method accepts a String containing
477
- # a valid atom entry document, an IO object, or an URI to a valid atom entry document. For example:
478
- #
479
- # # Using a File
480
- # entry = Entry.load_entry(File.open("/path/to/myfeedentry.atom"))
481
- #
482
- # # Using a URL
483
- # Entry = Entry.load_entry(URI.parse("http://example.org/afeedentry.atom"))
484
- #
485
- # The document must contain a stand alone entry element as described in the Atom Syndication Format.
486
- #
487
- # == Encoding
488
- #
489
- # A Entry can be converted to XML using, the to_xml method that returns a valid atom entry document in a String.
490
- #
491
- # == Attributes
492
- #
493
- # An entry has the following attributes:
494
- #
495
- # +id+:: A unique id for the entry.
496
- # +updated+:: The Time the entry was updated.
497
- # +published+:: The Time the entry was published.
498
- # +title+:: The title of the entry.
499
- # +summary+:: A short textual summary of the item.
500
- # +authors+:: An array of Atom::Person objects that are authors of this entry.
501
- # +contributors+:: An array of Atom::Person objects that are contributors to this entry.
502
- # +rights+:: A string describing the rights associated with this entry.
503
- # +links+:: An array of Atom:Link objects. (This is actually an Atom::Links array which is an Array with some sugar).
504
- # +source+:: Metadata of a feed that was the source of this item, for feed aggregators, etc.
505
- # +categories+:: Array of Atom::Categories.
506
- # +content+:: The content of the entry. This will be one of Atom::Content::Text, Atom::Content:Html or Atom::Content::Xhtml.
507
- #
508
- # == References
509
- # See also http://www.atomenabled.org/developers/syndication/atom-format-spec.php#element.entry for more detailed
510
- # definitions of the attributes.
511
- #
512
- class Entry
513
- include Xml::Parseable
514
- include SimpleExtensions
515
- extend Forwardable
516
- def_delegators :@links, :alternate, :self, :alternates, :enclosures, :edit_link, :via
517
-
518
- loadable!
519
- namespace Atom::NAMESPACE
520
- element :title, :id, :summary
521
- element :updated, :published, :class => Time, :content_only => true
522
- element :content, :class => Content
523
- element :source, :class => Source
524
- elements :links
525
- elements :authors, :contributors, :class => Person
526
- elements :categories, :class => Category
527
-
528
- # Initialize an Entry.
529
- #
530
- # This will also yield itself, so an Entry can be constructed like this:
531
- #
532
- # entry = Entry.new do |entry|
533
- # entry.title = "My Cool entry"
534
- # end
535
- #
536
- # +o+:: An XML Reader or a Hash of attributes.
537
- #
538
- def initialize(o = {})
539
- @links = Links.new
540
- @authors = []
541
- @contributors = []
542
- @categories = []
543
-
544
- case o
545
- when XML::Reader
546
- if current_node_is?(o, 'entry', Atom::NAMESPACE) || next_node_is?(o, 'entry', Atom::NAMESPACE)
547
- o.read
548
- parse(o)
549
- else
550
- raise ArgumentError, "Entry created with node other than atom:entry: #{o.name}"
551
- end
552
- when Hash
553
- o.each do |k,v|
554
- send("#{k.to_s}=", v)
555
- end
556
- end
557
-
558
- yield(self) if block_given?
559
- end
560
-
561
- # Reload the Entry by fetching the self link.
562
- def reload!
563
- if links.self
564
- Entry.load_entry(URI.parse(links.self.href))
565
- end
566
- end
567
- end
568
-
569
- # Links provides an Array of Link objects belonging to either a Feed or an Entry.
570
- #
571
- # Some additional methods to get specific types of links are provided.
572
- #
573
- # == References
574
- #
575
- # See also http://www.atomenabled.org/developers/syndication/atom-format-spec.php#element.link
576
- # for details on link selection and link attributes.
577
- #
578
- class Links < DelegateClass(Array)
579
- include Enumerable
580
-
581
- # Initialize an empty Links array.
582
- def initialize
583
- super([])
584
- end
585
-
586
- # Get the alternate.
587
- #
588
- # Returns the first link with rel == 'alternate' that matches the given type.
589
- def alternate(type = nil)
590
- detect { |link| (link.rel.nil? || link.rel == Link::Rel::ALTERNATE) && (type.nil? || type == link.type) }
591
- end
592
-
593
- # Get all alternates.
594
- def alternates
595
- select { |link| link.rel.nil? || link.rel == Link::Rel::ALTERNATE }
596
- end
597
-
598
- # Gets the self link.
599
- def self
600
- detect { |link| link.rel == Link::Rel::SELF }
601
- end
602
-
603
- # Gets the via link.
604
- def via
605
- detect { |link| link.rel == Link::Rel::VIA }
606
- end
607
-
608
- # Gets all links with rel == 'enclosure'
609
- def enclosures
610
- select { |link| link.rel == Link::Rel::ENCLOSURE }
611
- end
612
-
613
- # Gets the link with rel == 'first'.
614
- #
615
- # This is defined as the first page in a pagination set.
616
- def first_page
617
- detect { |link| link.rel == Link::Rel::FIRST }
618
- end
619
-
620
- # Gets the link with rel == 'last'.
621
- #
622
- # This is defined as the last page in a pagination set.
623
- def last_page
624
- detect { |link| link.rel == Link::Rel::LAST }
625
- end
626
-
627
- # Gets the link with rel == 'next'.
628
- #
629
- # This is defined as the next page in a pagination set.
630
- def next_page
631
- detect { |link| link.rel == Link::Rel::NEXT }
632
- end
633
-
634
- # Gets the link with rel == 'prev'.
635
- #
636
- # This is defined as the previous page in a pagination set.
637
- def prev_page
638
- detect { |link| link.rel == Link::Rel::PREVIOUS }
639
- end
640
-
641
- # Gets the edit link.
642
- #
643
- # This is the link which can be used for posting updates to an item using the Atom Publishing Protocol.
644
- #
645
- def edit_link
646
- detect { |link| link.rel == 'edit' }
647
- end
648
- end
649
-
650
- # Represents a link in an Atom document.
651
- #
652
- # A link defines a reference from an Atom document to a web resource.
653
- #
654
- # == References
655
- # See http://www.atomenabled.org/developers/syndication/atom-format-spec.php#element.link for
656
- # a description of the different types of links.
657
- #
658
- class Link
659
- module Rel # :nodoc:
660
- ALTERNATE = 'alternate'
661
- SELF = 'self'
662
- VIA = 'via'
663
- ENCLOSURE = 'enclosure'
664
- FIRST = 'first'
665
- LAST = 'last'
666
- PREVIOUS = 'prev'
667
- NEXT = 'next'
668
- end
669
-
670
- include Xml::Parseable
671
- attribute :href, :rel, :type, :length
672
-
673
- # Create a link.
674
- #
675
- # +o+:: An XML::Reader containing a link element or a Hash of attributes.
676
- #
677
- def initialize(o)
678
- case o
679
- when XML::Reader
680
- if current_node_is?(o, 'link')
681
- parse(o, :once => true)
682
- else
683
- raise ArgumentError, "Link created with node other than atom:link: #{o.name}"
684
- end
685
- when Hash
686
- [:href, :rel, :type, :length].each do |attr|
687
- self.send("#{attr}=", o[attr])
688
- end
689
- else
690
- raise ArgumentError, "Don't know how to handle #{o}"
691
- end
692
- end
693
-
694
- remove_method :length=
695
- def length=(v)
696
- @length = v.to_i
697
- end
698
-
699
- def to_s
700
- self.href
701
- end
702
-
703
- def ==(o)
704
- o.respond_to?(:href) && o.href == self.href
705
- end
706
-
707
- # This will fetch the URL referenced by the link.
708
- #
709
- # If the URL contains a valid feed, a Feed will be returned, otherwise,
710
- # the body of the response will be returned.
711
- #
712
- # TODO: Handle redirects.
713
- #
714
- def fetch
715
- content = Net::HTTP.get_response(URI.parse(self.href)).body
716
-
717
- begin
718
- Atom::Feed.load_feed(content)
719
- rescue ArgumentError, ParseError => ae
720
- content
721
- end
722
- end
723
-
724
- def inspect
725
- "<Atom::Link href:'#{href}' type:'#{type}'>"
726
- end
727
- end
728
- end