Class: FeedNormalizer::FeedNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/feed-normalizer.rb

Class Method Summary collapse

Class Method Details

.parse(xml, forced_parser = nil, try_others = false) ⇒ Object

Parses the given xml and attempts to return a normalized Feed object. Setting forced parser to a suitable parser will mean that parser is used first, and if try_others is false, it is the only parser used, otherwise all parsers in the ParserRegistry are attempted next, in order of priority.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/feed-normalizer.rb', line 91

def self.parse(xml, forced_parser=nil, try_others=false)

  # Get a string ASAP, as multiple read()'s will start returning nil..
  xml = xml.respond_to?(:read) ? xml.read : xml.to_s

  if forced_parser
    result = forced_parser.parse(xml)

    if result
      return result
    elsif !try_others
      return nil
    else
      # fall through and continue with other parsers
    end
  end

  ParserRegistry.parsers.each do |parser|
    result = parser.parse(xml)
    return result if result
  end

  # if we got here, no parsers worked.
  return nil
end