Class: Bullematic::EvidenceStore

Inherits:
Object
  • Object
show all
Defined in:
lib/bullematic/evidence_store.rb,
sig/generated/bullematic/evidence_store.rbs

Constant Summary collapse

ENV_KEY =

Returns:

  • (::String)
"BULLEMATIC_RECORD_FILE"
DEFAULT_PATH =

Returns:

  • (::String)
".bullematic/evidence.jsonl"

Class Method Summary collapse

Class Method Details

.append(detection, filepath = path) ⇒ void

This method returns an undefined value.

RBS:

  • detection: Detection

  • filepath: String

  • return: void

Parameters:

  • detection (Detection)
  • filepath (String) (defaults to: path)


26
27
28
29
30
31
32
33
# File 'lib/bullematic/evidence_store.rb', line 26

def append(detection, filepath = path)
  FileUtils.mkdir_p(File.dirname(filepath))
  reject_symlink!(filepath)
  open_evidence(filepath, File::WRONLY | File::CREAT | File::APPEND) do |file|
    file.flock(File::LOCK_EX)
    file.puts(JSON.generate(detection.to_h))
  end
end

.clear(filepath = path) ⇒ void

This method returns an undefined value.

RBS:

  • filepath: String

  • return: void

Parameters:

  • filepath (String) (defaults to: path)


37
38
39
40
41
# File 'lib/bullematic/evidence_store.rb', line 37

def clear(filepath = path)
  FileUtils.mkdir_p(File.dirname(filepath))
  reject_symlink!(filepath)
  open_evidence(filepath, File::WRONLY | File::CREAT | File::TRUNC) {}
end

.pathString

: () -> String

Returns:

  • (String)


19
20
21
# File 'lib/bullematic/evidence_store.rb', line 19

def path
  File.expand_path(ENV.fetch(ENV_KEY, DEFAULT_PATH))
end

.read(filepath = path) ⇒ Array[Detection]

RBS:

  • filepath: String

  • return: Array[Detection]

Parameters:

  • filepath (String) (defaults to: path)

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bullematic/evidence_store.rb', line 45

def read(filepath = path)
  reject_symlink!(filepath)
  return [] unless File.file?(filepath)

  detections = open_evidence(filepath, File::RDONLY) do |file|
    file.each_line.filter_map do |line|
      next if line.strip.empty?

      Detection.from_h(JSON.parse(line))
    end
  end
  detections.uniq(&:fingerprint)
rescue JSON::ParserError, KeyError, ArgumentError, TypeError => error
  raise Error, "invalid evidence file #{filepath}: #{error.message}"
end

.recording?Boolean

: () -> bool

Returns:

  • (Boolean)


14
15
16
# File 'lib/bullematic/evidence_store.rb', line 14

def recording?
  !ENV[ENV_KEY].to_s.empty?
end