class Structured < Null
- Inherits from
Null: #buffered, #append, #indent, #outdent, #indented, #write, #puts, #pass_prefix, #fail_prefix, #assert, #skip_prefix, #error_prefix, #error, #inform_prefix
Represents a structured JSON output handler for machine-readable output.
Definitions
def self.buffered(...)
Create a buffered structured output handler.
Signature
-
parameter
ioIO The IO object to write to.
-
parameter
identityIdentity, nil Optional identity.
-
returns
Buffered A new Buffered instance wrapping a Structured handler.
Implementation
def self.buffered(...)
Buffered.new(self.new(...))
end
def initialize(io, identity = nil)
Initialize a new Structured output handler.
Signature
-
parameter
ioIO The IO object to write to.
-
parameter
identityIdentity, nil Optional identity.
Implementation
def initialize(io, identity = nil)
@io = io
@identity = identity
end
def skip(reason, identity)
Output a skip message as JSON.
Signature
-
parameter
reasonString The reason for skipping.
-
parameter
identityIdentity, nil The identity where the skip occurred.
Implementation
def skip(reason, identity)
inform(reason.to_s, identity)
end
def inform(message, identity)
Output an informational message as JSON.
Signature
-
parameter
messageString, Object The message to output.
-
parameter
identityIdentity, nil The identity where the message was generated.
Implementation
def inform(message, identity)
unless message.is_a?(String)
message = message.inspect
end
@io.puts(JSON.generate({
inform: @identity,
message: {
text: message,
location: identity&.to_location,
}
}))
@io.flush
end