Class: Servus::Testing::ExampleExtractor
- Inherits:
-
Object
- Object
- Servus::Testing::ExampleExtractor
- Defined in:
- lib/servus/testing/example_extractor.rb
Overview
Extracts example values from JSON Schema definitions for use in testing.
This class understands both OpenAPI-style example (singular) and
JSON Schema-style examples (plural, array) keywords. It can handle
nested objects, arrays, and complex schema structures.
Class Method Summary collapse
-
.extract(service_class, schema_type) ⇒ Hash<Symbol, Object>
Extracts example values from a service class's schema.
Instance Method Summary collapse
-
#extract ⇒ Hash<Symbol, Object>
Extracts all example values from the schema.
-
#initialize(schema) ⇒ ExampleExtractor
constructor
Initializes a new ExampleExtractor with a schema.
Constructor Details
#initialize(schema) ⇒ ExampleExtractor
Initializes a new ExampleExtractor with a schema.
The schema is deeply symbolized on initialization to normalize all keys, eliminating the need for double lookups throughout extraction.
64 65 66 |
# File 'lib/servus/testing/example_extractor.rb', line 64 def initialize(schema) @schema = deep_symbolize_keys(schema) end |
Class Method Details
.extract(service_class, schema_type) ⇒ Hash<Symbol, Object>
Extracts example values from a service class's schema.
This is a convenience class method that loads the schema via the Validator and extracts examples in one call.
47 48 49 50 51 52 |
# File 'lib/servus/testing/example_extractor.rb', line 47 def self.extract(service_class, schema_type) schema = load_schema(service_class, schema_type) return {} unless schema new(schema).extract end |
Instance Method Details
#extract ⇒ Hash<Symbol, Object>
Extracts all example values from the schema.
Traverses the schema structure and collects example values from:
- Simple properties with
exampleorexampleskeywords - Nested objects (recursively)
- Arrays (using array-level examples or generating from item schemas)
105 106 107 108 109 |
# File 'lib/servus/testing/example_extractor.rb', line 105 def extract return {} unless @schema.is_a?(Hash) extract_examples_from_properties(@schema) end |