Class: Planet::Parsers
- Inherits:
-
Object
- Object
- Planet::Parsers
- Defined in:
- lib/planet/parsers.rb,
lib/planet/parsers/base_parser.rb
Overview
Parsers class - manager for the feed parsers
parser classes inherit from Planet::Parsers::BaseParser and are added automatically to the list of available parsers. files located on planet/parsers are automatically loaded.
Defined Under Namespace
Classes: BaseParser
Constant Summary collapse
- @@parsers =
Set.new
Class Method Summary collapse
Instance Method Summary collapse
-
#get_parser(type) ⇒ Object
returns the appropiate parser based on the type.
-
#get_parser_for(feed) ⇒ Object
returns any parser that can handle this feeds’ domain, defaults to Feedjira if none available.
-
#initialize ⇒ Parsers
constructor
Parser instances keep indexes of the available parsers and check for duplicate definitions (need to use an instance because #inherited gets called as soon as the class is seen but before it is fully defined).
Constructor Details
#initialize ⇒ Parsers
Parser instances keep indexes of the available parsers and check for duplicate definitions (need to use an instance because #inherited gets called as soon as the class is seen but before it is fully defined).
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/planet/parsers.rb', line 21 def initialize @types, @domains = {}, {} @@parsers.each do |parser| new_type, new_domains = parser.type, parser.domains fail("duplicate type") if new_type and @types.has_key? new_type fail("overlapping domains") unless (@domains.keys & new_domains).empty? @types[new_type] = parser if new_type new_domains.each do |new_domain| @domains[new_domain] = parser end end end |
Class Method Details
.add_parser(parser) ⇒ Object
13 14 15 |
# File 'lib/planet/parsers.rb', line 13 def self.add_parser(parser) @@parsers << parser end |
Instance Method Details
#get_parser(type) ⇒ Object
returns the appropiate parser based on the type
38 39 40 41 42 43 44 |
# File 'lib/planet/parsers.rb', line 38 def get_parser(type) begin return @types.fetch(type) rescue KeyError => e raise(ArgumentError, "No parser for type '#{ type }'", caller) end end |
#get_parser_for(feed) ⇒ Object
returns any parser that can handle this feeds’ domain, defaults to Feedjira if none available.
48 49 50 51 52 53 54 55 56 |
# File 'lib/planet/parsers.rb', line 48 def get_parser_for(feed) feed_domain = URI(feed).host @domains.each do |domain, parser| return parser if feed_domain.end_with? domain end return Feedjira::Feed # default generic parser end |