Class: Rack::JsonSchema::Mock::ResponseGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/json_schema/mock.rb

Class Method Summary collapse

Class Method Details

.call(schema) ⇒ Hash

Generates example response Hash from given schema

Examples:

Rack::JsonSchema::Mock::ResponseGenerator(schema) #=> { "id" => 1, "name" => "example" }

Returns:

  • (Hash)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rack/json_schema/mock.rb', line 61

def self.call(schema)
  schema.properties.inject({}) do |result, (key, value)|
    result.merge(
      key => case
      when !value.properties.empty?
        call(value)
      when !value.data["example"].nil?
        value.data["example"]
      when value.type.include?("null")
        nil
      when value.type.include?("array")
        [call(value.items)]
      else
        raise ExampleNotFound, "No example found for #{schema.pointer}/#{key}"
      end
    )
  end
end