Class: FeedMe::FeedParser

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

Direct Known Subclasses

AtomFeedParser, Rss2FeedParser

Instance Attribute Summary collapse

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, encoding) ⇒ FeedParser

class << self



44
45
46
47
# File 'lib/feed_me/feed_parser.rb', line 44

def initialize(xml, encoding)
  @xml = xml
  @encoding = encoding
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



49
50
51
# File 'lib/feed_me/feed_parser.rb', line 49

def encoding
  @encoding
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
# 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')
    parsers.each do |parser|
      node = root.xpath(parser.root_node).first
      if node
        return parser.new(node, document.encoding)
      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