Class: Feedzirra::FeedParser

Inherits:
Object
  • Object
show all
Defined in:
lib/feedzirra/feed_parser.rb

Overview

Determines the correct parser for an XML feed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ FeedParser

Returns a new instance of FeedParser.



8
9
10
11
# File 'lib/feedzirra/feed_parser.rb', line 8

def initialize(xml)
  @xml    = xml
  @parser = detect_feed_processor_for_xml(@xml)
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



6
7
8
# File 'lib/feedzirra/feed_parser.rb', line 6

def parser
  @parser
end

#xmlObject (readonly)

Returns the value of attribute xml.



6
7
8
# File 'lib/feedzirra/feed_parser.rb', line 6

def xml
  @xml
end

Instance Method Details

#add_common_feed_entry_element(element_tag, options = {}) ⇒ Object

Makes all entry types look for the passed in element to parse. This is actually just a call to element (a SAXMachine call) in the class

Parameters

[element_tag] [options] Valid keys are same as with SAXMachine



57
58
59
60
61
# File 'lib/feedzirra/feed_parser.rb', line 57

def add_common_feed_entry_element(element_tag, options = {})
  feed_classes.map{|k| eval("#{k}Entry") }.each do |klass|
    klass.send(:element, element_tag, options)
  end
end

#add_feed_class(klass) ⇒ Object

Adds a new feed parsing class that will be used for parsing.

Parameters

[klass] The class/constant that you want to register.

Returns

A updated array of feed parser class names.



39
40
41
# File 'lib/feedzirra/feed_parser.rb', line 39

def add_feed_class(klass) 
  feed_classes.unshift klass
end

#detect_feed_processor_for_xml(xml) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/feedzirra/feed_parser.rb', line 13

def detect_feed_processor_for_xml(xml)
  start_of_doc = xml.slice(0, 1000)

  if_none_found = lambda do
    raise NoParserAvailable.new("No valid parser for XML.")
  end

  feed_classes.detect(if_none_found) do |klass| 
    klass.able_to_parse?(start_of_doc)
  end    
end

#feed_classesObject

Provides a list of registered feed parsing classes.

Returns

A array of class names.



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

def feed_classes
  @@feed_classes ||= [Feedzirra::Parser::RSS, Feedzirra::Parser::AtomFeedBurner, Feedzirra::Parser::Atom]
end

#runObject

Determines the correct Parser for the XML document with which we were initialized and returns results of this parser on the document. Raises NoParserAvailable if no valid parser is found.



29
30
31
# File 'lib/feedzirra/feed_parser.rb', line 29

def run
  @parser.parse(@xml)
end