Class: FeedMe::FeedParser

Inherits:
AbstractParser show all
Defined in:
lib/feed_me/feed_parser.rb

Direct Known Subclasses

AtomFeedParser, Rss1FeedParser, Rss2FeedParser

Instance Attribute Summary

Attributes inherited from AbstractParser

#feed, #xml

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractParser

has_many, has_one, properties, property, #to_hash

Constructor Details

#initialize(xml) ⇒ FeedParser

class << self



47
48
49
# File 'lib/feed_me/feed_parser.rb', line 47

def initialize(xml)
  @xml = xml
end

Class Method Details

.inherited(subclass) ⇒ Object



16
17
18
19
# File 'lib/feed_me/feed_parser.rb', line 16

def inherited(subclass)
  super
  parsers << subclass
end

.open(file) ⇒ Object



21
22
23
# File 'lib/feed_me/feed_parser.rb', line 21

def open(file)
  self.parse(Kernel.open(file).read)
end

.parse(feed) ⇒ Object

parses the passed feed and identifeis what kind of feed it is then returns a parser object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/feed_me/feed_parser.rb', line 27

def parse(feed)
  document = Nokogiri::XML(feed)
  if root = document.root
    root.add_namespace_definition('atom', 'http://www.w3.org/2005/Atom')
    root.add_namespace_definition('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
    root.add_namespace_definition('rss1', 'http://purl.org/rss/1.0/')
    root.add_namespace_definition('dc', 'http://purl.org/dc/elements/1.1/')
    parsers.each do |parser|
      node = root.xpath(parser.root_node).first
      if node
        return parser.new(node)
      end
    end
  end
  # if we cannot find a parser, raise an error.
  raise InvalidFeedFormat
end

.parsersObject



12
13
14
# File 'lib/feed_me/feed_parser.rb', line 12

def parsers
  @parsers ||= []
end

.root_node(node = nil) ⇒ Object



7
8
9
10
# File 'lib/feed_me/feed_parser.rb', line 7

def root_node(node=nil)
  @root_node = node if node
  @root_node
end