planet 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/planet +6 -8
  2. data/lib/planet/version.rb +1 -1
  3. data/lib/planet.rb +22 -52
  4. metadata +1 -1
data/bin/planet CHANGED
@@ -15,7 +15,7 @@ command :generate do |c|
15
15
  c.action do |global_options,options,args|
16
16
  conf = YAML.load File.open('planet.yml', 'r').read
17
17
 
18
- @planet = Planet.new(conf.fetch('planet', {}))
18
+ @planet = Planet.new(config: conf.fetch('planet', {}))
19
19
 
20
20
  conf['blogs'].each do |blog|
21
21
  @planet.blogs << Planet::Blog.new(
@@ -69,11 +69,11 @@ end
69
69
  desc 'Creates basic templates on the templates_directory specified in planet.yml'
70
70
  command :create_templates do |c|
71
71
  c.action do |global_options,options,args|
72
- conf = YAML.load File.open('planet.yml', 'r').read
72
+ conf = YAML.load File.open('planet.yml', 'r') { |f| f.read }
73
73
 
74
74
  templates_dir = conf.fetch('planet').fetch('templates_directory', '_layouts/')
75
75
 
76
- Dir.mkdir(templates_dir) unless File.exists?(templates_dir)
76
+ FileUtils.mkdir_p(templates_dir)
77
77
 
78
78
  author = '
79
79
  <div class="author">
@@ -109,12 +109,10 @@ end
109
109
 
110
110
  pre do |global,command,options,args|
111
111
  if command.name == :generate
112
- conf = YAML.load File.open('planet.yml', 'r').read
113
-
114
- templates_dir = conf.fetch('planet').fetch('templates_directory', '_layout')
112
+ conf = YAML.load File.open('planet.yml', 'r') { |f| f.read }
115
113
 
116
- ## I probably shouldn't do this, but I want the default setup to be simple and smooth.
117
- Dir.mkdir('source') if templates_dir.split('/').first == 'source' && !File.exists?('source')
114
+ templates_dir = conf.fetch('planet').fetch('templates_directory', 'source/_layouts')
115
+ FileUtils.mkdir_p(templates_dir)
118
116
 
119
117
  FILES = [templates_dir, templates_dir + 'author.html', templates_dir + 'header.md']
120
118
 
@@ -2,7 +2,7 @@ module Planet
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 3
5
- PATCH = 0
5
+ PATCH = 1
6
6
 
7
7
  def self.to_s
8
8
  [MAJOR, MINOR, PATCH].join('.')
data/lib/planet.rb CHANGED
@@ -1,80 +1,50 @@
1
1
  require 'feedzirra'
2
2
  require 'mustache'
3
3
 
4
- class Planet
4
+ class Planet < Struct.new(:config, :blogs)
5
5
 
6
- def initialize(config = {})
7
- @@_posts = []
8
- @@_blogs = []
9
-
10
- @@_config = config
11
- end
12
-
13
- def config
14
- @@_config
6
+ def initialize(attributes = {})
7
+ self.config = attributes[:config]
8
+ self.blogs = attributes.fetch(:blogs, [])
15
9
  end
16
10
 
17
- def blogs
18
- @@_blogs
19
- end
20
-
21
- def posts(options = {})
22
- return @@_posts unless options[:filter]
23
-
24
- filtered_posts = @@_posts
25
-
26
- filtered_posts = case options[:filter][:date]
27
- when true
28
- filtered_posts.reject { |p| p.date.nil? }
29
- when false || nil
30
- filtered_posts.reject { |p| !p.date.nil? }
31
- else
32
- filtered_posts
33
- end
34
-
35
- filtered_posts = case options[:filter][:order]
36
- when :date
37
- with_date = filtered_posts.reject { |p| p.date.nil? }
38
- without_date = filtered_posts.reject { |p| !p.date.nil? }
39
-
40
- with_date.sort_by { |po| po.date }.reverse + without_date
41
- else
42
- filtered_posts
43
- end
44
-
45
- filtered_posts
11
+ def posts
12
+ self.blogs.map { |b| b.posts }.flatten
46
13
  end
47
14
 
48
15
  def aggregate
49
- @@_blogs.each do |blog|
16
+ self.blogs.each do |blog|
50
17
  puts "=> Parsing #{ blog.feed }"
51
18
  feed = Feedzirra::Feed.fetch_and_parse(blog.feed)
52
19
 
53
20
  blog.name ||= feed.title || 'the source'
54
21
  blog.url ||= feed.url
55
- raise "#{ blog.author }'s blog does not have a url field on it's feed, you will need to specify it on planet.yml" if blog.url.nil?
22
+
23
+ ## This is slightly gay.
24
+ if blog.url.nil?
25
+ raise "#{ blog.author }'s blog does not have a url field on it's feed, you will need to specify it on planet.yml"
26
+ end
56
27
 
57
28
  feed.entries.each do |entry|
58
- @@_posts << @post = Post.new(
29
+ blog.posts << @post = Post.new(
59
30
  title: entry.title.sanitize,
60
- content: entry.content.strip.gsub('<img src="', "<img src=\"#{ blog.url }"),
61
- date: entry.published,
31
+ content: entry.content.strip.gsub('<img src="', "<img src=\"#{ blog.url }"), ## => I don't like this that much, move it away
32
+ date: entry.published, ## and check that it's needed post by post.
62
33
  url: blog.url + entry.url,
63
34
  blog: blog
64
35
  )
65
36
 
66
- blog.posts << @post
67
37
  puts "=> Found post titled #{ @post.title } - by #{ @post.blog.author }"
68
38
  end
69
39
  end
70
40
  end
71
41
 
72
42
  def write_posts
73
- posts_dir = @@_config.fetch('posts_directory', '_posts')
74
- Dir.mkdir(posts_dir) unless File.directory?(posts_dir)
75
- puts "=> Writing #{ posts.size } posts to the #{ posts_dir } directory"
43
+ posts_dir = self.config.fetch('posts_directory', '_posts')
44
+ FileUtils.mkdir_p(posts_dir)
45
+ puts "=> Writing #{ self.posts.size } posts to the #{ posts_dir } directory"
76
46
 
77
- posts(filter: {date: true, order: :date}).each do |post|
47
+ self.posts.each do |post|
78
48
  file_name = posts_dir + post.file_name
79
49
 
80
50
  File.open(file_name + '.markdown', "w+") { |f| f.write(post.to_s) }
@@ -109,7 +79,7 @@ class Planet
109
79
  def header
110
80
  ## TODO: We need categories/tags
111
81
  file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'header.md'
112
- file_contents = File.open(file, 'r').read
82
+ file_contents = File.open(file, 'r') { |f| f.read }
113
83
 
114
84
  Mustache.render(file_contents, self.to_hash)
115
85
  end
@@ -123,13 +93,13 @@ class Planet
123
93
 
124
94
  def footer
125
95
  file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'author.html'
126
- file_contents = File.open(file, 'r').read
96
+ file_contents = File.open(file, 'r') { |f| f.read }
127
97
 
128
98
  Mustache.render(file_contents, self.to_hash)
129
99
  end
130
100
 
131
101
  def to_s
132
- self.header + self.content + self.footer
102
+ "#{ header }#{ content }#{ footer }"
133
103
  end
134
104
  end
135
105
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: planet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: