Class: Sinatra::SwaggerExposer::SwaggerType
- Inherits:
-
Object
- Object
- Sinatra::SwaggerExposer::SwaggerType
show all
- Includes:
- SwaggerUtilities
- Defined in:
- lib/sinatra/swagger-exposer/swagger-type.rb
Overview
Constant Summary
collapse
- PROPERTY_PROPERTIES =
:properties
- PROPERTY_REQUIRED =
:required
- PROPERTY_EXAMPLE =
:example
- PROPERTY_EXTENDS =
:extends
- PROPERTIES =
[PROPERTY_PROPERTIES, PROPERTY_REQUIRED, PROPERTY_EXAMPLE, PROPERTY_EXTENDS]
Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_BODY, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_HEADER, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_PATH, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_QUERY, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_DEFAULT, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXAMPLE, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXCLUSIVE_MAXIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXCLUSIVE_MINIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_FORMAT, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_LIST, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MAXIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MAX_LENGTH, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MINIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MIN_LENGTH, Sinatra::SwaggerExposer::SwaggerParameterHelper::PRIMITIVE_TYPES, Sinatra::SwaggerExposer::SwaggerParameterHelper::PRIMITIVE_TYPES_FOR_NON_BODY, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_BOOLEAN, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_BYTE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DATE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DATE_TIME, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DOUBLE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_FILE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_FLOAT, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_INTEGER, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_LONG, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_NUMBER, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_PASSWORD, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_STRING
Instance Method Summary
collapse
-
#check_attribute_empty_or_bad(type_name, type_content, attribute_name, attribute_class) ⇒ Object
-
#initialize(type_name, type_properties, known_types) ⇒ SwaggerType
constructor
A new instance of SwaggerType.
-
#process_example(type_name, type_content, properties_names) ⇒ Object
-
#process_extends(type_properties, known_types) ⇒ Object
-
#process_properties(type_name, type_content, known_types) ⇒ Object
-
#process_required(type_name, type_content, properties_names) ⇒ Object
-
#to_s ⇒ Object
-
#to_swagger ⇒ Object
#get_type, #hash_to_swagger, #list_or_none, #ref_to_type, #type_to_s, #white_list_params
Constructor Details
#initialize(type_name, type_properties, known_types) ⇒ SwaggerType
Returns a new instance of SwaggerType.
20
21
22
23
24
25
26
|
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 20
def initialize(type_name, type_properties, known_types)
white_list_params(type_properties, PROPERTIES)
@properties = process_properties(type_name, type_properties, known_types)
@required = process_required(type_name, type_properties, @properties.keys)
@example = process_example(type_name, type_properties, @properties.keys)
@extends = process_extends(type_properties, known_types)
end
|
Instance Method Details
#check_attribute_empty_or_bad(type_name, type_content, attribute_name, attribute_class) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 71
def check_attribute_empty_or_bad(type_name, type_content, attribute_name, attribute_class)
if !type_content.key?(attribute_name)
attribute_class.new
elsif !type_content[attribute_name].is_a? attribute_class
raise SwaggerInvalidException.new("Attribute [#{attribute_name}] of #{type_name} is not an hash: #{type_content[attribute_name]}")
end
end
|
#process_example(type_name, type_content, properties_names) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 56
def process_example(type_name, type_content, properties_names)
possible_value = check_attribute_empty_or_bad(type_name, type_content, PROPERTY_EXAMPLE, Hash)
if possible_value
possible_value
else
type_content[PROPERTY_EXAMPLE].each_pair do |property_name, property_value|
property_name = property_name.to_s
unless properties_names.include? property_name
raise SwaggerInvalidException.new("Example property [#{property_name}] with value [#{property_value}] of [#{type_name}] is unknown#{list_or_none(properties_names, 'properties')}")
end
end
type_content[PROPERTY_EXAMPLE]
end
end
|
#process_extends(type_properties, known_types) ⇒ Object
79
80
81
82
83
84
|
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 79
def process_extends(type_properties, known_types)
if type_properties.key? PROPERTY_EXTENDS
check_type(type_properties[PROPERTY_EXTENDS], known_types)
@extends = type_properties[PROPERTY_EXTENDS]
end
end
|
#process_properties(type_name, type_content, known_types) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 28
def process_properties(type_name, type_content, known_types)
possible_value = check_attribute_empty_or_bad(type_name, type_content, PROPERTY_PROPERTIES, Hash)
if possible_value
possible_value
else
result = {}
type_content[PROPERTY_PROPERTIES].each_pair do |property_name, property_properties|
result[property_name.to_s] = SwaggerTypeProperty.new(type_name, property_name, property_properties, known_types)
end
result
end
end
|
#process_required(type_name, type_content, properties_names) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 41
def process_required(type_name, type_content, properties_names)
possible_value = check_attribute_empty_or_bad(type_name, type_content, PROPERTY_REQUIRED, Array)
if possible_value
possible_value
else
type_content[PROPERTY_REQUIRED].each do |property_name|
property_name = property_name.to_s
unless properties_names.include? property_name
raise SwaggerInvalidException.new("Required property [#{property_name}] of [#{type_name}] is unknown#{list_or_none(properties_names, 'properties')}")
end
end
type_content[PROPERTY_REQUIRED]
end
end
|
#to_s ⇒ Object
113
114
115
116
117
118
119
|
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 113
def to_s
{
:properties => @properties,
:required => @required,
:example => @example,
}.to_json
end
|
#to_swagger ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 86
def to_swagger
result = {:type => 'object'}
unless @properties.empty?
result[PROPERTY_PROPERTIES] = hash_to_swagger(@properties)
end
unless @required.empty?
result[PROPERTY_REQUIRED] = @required
end
unless @example.empty?
result[PROPERTY_EXAMPLE] = @example
end
if @extends
result = {
:allOf => [
ref_to_type(@extends),
result
]
}
end
result
end
|