Class: Planet::Parsers

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeParsers

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).



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/planet/parsers.rb', line 22

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



14
15
16
# File 'lib/planet/parsers.rb', line 14

def self.add_parser(parser)
  @@parsers << parser
end

Instance Method Details

#get_parser(type) ⇒ Object

returns the appropiate parser based on the type



39
40
41
42
43
44
45
# File 'lib/planet/parsers.rb', line 39

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.



49
50
51
52
53
54
55
56
57
# File 'lib/planet/parsers.rb', line 49

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