Reads an Atom XML feed into a Ruby object.
AtomFeed.new
accepts either a string or any object with a
read
method. It parses that as an Atom feed and exposes two
read-only fields, metadata
and entries
. The
metadata
field is a hash of all the header fields of the
feed. The entries
field is a list of hashes giving the details
of each entry in the feed.
Example:
file = File.open("some_feed.xml") feed = AtomFeed.new(file) # or AtomFeed.new(file.read()) # or AtomFeed.new(file, xml_library=:rexml) feed.metadata.is_a?(Hash) == true feed.entries.is_a?(Array) == true
The entries in the feed.
Returns: an Array
containing Hashes
that
represent each entry in the feed.
The header fields of the feed.
Typically this has keys such as “author
”,
“title
”, and “totalResults
”.
Returns: a Hash
with Strings
as keys.
# File lib/splunk-sdk-ruby/atomfeed.rb, line 69 def initialize(text_or_stream) if text_or_stream.respond_to?(:read) text = text_or_stream.read() else text = text_or_stream end # Sanity checks raise ArgumentError, 'text is nil' if text.nil? text = text.strip raise ArgumentError, 'text size is 0' if text.size == 0 if $splunk_xml_library == :nokogiri doc = Nokogiri::XML(text) else doc = REXML::Document.new(text) end # Skip down to the content of the Atom feed. Most of Splunk's # endpoints return a feed of the form # # <feed> # ...metadata... # <entry>...details of entry...</entry> # <entry>...details of entry...</entry> # <entry>...details of entry...</entry> # ... # </feed> # # with the exception of fetching a single job entity from Splunk 4.3, # where it returns # # <entry>...details of entry...</entry> # # To handle both, we have to check whether <feed> is there # before skipping. if doc.root.name == "feed" @metadata, @entries = read_feed(doc.root) elsif doc.root.name == "entry" @metadata = {} @entries = [read_entry(doc.root)] else raise ArgumentError, 'root element of Atom must be feed or entry' end end