Permalink
Browse files

split Planet classes into separate files

  • Loading branch information...
1 parent 85db784 commit 442487e3cbecf4e551c68ecaebd566fe06764d51 @git2samus git2samus committed with git2samus May 15, 2012
Showing with 134 additions and 132 deletions.
  1. +1 −127 lib/planet.rb
  2. +74 −0 lib/planet/blog.rb
  3. +58 −0 lib/planet/post.rb
  4. +1 −5 planet.gemspec
View
@@ -1,6 +1,5 @@
-require 'feedzirra'
-require 'mustache'
require 'planet/version'
+require 'planet/blog'
class Planet
@@ -33,129 +32,4 @@ def write_posts
File.open(file_name + '.markdown', "w+") { |f| f.write(post.to_s) }
end
end
-
- class Post
-
- attr_accessor :title, :content, :date, :url, :blog
-
- def initialize(attributes = {})
- self.title = attributes[:title]
- self.content = attributes[:content]
- self.date = attributes[:date]
- self.url = attributes[:url]
- self.blog = attributes[:blog]
- end
-
- def to_s
- "#{ header }#{ content }#{ footer }"
- end
-
- def to_hash
- {
- post_content: self.content,
- post_title: self.title,
- post_date: self.date,
- image_url: self.blog.image,
- author: self.blog.author,
- blog_url: self.blog.url,
- blog_name: self.blog.name,
- post_url: self.url,
- twitter: self.blog.twitter,
- twitter_url: "http://twitter.com/#{ self.blog.twitter }"
- }
- end
-
- def header
- ## TODO: We need categories/tags
- file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'header.md'
- file_contents = File.read(file)
-
- Mustache.render(file_contents, self.to_hash)
- end
-
- def footer
- file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'author.html'
- file_contents = File.read(file)
-
- Mustache.render(file_contents, self.to_hash)
- end
-
- def file_name
- name_date = date ? date.strftime('%Y-%m-%d') : nil
- name_title = title.downcase.scan(/\w+/).join('-')
-
- [name_date, name_title].join('-')
- end
-
- end
-
- class Blog
-
- attr_accessor :url, :feed, :name, :author, :image, :twitter, :posts, :planet
-
- def initialize(attributes = {})
- self.url = attributes[:url]
- self.feed = attributes[:feed]
- self.name = attributes[:name]
- self.author = attributes[:author]
- self.image = attributes[:image]
- self.twitter = attributes[:twitter]
- self.posts = attributes.fetch(:posts, [])
- self.planet = attributes[:planet]
- end
-
- def fetch
- feed = Feedzirra::Feed.fetch_and_parse(self.feed)
-
- self.name ||= feed.title || 'the source'
- self.url ||= feed.url
-
- if self.url.nil?
- abort "#{ self.author }'s blog does not have a url field on it's feed, you will need to specify it on planet.yml"
- 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?
- self.sanitize_images(entry.summary.strip)
- else
- abort "=> No content found on entry"
- end
-
- title = if !entry.title.nil?
- entry.title.sanitize
- else
- self.name
- end
-
- self.posts << @post = Post.new(
- title: title,
- content: content,
- date: entry.published,
- url: self.url + entry.url,
- blog: self
- )
-
- puts "=> Found post titled #{ @post.title } - by #{ @post.blog.author }"
- end
- end
-
- def sanitize_images(html)
- ## We take all images with src not matching http refs and append
- ## the original blog to them.
- html.scan(/<img src="([^h"]+)"/).flatten.each do |img|
- if img[0] == '/'
- html.gsub!(img, "#{ self.url }#{ img }")
- else
- html.gsub!(img, "#{ self.url }/#{ img }")
- end
- end
-
- html
- end
- end
end
View
@@ -0,0 +1,74 @@
+require 'feedzirra'
+require 'planet/post'
+
+class Planet
+ class Blog
+
+ attr_accessor :url, :feed, :name, :author, :image, :twitter, :posts, :planet
+
+ def initialize(attributes = {})
+ self.url = attributes[:url]
+ self.feed = attributes[:feed]
+ self.name = attributes[:name]
+ self.author = attributes[:author]
+ self.image = attributes[:image]
+ self.twitter = attributes[:twitter]
+ self.posts = attributes.fetch(:posts, [])
+ self.planet = attributes[:planet]
+ end
+
+ def fetch
+ feed = Feedzirra::Feed.fetch_and_parse(self.feed)
+
+ self.name ||= feed.title || 'the source'
+ self.url ||= feed.url
+
+ if self.url.nil?
+ abort "#{ self.author }'s blog does not have a url field on it's feed, you will need to specify it on planet.yml"
+ 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?
+ self.sanitize_images(entry.summary.strip)
+ else
+ abort "=> No content found on entry"
+ end
+
+ title = if !entry.title.nil?
+ entry.title.sanitize
+ else
+ self.name
+ end
+
+ self.posts << @post = Post.new(
+ title: title,
+ content: content,
+ date: entry.published,
+ url: self.url + entry.url,
+ blog: self
+ )
+
+ puts "=> Found post titled #{ @post.title } - by #{ @post.blog.author }"
+ end
+ end
+
+ def sanitize_images(html)
+ ## We take all images with src not matching http refs and append
+ ## the original blog to them.
+ html.scan(/<img src="([^h"]+)"/).flatten.each do |img|
+ if img[0] == '/'
+ html.gsub!(img, "#{ self.url }#{ img }")
+ else
+ html.gsub!(img, "#{ self.url }/#{ img }")
+ end
+ end
+
+ html
+ end
+ end
+end
View
@@ -0,0 +1,58 @@
+require 'mustache'
+
+class Planet
+ class Post
+
+ attr_accessor :title, :content, :date, :url, :blog
+
+ def initialize(attributes = {})
+ self.title = attributes[:title]
+ self.content = attributes[:content]
+ self.date = attributes[:date]
+ self.url = attributes[:url]
+ self.blog = attributes[:blog]
+ end
+
+ def to_s
+ "#{ header }#{ content }#{ footer }"
+ end
+
+ def to_hash
+ {
+ post_content: self.content,
+ post_title: self.title,
+ post_date: self.date,
+ image_url: self.blog.image,
+ author: self.blog.author,
+ blog_url: self.blog.url,
+ blog_name: self.blog.name,
+ post_url: self.url,
+ twitter: self.blog.twitter,
+ twitter_url: "http://twitter.com/#{ self.blog.twitter }"
+ }
+ end
+
+ def header
+ ## TODO: We need categories/tags
+ file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'header.md'
+ file_contents = File.read(file)
+
+ Mustache.render(file_contents, self.to_hash)
+ end
+
+ def footer
+ file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'author.html'
+ file_contents = File.read(file)
+
+ Mustache.render(file_contents, self.to_hash)
+ end
+
+ def file_name
+ name_date = date ? date.strftime('%Y-%m-%d') : nil
+ name_title = title.downcase.scan(/\w+/).join('-')
+
+ [name_date, name_title].join('-')
+ end
+
+ end
+end
View
@@ -8,11 +8,7 @@ spec = Gem::Specification.new do |s|
s.homepage = 'http://poteland.com'
s.platform = Gem::Platform::RUBY
s.summary = 'An awesome rss/atom feed aggregator designed to work with Octopress/Jekyll'
- s.files = %w(
-bin/planet
-lib/planet/version.rb
-lib/planet.rb
- )
+ s.files = Dir['bin/*'] + Dir['lib/**/*.rb']
s.require_paths << 'lib'
s.has_rdoc = false
s.bindir = 'bin'

0 comments on commit 442487e

Please sign in to comment.