Skip to content

Commit

Permalink
modular approach foundation 8¬{
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Cetrulo committed May 16, 2012
1 parent d58e28a commit 4830db6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/planet/blog.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
Expand All @@ -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?
Expand Down
25 changes: 25 additions & 0 deletions lib/planet/parsers.rb
Original file line number Diff line number Diff line change
@@ -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.