Class: Textus::AuditLog

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/audit_log.rb

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ AuditLog

Returns a new instance of AuditLog.



6
7
8
# File 'lib/textus/audit_log.rb', line 6

def initialize(root)
  @path = File.join(root, "audit.log")
end

Instance Method Details

#append(role:, verb:, key:, etag_before:, etag_after:, extras: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/textus/audit_log.rb', line 18

def append(role:, verb:, key:, etag_before:, etag_after:, extras: nil)
  fields = [
    Time.now.utc.iso8601, role, verb, key,
    etag_before || "NULL",
    etag_after  || "NULL"
  ]
  fields << JSON.generate(extras) if extras && !extras.empty?
  line = fields.join("\t") + "\n"
  File.open(@path, File::WRONLY | File::APPEND | File::CREAT, 0o644) do |f|
    f.flock(File::LOCK_EX)
    f.write(line)
  end
end

#last_writer_for(key) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/textus/audit_log.rb', line 10

def last_writer_for(key)
  return nil unless File.exist?(@path)

  File.foreach(@path).map { |l| l.chomp.split("\t") }
                     .select { |row| row[3] == key && %w[put delete].include?(row[2]) }
                     .last&.fetch(1)
end