Permalink
Browse files

Proper scoping of classes under Planet

  • Loading branch information...
1 parent 60e9d5a commit 152ebe41a0f411a3099cdcb06a29a659cefb9eda @pote committed Dec 14, 2012
Showing with 100 additions and 111 deletions.
  1. +3 −2 bin/planet
  2. +2 −1 lib/planet.rb
  3. +3 −9 lib/planet/blog.rb
  4. +1 −5 lib/planet/importer.rb
  5. +40 −41 lib/planet/parsers.rb
  6. +51 −53 lib/planet/post.rb
View
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
+$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
require 'gli'
require 'planet'
@@ -50,12 +51,12 @@ command :generate do |c|
c.action do |global_options,options,args|
- PlanetPostImporter.import('planet.yml') do |planet|
+ Planet::Importer.import('planet.yml') do |planet|
posts_dir = planet.config.fetch('posts_directory', 'source/_posts/')
FileUtils.mkdir_p(posts_dir)
puts "=> Writing #{ planet.posts.size } posts to the #{ posts_dir } directory."
-
+
planet.posts.each do |post|
file_name = posts_dir + post.file_name
File.open(file_name + '.markdown', "w+") { |f| f.write(post.to_s) }
View
@@ -25,7 +25,7 @@ def aggregate
end
def write_posts
- PostImporter.import(self)
+ Importer.import(self)
end
private
@@ -44,6 +44,7 @@ def read_config_file(config_file_path)
twitter: blog['twitter']
)
end
+
{ planet: planet, blogs: blogs }
end
end
View
@@ -36,22 +36,16 @@ def fetch
end
feed.entries.each do |entry|
- content = if !entry.content.nil?
+ content = if entry.content
self.sanitize_images(entry.content.strip)
- elsif !entry.summary.nil?
+ elsif entry.summary
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,
+ title: entry.title.nil? ? self.name : entry.title,
content: content,
date: entry.published,
url: self.url + entry.url,
@@ -1,13 +1,9 @@
-class PlanetPostImporter
+class Planet::Importer
def self.import(config)
-
planet = Planet.new(config)
-
planet.aggregate
yield(planet)
-
end
-
end
View
@@ -1,59 +1,58 @@
require 'feedzirra'
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
+# 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.
- def self.add_parser(parser)
- @@parsers << parser
- end
+class Planet::Parsers
+ @@parsers = Set.new
- # 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 = {}, {}
+ def self.add_parser(parser)
+ @@parsers << parser
+ end
- @@parsers.each do |parser|
- new_type, new_domains = parser.type, parser.domains
+ # 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 = {}, {}
- fail("duplicate type") if new_type and @types.has_key? new_type
- fail("overlapping domains") unless (@domains.keys & new_domains).empty?
+ @@parsers.each do |parser|
+ new_type, new_domains = parser.type, parser.domains
- @types[new_type] = parser if new_type
- new_domains.each do |new_domain|
- @domains[new_domain] = parser
- end
- end
- end
+ fail("duplicate type") if new_type and @types.has_key? new_type
+ fail("overlapping domains") unless (@domains.keys & new_domains).empty?
- # returns the appropiate parser based on the type
- def get_parser(type)
- begin
- return @types.fetch(type)
- rescue KeyError => e
- raise(ArgumentError, "No parser for type '#{ type }'", caller)
+ @types[new_type] = parser if new_type
+ new_domains.each do |new_domain|
+ @domains[new_domain] = parser
end
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
+ # returns the appropiate parser based on the type
+ def get_parser(type)
+ begin
+ return @types.fetch(type)
+ rescue KeyError => e
+ raise(ArgumentError, "No parser for type '#{ type }'", caller)
+ end
+ end
- @domains.each do |domain, parser|
- return parser if feed_domain.end_with? domain
- 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
- return Feedzirra::Feed # default generic parser
+ @domains.each do |domain, parser|
+ return parser if feed_domain.end_with? domain
end
+
+ return Feedzirra::Feed # default generic parser
end
end
View
@@ -1,58 +1,56 @@
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
+class Planet::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

0 comments on commit 152ebe4

Please sign in to comment.