Class: Atom::Feed

Inherits:
Object
  • Object
show all
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.

categories

A list of Atom:Category objects for the feed.

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

#simple_extensions

Instance Method Summary (collapse)

Methods included from SimpleExtensions

#[]

Methods included from Xml::Parseable

#==, #accessor_name, #current_node_is?, included, #next_node_is?, #parse, #to_xml

Constructor Details

- (Feed) initialize(o = {}) {|_self| ... }

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.

Yields:

  • (_self)

Yield Parameters:

  • _self (Atom::Feed)

    the object that the method was called on



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/atom.rb', line 435

def initialize(o = {})
  @links, @entries, @authors, @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

- (Object) each_entry(options = {}, &block)

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.

user

User name for HTTP Basic Authentication.

pass

Password for HTTP Basic Authentication.



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/atom.rb', line 483

def each_entry(options = {}, &block)
  if options[:paginate]
    since_reached = false
    feed = self
    loop do          
      feed.entries.each do |entry|
        if options[:since] && entry.updated && options[: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(options)
      end
    end
  else
    self.entries.each(&block)
  end
end

- (Boolean) first?

Return true if this is the first feed in a paginated set.

Returns:

  • (Boolean)


458
459
460
# File 'lib/atom.rb', line 458

def first?
  links.self == links.first_page
end

- (Boolean) last?

Returns true if this is the last feed in a paginated set.

Returns:

  • (Boolean)


463
464
465
# File 'lib/atom.rb', line 463

def last?
  links.self == links.last_page
end

- (Object) reload!(opts = {})

Reloads the feed by fetching the self uri.



468
469
470
471
472
# File 'lib/atom.rb', line 468

def reload!(opts = {})
  if links.self
    Feed.load_feed(URI.parse(links.self.href), opts)
  end
end