planet 0.0.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 +78 -0
  2. data/lib/planet.rb +106 -0
  3. data/lib/planet_version.rb +3 -0
  4. metadata +82 -0
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+ # 1.9 adds realpath to resolve symlinks; 1.8 doesn't
3
+ # have this method, so we add it so we get resolved symlinks
4
+ # and compatibility
5
+
6
+ unless File.respond_to? :realpath
7
+ class File #:nodoc:
8
+ def self.realpath path
9
+ return realpath(File.readlink(path)) if symlink?(path)
10
+ path
11
+ end
12
+ end
13
+ end
14
+ $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
15
+ require 'rubygems'
16
+ require 'gli'
17
+ require 'planet'
18
+ require 'ruby-debug'
19
+
20
+ include GLI
21
+
22
+ program_desc 'Describe your application here'
23
+
24
+ desc 'Parses planet.yml file for blogs and generates their posts in Jekyll compliant format under the _posts directory'
25
+ command :generate do |c|
26
+
27
+ c.action do |global_options,options,args|
28
+ conf = YAML.load File.open('planet.yml', 'r').read
29
+
30
+ @planet = Planet.new
31
+
32
+ conf['blogs'].each do |blog|
33
+ @planet.blogs << Blog.new(
34
+ blog['feed'],
35
+ blog['author'],
36
+ blog['image'],
37
+ []
38
+ )
39
+ end
40
+
41
+ @planet.aggregate
42
+
43
+ @planet.write_posts
44
+ end
45
+ end
46
+
47
+ desc 'Creates basic planet.yml file'
48
+ command :init do |c|
49
+ c.action do |global_options,options,args|
50
+ raise Exception.new('There is already a planet.yml file present') if File.exist? 'planet.yml'
51
+
52
+ default = 'blogs:
53
+ - author: "Pablo Astigarraga"
54
+ feed: "http://blog.poteland.com/atom.xml"
55
+ image: "http://poteland.com/images/avatars/red_mage.png"
56
+
57
+ - author: "Cubox"
58
+ feed: "http://blog.cuboxlabs.com/atom.xml"
59
+ image: "http://cuboxlabs.com/img/cubox-humans/could-be-you.png"'
60
+
61
+ File.open('planet.yml', 'w') { |f| f.write(default) }
62
+ puts '=> created default planet.yml'
63
+ end
64
+ end
65
+
66
+ pre do |global,command,options,args|
67
+ true
68
+ end
69
+
70
+ post do |global,command,options,args|
71
+ true
72
+ end
73
+
74
+ on_error do |exception|
75
+ true
76
+ end
77
+
78
+ exit GLI.run(ARGV)
@@ -0,0 +1,106 @@
1
+ require 'simple-rss'
2
+ require 'open-uri'
3
+
4
+ class Blog < Struct.new(:feed, :author, :image, :posts)
5
+ end
6
+
7
+ class Post < Struct.new(:title, :content, :date, :link, :blog)
8
+
9
+ def to_hash
10
+ {
11
+ title: title,
12
+ date: date.strftime('%Y-%m-%d %H:%M'),
13
+ link: link,
14
+ content: content,
15
+ author: blog.author
16
+ }
17
+ end
18
+
19
+ def header
20
+ ## TODO: We need categories/tags
21
+ "---
22
+ title: \"%{title}\"
23
+ kind: article
24
+ author: %{author}
25
+ created_at: %{date}
26
+ ---
27
+ " % self.to_hash
28
+ end
29
+
30
+ def file_name
31
+ name_date = date ? date.strftime('%Y-%m-%d') : nil
32
+ name_title = title.downcase.scan(/\w+/).join('-')
33
+
34
+ [name_date, name_title].join('-')
35
+ end
36
+ end
37
+
38
+ class Planet
39
+
40
+ def initialize
41
+ @@_posts = []
42
+ @@_blogs = []
43
+ end
44
+
45
+ def posts(options = {})
46
+ return @@_posts unless options[:filter]
47
+
48
+ filtered_posts = @@_posts
49
+
50
+ filtered_posts = case options[:filter][:date]
51
+ when true
52
+ filtered_posts.reject { |p| p.date.nil? }
53
+ when false || nil
54
+ filtered_posts.reject { |p| !p.date.nil? }
55
+ else
56
+ filtered_posts
57
+ end
58
+
59
+ filtered_posts = case options[:filter][:order]
60
+ when :date
61
+ with_date = filtered_posts.reject { |p| p.date.nil? }
62
+ without_date = filtered_posts.reject { |p| !p.date.nil? }
63
+
64
+ with_date.sort_by { |po| po.date }.reverse + without_date
65
+ else
66
+ filtered_posts
67
+ end
68
+
69
+ filtered_posts
70
+ end
71
+
72
+ def blogs
73
+ @@_blogs
74
+ end
75
+
76
+ def aggregate
77
+ @@_blogs.each do |blog|
78
+ rss = SimpleRSS.parse open(blog.feed)
79
+ rss.entries.each do |entry|
80
+ @@_posts << @post = Post.new(
81
+ entry.fetch(:title),
82
+ entry.fetch(:content).strip,
83
+ entry.fetch(:updated, nil), # Yeah, I know, Im following the default octopress value for the date parameter.
84
+ entry.fetch(:id, nil), # Er, this is the full link to the article
85
+ blog
86
+ )
87
+
88
+ blog.posts << @post
89
+ end
90
+ end
91
+ end
92
+
93
+ def write_posts
94
+ Dir.mkdir("_posts") unless File.directory?("_posts")
95
+
96
+ posts(filter: {date: true, order: :date}).each do |post|
97
+ file_name = '_posts/'.concat post.file_name
98
+
99
+ File.open(file_name + '.markdown', "w+") { |f|
100
+ f.write(post.header)
101
+ f.write(post.content)
102
+ f.close
103
+ }
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ module Planet
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: planet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pablo Astigarraga
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &2152640020 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152640020
25
+ - !ruby/object:Gem::Dependency
26
+ name: gli
27
+ requirement: &2152639180 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2152639180
36
+ - !ruby/object:Gem::Dependency
37
+ name: simple-rss
38
+ requirement: &2152637640 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2152637640
47
+ description:
48
+ email: pote@tardis.com.uy
49
+ executables:
50
+ - planet
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - bin/planet
55
+ - lib/planet_version.rb
56
+ - lib/planet.rb
57
+ homepage: http://poteland.com
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 1.8.10
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: An awesome rss/atom feed aggregator designed to work with Octopress/Jekyll
82
+ test_files: []