Permalink
Browse files

comments

  • Loading branch information...
1 parent a8b3c28 commit f84c033f3a4b53afda1155a4e649dd6adaa71c50 @git2samus git2samus committed May 18, 2012
Showing with 18 additions and 0 deletions.
  1. +3 −0 lib/planet/blog.rb
  2. +12 −0 lib/planet/parsers.rb
  3. +3 −0 lib/planet/parsers/base_parser.rb
View
3 lib/planet/blog.rb
@@ -17,12 +17,15 @@ def initialize(attributes = {})
self.posts = attributes.fetch(:posts, [])
self.planet = attributes[:planet]
+ # get parser-manager instance
@parsers = Parsers.new
end
def fetch
+ # given parser can be set arbitrarily with :type or inferred from the domain
parser = self.type ? @parsers.get_parser(self.type) : @parsers.get_parser_for(self.feed)
+ # parser instances should mimick Feedzirra interface
feed = parser.fetch_and_parse(self.feed)
self.name ||= feed.title || 'the source'
View
12 lib/planet/parsers.rb
@@ -2,13 +2,22 @@
require 'set'
class Planet
+ # 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.
class Parsers
@@parsers = Set.new
def self.add_parser(parser)
@@parsers << parser
end
+ # 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).
def initialize
@types, @domains = {}, {}
@@ -25,6 +34,7 @@ def initialize
end
end
+ # returns the appropiate parser based on the type
def get_parser(type)
begin
return @types.fetch(type)
@@ -33,6 +43,8 @@ def get_parser(type)
end
end
+ # returns any parser that can handle this feeds' domain,
+ # defaults to Feedzirra if none available.
def get_parser_for(feed)
feed_domain = URI(feed).host
View
3 lib/planet/parsers/base_parser.rb
@@ -1,5 +1,8 @@
class Planet
class Parsers
+ # base class for feed parsers
+ # subclasses should declare @type and @domains
+ # and also mimick Feedzirra interface.
class BaseParser
def self.type
@type

0 comments on commit f84c033

Please sign in to comment.