Class: Servus::Testing::ExampleExtractor

Inherits:
Object
  • Object
show all
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.

Examples:

Basic extraction

schema = {
  type: 'object',
  properties: {
    name: { type: 'string', example: 'John Doe' },
    age: { type: 'integer', example: 30 }
  }
}

extractor = ExampleExtractor.new(schema)
extractor.extract
# => { name: 'John Doe', age: 30 }

With service class

examples = ExampleExtractor.extract(MyService, :arguments)
# => { user_id: 123, amount: 100.0 }

See Also:

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

schema = { type: 'object', properties: { name: { example: 'Test' } } }
extractor = ExampleExtractor.new(schema)

Parameters:

  • schema (Hash, nil)

    A JSON Schema hash with properties and examples



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.

Examples:

Extract argument examples

ExampleExtractor.extract(ProcessPayment::Service, :arguments)
# => { user_id: 123, amount: 100.0, currency: 'USD' }

Extract result examples

ExampleExtractor.extract(ProcessPayment::Service, :result)
# => { transaction_id: 'txn_123', status: 'approved' }

Parameters:

  • service_class (Class)

    The service class to extract examples from

  • schema_type (Symbol)

    Either :arguments or :result

Returns:

  • (Hash<Symbol, Object>)

    Extracted example values with symbolized keys



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

#extractHash<Symbol, Object>

Extracts all example values from the schema.

Traverses the schema structure and collects example values from:

  • Simple properties with example or examples keywords
  • Nested objects (recursively)
  • Arrays (using array-level examples or generating from item schemas)

Examples:

Simple properties

schema = {
  type: 'object',
  properties: {
    name: { type: 'string', example: 'John' },
    age: { type: 'integer', examples: [30, 25, 40] }
  }
}
extractor = ExampleExtractor.new(schema)
extractor.extract
# => { name: 'John', age: 30 }

Nested objects

schema = {
  type: 'object',
  properties: {
    user: {
      type: 'object',
      properties: {
        id: { type: 'integer', example: 123 },
        name: { type: 'string', example: 'Jane' }
      }
    }
  }
}
extractor = ExampleExtractor.new(schema)
extractor.extract
# => { user: { id: 123, name: 'Jane' } }

Returns:

  • (Hash<Symbol, Object>)

    Hash of example values with symbolized keys



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