Class: Directory
- Inherits:
-
Object
- Object
- Directory
- Defined in:
- lib/abelard/dir.rb
Instance Method Summary collapse
- #base_doc ⇒ Object
-
#each ⇒ Object
iterates the Item objects for the feed, in order.
- #each_unsorted ⇒ Object
- #info ⇒ Object
-
#initialize(path) ⇒ Directory
constructor
A new instance of Directory.
- #insert_posts(collection) ⇒ Object
- #posts_feed ⇒ Object
- #posts_feed_atom(doc) ⇒ Object
- #posts_feed_rss(rssdoc) ⇒ Object
- #read_base_doc ⇒ Object
- #save ⇒ Object
- #sort_entries(repo_entries) ⇒ Object
Constructor Details
#initialize(path) ⇒ Directory
Returns a new instance of Directory.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/abelard/dir.rb', line 68 def initialize(path) @path = path @base_doc = read_base_doc @feed_type = case @base_doc.root.name when "feed" :atom when "rss" :rss else :unknown end @git = History.new(self, path) end |
Instance Method Details
#base_doc ⇒ Object
96 97 98 99 100 101 |
# File 'lib/abelard/dir.rb', line 96 def base_doc if ! @base_doc @base_doc = read_base_doc end @base_doc end |
#each ⇒ Object
iterates the Item objects for the feed, in order
104 105 106 107 108 109 110 111 |
# File 'lib/abelard/dir.rb', line 104 def each by_date = {} each_unsorted do |post,filename| item = Item.new(post,filename) by_date[item.] = item end by_date.keys.sort.map { |dt| yield by_date[dt] } end |
#each_unsorted ⇒ Object
139 140 141 142 143 144 |
# File 'lib/abelard/dir.rb', line 139 def each_unsorted Dir.glob("#{@path}/post-*.xml") do |filename| post = LibXML::XML::Parser.file(filename).parse yield post, filename end end |
#info ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/abelard/dir.rb', line 113 def info inf = {} el = base_doc.find_first("/atom:feed/atom:title", NS) || base_doc.find_first("/rss/channel/title") inf["title"] = el.content inf end |
#insert_posts(collection) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/abelard/dir.rb', line 131 def insert_posts(collection) each do |post| $stderr.puts "adding #{post.file}" collection << collection.doc.import(post.doc.root) end collection end |
#posts_feed ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/abelard/dir.rb', line 121 def posts_feed feed = read_base_doc case @feed_type when :atom posts_feed_atom(feed) when :rss posts_feed_rss(feed) end end |
#posts_feed_atom(doc) ⇒ Object
146 147 148 149 |
# File 'lib/abelard/dir.rb', line 146 def posts_feed_atom(doc) insert_posts(doc.root) doc end |
#posts_feed_rss(rssdoc) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/abelard/dir.rb', line 151 def posts_feed_rss(rssdoc) doc = LibXML::XML::Parser.file("#{@path}/channel-1.xml").parse channel = doc.find_first("/rss/channel"); insert_posts(channel) doc end |
#read_base_doc ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/abelard/dir.rb', line 87 def read_base_doc feed = LibXML::XML::Parser.file("#{@path}/feed.xml").parse if feed.root.name == "rss" LibXML::XML::Parser.file("#{@path}/channel-1.xml").parse else feed end end |
#save ⇒ Object
83 84 85 |
# File 'lib/abelard/dir.rb', line 83 def save @git.commit_posts end |
#sort_entries(repo_entries) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/abelard/dir.rb', line 158 def sort_entries(repo_entries) by_date = repo_entries.map do |e| { :entry => e, :time => Item.new(LibXML::XML::Parser.file(e.path).parse, e.path ). } end by_date.sort! { |a,b| a[:time] <=> b[:time] } by_date.map { |hash| hash[:entry] } end |