Class: OAPI::OpenAPI::V30::Parsers::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/oapi/open_api/v30/parsers/json.rb

Class Method Summary collapse

Class Method Details

.parse(input) ⇒ Object



7
# File 'lib/oapi/open_api/v30/parsers/json.rb', line 7

def parse(input) = parse_object(input, OAPI::OpenAPI::V30::Definition)

.parse_array(input, klass) ⇒ Object



35
# File 'lib/oapi/open_api/v30/parsers/json.rb', line 35

def parse_array(input, klass) = klass.new(input.map { parse_type(_1, klass.item_type) })

.parse_map(input, klass) ⇒ Object



36
# File 'lib/oapi/open_api/v30/parsers/json.rb', line 36

def parse_map(input, klass) = klass.new(input.transform_values { parse_type(_1, klass.item_type) })

.parse_object(input, klass) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/oapi/open_api/v30/parsers/json.rb', line 25

def parse_object(input, klass)
  klass.new.tap do |obj|
    klass.properties.each do |name, type|
      value = input[name.camelize]

      obj.send(name, parse_type(value, type)) unless value.nil?
    end
  end
end

.parse_type(input, type) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/oapi/open_api/v30/parsers/json.rb', line 9

def parse_type(input, type)
  return input if type.nil?

  return parse_array(input, type) if type <= OAPI::Types::Array

  return parse_map(input, type) if type <= OAPI::Types::Map

  return OAPI::Ref.new(input[:$ref]) if input[:$ref]

  return type.new { input } if type <= OAPI::Schema

  return parse_object(input, type) if type <= OAPI::Types::Object

  raise ArgumentError, "unknown type #{type}"
end