Module: RSpecTelemetry::Ndjson

Defined in:
lib/rspec_telemetry/ndjson.rb

Class Method Summary collapse

Class Method Details

.parse(line) ⇒ Object

Trace files are untrusted and may contain a truncated final line after a crash.



8
9
10
11
12
13
14
15
16
# File 'lib/rspec_telemetry/ndjson.rb', line 8

def self.parse(line)
  text = scrub(line).strip
  return nil if text.empty?

  value = JSON.parse(text)
  value.is_a?(Hash) ? value : nil
rescue JSON::ParserError
  nil
end

.scrub(line) ⇒ Object

Keep downstream rendering on valid UTF-8 even when the trace has bad bytes.



19
20
21
22
# File 'lib/rspec_telemetry/ndjson.rb', line 19

def self.scrub(line)
  string = line.to_s
  string.valid_encoding? ? string : string.scrub("?")
end