Permalink
Browse files

modular approach foundation 8¬{

  • Loading branch information...
git2samus committed May 15, 2012
1 parent d58e28a commit 4830db6b47f8fbefba58141006262ca6d7de1161
Showing with 31 additions and 6 deletions.
  1. +6 −6 lib/planet/blog.rb
  2. +25 −0 lib/planet/parsers.rb
View
@@ -1,14 +1,15 @@
-require 'feedzirra'
require 'planet/post'
+require 'planet/parsers'
class Planet
class Blog
- attr_accessor :url, :feed, :name, :author, :image, :twitter, :posts, :planet
+ attr_accessor :url, :feed, :type, :name, :author, :image, :twitter, :posts, :planet
def initialize(attributes = {})
self.url = attributes[:url]
self.feed = attributes[:feed]
+ self.type = attributes[:type]
self.name = attributes[:name]
self.author = attributes[:author]
self.image = attributes[:image]
@@ -18,7 +19,9 @@ def initialize(attributes = {})
end
def fetch
- feed = Feedzirra::Feed.fetch_and_parse(self.feed)
+ parser = self.type ? Parsers.get_parser(self.type) : Parsers.get_parser_for(self.feed)
+
+ feed = parser.fetch_and_parse(self.feed)
self.name ||= feed.title || 'the source'
self.url ||= feed.url
@@ -28,9 +31,6 @@ def fetch
end
feed.entries.each do |entry|
- ## TODO: I should probably consider using feed 'adapters' for specific
- ## blog engine feeds that don't have their stuff on the standard fields.
- ## Example: blogspot has the content on "summary" instead of content ¬¬.
content = if !entry.content.nil?
self.sanitize_images(entry.content.strip)
elsif !entry.summary.nil?
View
@@ -0,0 +1,25 @@
+require 'feedzirra'
+
+class Planet
+ class Parsers
+ @@parsers = []
+
+ def self.get_parser(type)
+ @@parsers.each do |parser|
+ return parser if parser.type == type
+ end
+
+ raise ArgumentError, "no parser for type '#{ type }'", caller
+ end
+
+ def self.get_parser_for(feed)
+ feed_domain = URI(feed).host
+
+ @@parsers.each do |parser|
+ return parser if parser.domains.any? { |domain| feed_domain.end_with? domain }
+ end
+
+ return Feedzirra::Feed
+ end
+ end
+end

0 comments on commit 4830db6

Please sign in to comment.