Class: Faun::ForumThread

Inherits:
Section show all
Defined in:
lib/faun.rb

Instance Attribute Summary

Attributes inherited from Section

#id, #items, #name

Instance Method Summary collapse

Methods inherited from Section

#each, #each_key, #each_value

Constructor Details

#initialize(id, name, path) ⇒ ForumThread

Returns a new instance of ForumThread.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/faun.rb', line 183

def initialize(id, name, path)
  @id = id
  @name = name

  comments = {}
  Dir.each_child(path) do |filename|
    file = File.join(path, filename)
    next if File.directory?(file) or filename.start_with?('.') or not filename.end_with?('.md')

    id, author, date_string = filename.scan(/^(\d{3})\.(\w+)\.(20\d\d-\d\d-\d\d\.\d\d-\d\d)\.md$/).first
    id = id.to_i
    datetime = DateTime.strptime(date_string, "%Y-%m-%d.%H-%M")

    content = nil
    Async do
      begin
        File.open(file, "r:UTF-8") do |file|
          generic = Async::IO::Stream.new(file)
          content = generic.read.force_encoding("UTF-8")
        end
      rescue
        content = ""
      end
    end.wait

    comments[id] = Comment.new(id, author, datetime, content)
  end
  @items = comments.sort_by { |id, _| id }.to_h
end

Instance Method Details

#authorsObject



221
222
223
# File 'lib/faun.rb', line 221

def authors
  @items.values.map(&:author).uniq
end

#commentsObject



217
218
219
# File 'lib/faun.rb', line 217

def comments
  @items
end

#item_nameObject



225
226
227
# File 'lib/faun.rb', line 225

def item_name
  "comments"
end

#titleObject



213
214
215
# File 'lib/faun.rb', line 213

def title
  @name
end

#to_json(*args) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/faun.rb', line 229

def to_json(*args)
  {
    :id => id,
    :title => @name,
    :comments => @items
  }.to_json
end