Class: Atom::Feed
- Inherits:
-
Object
- Object
- Atom::Feed
- Extended by:
- Forwardable
- Includes:
- SimpleExtensions, Xml::Parseable
- Defined in:
- lib/atom.rb
Overview
Represents a Feed as defined by the Atom Syndication Format specification.
A feed is the top level element in an atom document. It is a container for feed level metadata and for each entry in the feed.
This supports pagination as defined in RFC 5005, see www.ietf.org/rfc/rfc5005.txt
Parsing
A feed can be parsed using the Feed.load_feed method. This method accepts a String containing a valid atom document, an IO object, or an URI to a valid atom document. For example:
# Using a File
feed = Feed.load_feed(File.open("/path/to/myfeed.atom"))
# Using a URL
feed = Feed.load_feed(URI.parse("http://example.org/afeed.atom"))
Encoding
A feed can be converted to XML using, the to_xml method that returns a valid atom document in a String.
Attributes
A feed has the following attributes:
id
-
A unique id for the feed.
updated
-
The Time the feed was updated.
title
-
The title of the feed.
subtitle
-
The subtitle of the feed.
authors
-
An array of Atom::Person objects that are authors of this feed.
contributors
-
An array of Atom::Person objects that are contributors to this feed.
generator
-
A Atom::Generator.
rights
-
A string describing the rights associated with this feed.
entries
-
An array of Atom::Entry objects.
links
-
An array of Atom:Link objects. (This is actually an Atom::Links array which is an Array with some sugar).
References
See also www.atomenabled.org/developers/syndication/atom-format-spec.php#element.feed
Instance Attribute Summary
Attributes included from SimpleExtensions
Instance Method Summary collapse
-
#each_entry(options = {}, &block) ⇒ Object
Iterates over each entry in the feed.
-
#first? ⇒ Boolean
Return true if this is the first feed in a paginated set.
-
#initialize(o = {}) {|_self| ... } ⇒ Feed
constructor
Initialize a Feed.
-
#last? ⇒ Boolean
Returns true if this is the last feed in a paginated set.
-
#reload! ⇒ Object
Reloads the feed by fetching the self uri.
Methods included from SimpleExtensions
Constructor Details
#initialize(o = {}) {|_self| ... } ⇒ Feed
Initialize a Feed.
This will also yield itself, so a feed can be constructed like this:
feed = Feed.new do |feed|
feed.title = "My Cool feed"
end
o
-
An XML Reader or a Hash of attributes.
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/atom.rb', line 400 def initialize(o = {}) @links, @entries, @authors, @contributors = Links.new, [], [], [] case o when XML::Reader if next_node_is?(o, 'feed', Atom::NAMESPACE) o.read parse(o) else raise ArgumentError, "XML document was missing atom:feed: #{o.read_outer_xml}" end when Hash o.each do |k, v| self.send("#{k.to_s}=", v) end end yield(self) if block_given? end |
Instance Method Details
#each_entry(options = {}, &block) ⇒ Object
Iterates over each entry in the feed.
Options
paginate
-
If true and the feed supports pagination this will fetch each page of the feed.
since
-
If a Time object is provided each_entry will iterate over all entries that were updated since that time.
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/atom.rb', line 444 def each_entry( = {}, &block) if [:paginate] since_reached = false feed = self loop do feed.entries.each do |entry| if [:since] && entry.updated && [:since] > entry.updated since_reached = true break else block.call(entry) end end if since_reached || feed.next_page.nil? break else feed.next_page feed = feed.next_page.fetch end end else self.entries.each(&block) end end |
#first? ⇒ Boolean
Return true if this is the first feed in a paginated set.
421 422 423 |
# File 'lib/atom.rb', line 421 def first? links.self == links.first_page end |
#last? ⇒ Boolean
Returns true if this is the last feed in a paginated set.
426 427 428 |
# File 'lib/atom.rb', line 426 def last? links.self == links.last_page end |
#reload! ⇒ Object
Reloads the feed by fetching the self uri.
431 432 433 434 435 |
# File 'lib/atom.rb', line 431 def reload! if links.self Feed.load_feed(URI.parse(links.self.href)) end end |