Class: SoberSwag::Reporting::Input::Object

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/reporting/input/object.rb,
lib/sober_swag/reporting/input/object/property.rb

Overview

Input object values

Defined Under Namespace

Classes: Property

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interface

#add_schema_key, #call!, #described, #enum, #format, #in_range, #list, #mapped, #modify_schema, #multiple_of, #optional, #or, #referenced, #|

Constructor Details

#initialize(fields) ⇒ Object

Returns a new instance of Object.

Parameters:



10
11
12
# File 'lib/sober_swag/reporting/input/object.rb', line 10

def initialize(fields)
  @fields = fields
end

Instance Attribute Details

#fieldsHash<String,#call> (readonly)

Returns:

  • (Hash<String,#call>)


16
17
18
# File 'lib/sober_swag/reporting/input/object.rb', line 16

def fields
  @fields
end

Instance Method Details

#call(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sober_swag/reporting/input/object.rb', line 18

def call(value)
  return Report::Value.new(['was a not a JSON object']) unless value.is_a?(Hash)

  bad, good = fields.map { |k, prop|
    extract_value(k, prop, value)
  }.compact.partition { |(_, v)| v.is_a?(Report::Base) }

  return Report::Object.new(bad.to_h) if bad.any?

  good.to_h
end

#extract_value(key, property, input) ⇒ Object (private)



90
91
92
93
94
95
96
# File 'lib/sober_swag/reporting/input/object.rb', line 90

def extract_value(key, property, input)
  if input.key?(key)
    [key, property.value.call(input[key])]
  elsif property.required?
    [key, Report::Value.new(['is required'])]
  end
end

#field_schemasObject (private)



67
68
69
70
71
72
73
74
75
# File 'lib/sober_swag/reporting/input/object.rb', line 67

def field_schemas
  fields.reduce([{}, {}]) do |(field_schemas, found), (k, v)|
    key_schema, key_found = v.property_schema
    [
      field_schemas.merge(k => key_schema),
      found.merge(key_found)
    ]
  end
end

#required_portionObject (private)

Either the list of required keys, or something stating "provide at least one key." This is needed because you can't have an empty list of keys.



80
81
82
83
84
85
86
87
88
# File 'lib/sober_swag/reporting/input/object.rb', line 80

def required_portion
  required_fields = fields.map { |k, v| k if v.required? }.compact

  if required_fields.empty?
    {}
  else
    { required: required_fields }
  end
end

#swagger_parameter_schemaObject (private)



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sober_swag/reporting/input/object.rb', line 55

def swagger_parameter_schema
  fields.map do |name, field|
    key_schema, = field.property_schema
    base = {
      name: name,
      schema: key_schema,
      required: field.required?
    }
    field.description ? base.merge(description: field.description) : base
  end
end

#swagger_path_schemaObject



47
48
49
50
51
# File 'lib/sober_swag/reporting/input/object.rb', line 47

def swagger_path_schema
  swagger_parameter_schema.map do |param|
    param.merge({ in: :path })
  end
end

#swagger_query_schemaObject



41
42
43
44
45
# File 'lib/sober_swag/reporting/input/object.rb', line 41

def swagger_query_schema
  swagger_parameter_schema.map do |param|
    param.merge({ in: :query, style: :deepObject, explode: true })
  end
end

#swagger_schemaObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/sober_swag/reporting/input/object.rb', line 30

def swagger_schema
  fields, found = field_schemas

  obj = {
    type: 'object',
    properties: fields
  }.merge(required_portion)

  [obj, found]
end