Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
PlanetPostImporter runs a block after initializing Planet.
I think this way is easier to move to other projects, rather than
allowing the executable to override the importer.
  • Loading branch information
Federico Bana committed Dec 13, 2012
1 parent db6ffd2 commit 1ea4d60
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
25 changes: 9 additions & 16 deletions bin/planet
Expand Up @@ -49,27 +49,20 @@ desc 'Parses planet.yml file for blogs and generates their posts in Jekyll compl
command :generate do |c|

c.action do |global_options,options,args|
conf = YAML.load_file('planet.yml')

@planet = Planet.new(
config: conf.fetch('planet', {}),
blogs: conf.fetch('blogs', [])
)
PlanetPostImporter.import('planet.yml') do |planet|

@planet.aggregate
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) }
end

# Take the first argument as the path
# for a ruby file in order to change
# the way the posts are imported.
# Use on your own risk.
unless args[0].blank?
require args[0]
puts '=> ** NOTICE **: Running custom PostImporter'
else
puts '=> Running default PostImporter'
end

@planet.write_posts
end
end

Expand Down
37 changes: 24 additions & 13 deletions lib/planet.rb
@@ -1,3 +1,4 @@
require 'yaml'
require 'planet/version'
require 'planet/blog'
require 'planet/importer'
Expand All @@ -6,19 +7,10 @@ class Planet

attr_accessor :config, :blogs

def initialize(attributes = {})
self.config = attributes[:config]
self.blogs = attributes.fetch(:blogs, []).map do |blog|
Blog.new(
feed: blog['feed'],
url: blog['url'],
author: blog['author'],
image: blog['image'],
posts: [],
planet: self,
twitter: blog['twitter']
)
end
def initialize(config_file_path)
config_file = read_config_file(config_file_path)
self.config = config_file[:planet]
self.blogs = config_file[:blogs]
end

def posts
Expand All @@ -35,4 +27,23 @@ def aggregate
def write_posts
PostImporter.import(self)
end

private

def read_config_file(config_file_path)
config = YAML.load_file(config_file_path)
planet = config.fetch('planet', {})
blogs = config.fetch('blogs', []).map do |blog|
Blog.new(
feed: blog['feed'],
url: blog['url'],
author: blog['author'],
image: blog['image'],
posts: [],
planet: self,
twitter: blog['twitter']
)
end
{ planet: planet, blogs: blogs }
end
end
16 changes: 7 additions & 9 deletions lib/planet/importer.rb
@@ -1,15 +1,13 @@
class PostImporter
class PlanetPostImporter

def self.import(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."
def self.import(config)

planet.posts.each do |post|
file_name = posts_dir + post.file_name
planet = Planet.new(config)

planet.aggregate

yield(planet)

File.open(file_name + '.markdown', "w+") { |f| f.write(post.to_s) }
end
end

end

0 comments on commit 1ea4d60

Please sign in to comment.