class AtomFeed::AtomFeed
AtomFeed::AtomFeed
is the central place for working with feeds in the Atom format.
Opening an atom feed from the network or a file system is done like this:
feed = AtomFeed::AtomFeed.open(open("http://example.com/atom.xml"))
If you have a file you should do:
f = File.open("feed.xml") feed = AtomFeed::AtomFeed.open(f) f.close
If you have an XML string you can do:
feed = AtomFeed::AtomFeed.open("<feed ...")
One can open and parse the feed like so:
AtomFeed::AtomFeed.open(...) do |feed| puts feed.title feed.entries do |entry| puts entry.title end end
You can access OpenSearch
extensions by using AtomFeed.open_search
. Access to other embedded XML types are available by using AtomFeed.doc
+ directly. It's a Nokogiri::XML
instance.
AtomFeed
uses Nokogiri for parsing.
Attributes
Public Class Methods
# File lib/atom_feed/atom_feed.rb, line 51 def initialize(doc) @doc = doc end
# File lib/atom_feed/atom_feed.rb, line 55 def self.open(string_or_io, url = nil, encoding = nil) doc = Nokogiri::XML(string_or_io, url, encoding) feed = AtomFeed.new(doc) yield feed if block_given? feed end
Public Instance Methods
Array of feed categories (optional).
# File lib/atom_feed/atom_feed.rb, line 96 def categories nodes = @doc.xpath("atom:feed/atom:category", ::AtomFeed::NS) || [] nodes.map { |node| AtomCategory.new(node) } end
Array of contributors (optional).
# File lib/atom_feed/atom_feed.rb, line 102 def contributors nodes = @doc.xpath("atom:feed/atom:contributor", ::AtomFeed::NS) || [] nodes.map { |node| AtomPerson.new(node) } end
Array of feed entries (optional).
# File lib/atom_feed/atom_feed.rb, line 90 def entries nodes = @doc.xpath("atom:feed/atom:entry", ::AtomFeed::NS) || [] nodes.map { |node| AtomFeedEntry.new(node) } end
Generator (optional).
# File lib/atom_feed/atom_feed.rb, line 108 def generator node = @doc.at_xpath("atom:feed/atom:generator", ::AtomFeed::NS) return nil unless node AtomGenerator.new(node) end
Icon (optional).
# File lib/atom_feed/atom_feed.rb, line 115 def icon @doc.at_xpath("atom:feed/atom:icon", ::AtomFeed::NS).try(:content) end
Feed id (required).
# File lib/atom_feed/atom_feed.rb, line 63 def id @doc.at_xpath("atom:feed/atom:id", ::AtomFeed::NS).content end
Array of links (optional).
# File lib/atom_feed/atom_feed.rb, line 84 def links nodes = @doc.xpath("atom:feed/atom:link", ::AtomFeed::NS) || [] nodes.map { |node| AtomLink.new(node) } end
Logo (optional).
# File lib/atom_feed/atom_feed.rb, line 120 def logo @doc.at_xpath("atom:feed/atom:logo", ::AtomFeed::NS).try(:content) end
Open Search extensions (optional)
# File lib/atom_feed/atom_feed.rb, line 139 def open_search @open_search ||= OpenSearch.new(@doc) end
rights (optional)
# File lib/atom_feed/atom_feed.rb, line 125 def rights node = @doc.at_xpath("atom:feed/atom:rights", ::AtomFeed::NS) return nil unless node AtomText.new(node) end
subtitle (optional)
# File lib/atom_feed/atom_feed.rb, line 132 def subtitle node = @doc.at_xpath("atom:feed/atom:subtitle", ::AtomFeed::NS) return nil unless node AtomText.new(node) end
Feed title (required).
# File lib/atom_feed/atom_feed.rb, line 68 def title @doc.at_xpath("atom:feed/atom:title", ::AtomFeed::NS).content end
Feed update date (required).
# File lib/atom_feed/atom_feed.rb, line 73 def updated Time.parse @doc.at_xpath("atom:feed/atom:updated", ::AtomFeed::NS).content end