Class: Phronomy::OutputParser::StructuredParser
- Defined in:
- lib/phronomy/output_parser/structured_parser.rb
Overview
Parses JSON and maps it to an instance of the given schema class.
Instance Method Summary collapse
-
#initialize(schema_class) ⇒ StructuredParser
constructor
A new instance of StructuredParser.
-
#parse(text) ⇒ Object
Instance of schema_class.
Methods inherited from Base
Methods included from Runnable
#batch, #invoke, #stream, #trace
Constructor Details
#initialize(schema_class) ⇒ StructuredParser
Returns a new instance of StructuredParser.
12 13 14 |
# File 'lib/phronomy/output_parser/structured_parser.rb', line 12 def initialize(schema_class) @schema_class = schema_class end |
Instance Method Details
#parse(text) ⇒ Object
Returns instance of schema_class.
19 20 21 22 23 24 |
# File 'lib/phronomy/output_parser/structured_parser.rb', line 19 def parse(text) data = JsonParser.new.parse(text) @schema_class.new(**data) rescue ArgumentError, TypeError => e raise Phronomy::ParseError, "Failed to map to schema: #{e.}" end |