Class: Splunk::AtomFeed

Inherits:
Object
  • Object
show all
Defined in:
lib/splunk-sdk-ruby/atomfeed.rb

Overview

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..is_a?(Hash) == true
feed.entries.is_a?(Array) == true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text_or_stream) ⇒ AtomFeed

Returns a new instance of AtomFeed.

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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

Instance Attribute Details

#entriesObject (readonly)

The entries in the feed.

Returns: an Array containing Hashes that represent each entry in the feed.



128
129
130
# File 'lib/splunk-sdk-ruby/atomfeed.rb', line 128

def entries
  @entries
end

#metadataObject (readonly)

The header fields of the feed.

Typically this has keys such as “author”, “title”, and “totalResults”.

Returns: a Hash with Strings as keys.



121
122
123
# File 'lib/splunk-sdk-ruby/atomfeed.rb', line 121

def 
  @metadata
end