Skip to content
Browse files

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...
1 parent db6ffd2 commit 1ea4d6015fed8946b1fa8a3399106939adb85a47 Federico Bana committed
Showing with 40 additions and 38 deletions.
  1. +9 −16 bin/planet
  2. +24 −13 lib/planet.rb
  3. +7 −9 lib/planet/importer.rb
View
25 bin/planet
@@ -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
View
37 lib/planet.rb
@@ -1,3 +1,4 @@
+require 'yaml'
require 'planet/version'
require 'planet/blog'
require 'planet/importer'
@@ -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
@@ -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
View
16 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.
Something went wrong with that request. Please try again.