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

#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.

Yields:

  • (_self)

Yield Parameters:

  • _self (Atom::Feed)

    the object that the method was called on



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/atom.rb', line 459

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

#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.

user

User name for HTTP Basic Authentication.

pass

Password for HTTP Basic Authentication.



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/atom.rb', line 507

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

#first?Boolean

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

Returns:

  • (Boolean)


482
483
484
# File 'lib/atom.rb', line 482

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

#last?Boolean

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

Returns:

  • (Boolean)


487
488
489
# File 'lib/atom.rb', line 487

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

#reload!(opts = {}) ⇒ Object

Reloads the feed by fetching the self uri.



492
493
494
495
496
# File 'lib/atom.rb', line 492

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