Class: SoberSwag::Reporting::Input::Object
- Inherits:
-
Base
- Object
- Base
- SoberSwag::Reporting::Input::Object
show all
- Defined in:
- lib/sober_swag/reporting/input/object.rb,
lib/sober_swag/reporting/input/object/property.rb
Overview
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.
10
11
12
|
# File 'lib/sober_swag/reporting/input/object.rb', line 10
def initialize(fields)
@fields = fields
end
|
Instance Attribute Details
#fields ⇒ 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|
(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
|
90
91
92
93
94
95
96
|
# File 'lib/sober_swag/reporting/input/object.rb', line 90
def (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_schemas ⇒ Object
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_portion ⇒ Object
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_schema ⇒ Object
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_schema ⇒ Object
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_schema ⇒ Object
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_schema ⇒ Object
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
|