class Postmortem::Index

Generates and parses an index of previously-sent emails.

Public Class Methods

new(index_path, mail_path, mail) click to toggle source
# File lib/postmortem/index.rb, line 6
def initialize(index_path, mail_path, mail)
  @index_path = index_path
  @mail_path = mail_path
  @mail = mail
end

Public Instance Methods

content() click to toggle source
# File lib/postmortem/index.rb, line 12
def content
  mail_path = @mail_path
  ERB.new(File.read(template_path), nil, '-').result(binding)
end
size() click to toggle source
# File lib/postmortem/index.rb, line 17
def size
  encoded_index.size
end

Private Instance Methods

encoded_index() click to toggle source
# File lib/postmortem/index.rb, line 31
def encoded_index
  return [encoded_mail] unless @index_path.file?

  @encoded_index ||= [encoded_mail] + lines[index(:start)..index(:end)]
end
encoded_mail() click to toggle source
# File lib/postmortem/index.rb, line 37
def encoded_mail
  Base64.encode64(mail_data.to_json).split("\n").join
end
index(position) click to toggle source
# File lib/postmortem/index.rb, line 55
def index(position)
  offset = { start: 1, end: -1 }.fetch(position)
  lines.index(marker(position)) + offset
end
lines() click to toggle source
# File lib/postmortem/index.rb, line 51
def lines
  @lines ||= @index_path.read.split("\n")
end
mail_data() click to toggle source
# File lib/postmortem/index.rb, line 41
def mail_data
  {
    subject: @mail.subject || '(no subject)',
    timestamp: timestamp,
    path: @mail_path,
    id: @mail.id,
    content: @mail.serializable
  }
end
marker(position) click to toggle source
# File lib/postmortem/index.rb, line 60
def marker(position)
  "### INDEX #{position.to_s.upcase}"
end
template_path() click to toggle source
# File lib/postmortem/index.rb, line 64
def template_path
  File.expand_path(File.join(__dir__, '..', '..', 'layout', 'postmortem_index.html.erb'))
end
timestamp() click to toggle source
# File lib/postmortem/index.rb, line 27
def timestamp
  Time.now.iso8601
end
uuid() click to toggle source
# File lib/postmortem/index.rb, line 23
def uuid
  @uuid ||= SecureRandom.uuid
end