Class: Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/abelard/dir.rb

Instance Method Summary collapse

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_docObject



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

#eachObject

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.timestamp] = item
  end
  by_date.keys.sort.map { |dt| yield by_date[dt] }
end

#each_unsortedObject



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

#infoObject



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_feedObject



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_docObject



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

#saveObject



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 ).timestamp }
  end
  by_date.sort! { |a,b| a[:time] <=> b[:time] }
  by_date.map { |hash| hash[:entry] }
end