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 http://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:
idA unique id for the feed.
updatedThe Time the feed was updated.
titleThe title of the feed.
subtitleThe subtitle of the feed.
authorsAn array of Atom::Person objects that are authors of this feed.
contributorsAn array of Atom::Person objects that are contributors to this feed.
generatorA Atom::Generator.
categoriesA list of Atom:Category objects for the feed.
rightsA string describing the rights associated with this feed.
entriesAn array of Atom::Entry objects.
linksAn array of Atom:Link objects. (This is actually an Atom::Links array which is an Array with some sugar).
References
See also http://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!(opts = {}) ⇒ Object
Reloads the feed by fetching the self uri.
Methods included from SimpleExtensions
Methods included from Xml::Parseable
#==, #accessor_name, #current_node_is?, included, #next_node_is?, #parse, #to_xml
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
oAn XML Reader or a Hash of attributes.
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/atom.rb', line 471 def initialize(o = {}) @links, @entries, , @contributors, @categories = 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 else raise ArgumentError, "Got #{o.class} but expected a Hash or XML::Reader" end yield(self) if block_given? end |
Instance Method Details
#each_entry(options = {}, &block) ⇒ Object
Iterates over each entry in the feed.
Options
paginateIf true and the feed supports pagination this will fetch each page of the feed.
sinceIf a Time object is provided each_entry will iterate over all entries that were updated since that time.
userUser name for HTTP Basic Authentication.
passPassword for HTTP Basic Authentication.
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/atom.rb', line 519 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.
494 495 496 |
# File 'lib/atom.rb', line 494 def first? links.self == links.first_page end |
#last? ⇒ Boolean
Returns true if this is the last feed in a paginated set.
499 500 501 |
# File 'lib/atom.rb', line 499 def last? links.self == links.last_page end |
#reload!(opts = {}) ⇒ Object
Reloads the feed by fetching the self uri.
504 505 506 507 508 |
# File 'lib/atom.rb', line 504 def reload!(opts = {}) if links.self Feed.load_feed(URI.parse(links.self.href), opts) end end |