Class: Riffer::StructuredOutput::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/structured_output/result.rb

Overview

Wraps the result of structured output parsing and validation.

On success, object contains the validated Hash and error is nil. On failure, error contains the error message and object is nil.

result = structured_output.parse_and_validate(json_string)
if result.success?
  result.object  #=> {sentiment: "positive", score: 0.9}
else
  result.error   #=> "JSON parse error: ..."
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object: nil, error: nil) ⇒ Result

– : (?object: Hash[Symbol, untyped]?, ?error: String?) -> void



22
23
24
25
# File 'lib/riffer/structured_output/result.rb', line 22

def initialize(object: nil, error: nil)
  @object = object
  @error = error
end

Instance Attribute Details

#errorObject (readonly)

: String?



18
19
20
# File 'lib/riffer/structured_output/result.rb', line 18

def error
  @error
end

#objectObject (readonly)

: Hash[Symbol, untyped]?



17
18
19
# File 'lib/riffer/structured_output/result.rb', line 17

def object
  @object
end

Instance Method Details

#failure?Boolean

Returns true when parsing or validation failed.

– : () -> bool

Returns:



37
# File 'lib/riffer/structured_output/result.rb', line 37

def failure? = !success?

#success?Boolean

Returns true when parsing and validation succeeded.

– : () -> bool

Returns:



31
# File 'lib/riffer/structured_output/result.rb', line 31

def success? = @error.nil?