Class: Bullematic::EvidenceStore
- Inherits:
-
Object
- Object
- Bullematic::EvidenceStore
- Defined in:
- lib/bullematic/evidence_store.rb,
sig/generated/bullematic/evidence_store.rbs
Constant Summary collapse
- ENV_KEY =
"BULLEMATIC_RECORD_FILE"- DEFAULT_PATH =
".bullematic/evidence.jsonl"
Class Method Summary collapse
- .append(detection, filepath = path) ⇒ void
- .clear(filepath = path) ⇒ void
-
.path ⇒ String
: () -> String.
- .read(filepath = path) ⇒ Array[Detection]
-
.recording? ⇒ Boolean
: () -> bool.
Class Method Details
.append(detection, filepath = path) ⇒ void
This method returns an undefined value.
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.
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 |
.path ⇒ String
: () -> String
19 20 21 |
# File 'lib/bullematic/evidence_store.rb', line 19 def path File.(ENV.fetch(ENV_KEY, DEFAULT_PATH)) end |
.read(filepath = path) ⇒ Array[Detection]
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.}" end |
.recording? ⇒ Boolean
: () -> bool
14 15 16 |
# File 'lib/bullematic/evidence_store.rb', line 14 def recording? !ENV[ENV_KEY].to_s.empty? end |