Class: Sinatra::SwaggerExposer::Configuration::SwaggerType
- Inherits:
-
Object
- Object
- Sinatra::SwaggerExposer::Configuration::SwaggerType
- Includes:
- SwaggerConfigurationUtilities
- Defined in:
- lib/sinatra/swagger-exposer/configuration/swagger-type.rb
Overview
A type
Constant Summary collapse
- PROPERTY_PROPERTIES =
:properties
- PROPERTY_REQUIRED =
:required
- PROPERTY_EXAMPLE =
:example
- PROPERTY_EXTENDS =
:extends
- PROPERTIES =
[PROPERTY_PROPERTIES, PROPERTY_REQUIRED, PROPERTY_EXAMPLE, PROPERTY_EXTENDS]
Constants included from SwaggerParameterHelper
SwaggerParameterHelper::HOW_TO_PASS, SwaggerParameterHelper::HOW_TO_PASS_BODY, SwaggerParameterHelper::HOW_TO_PASS_HEADER, SwaggerParameterHelper::HOW_TO_PASS_PATH, SwaggerParameterHelper::HOW_TO_PASS_QUERY, SwaggerParameterHelper::PARAMS_DEFAULT, SwaggerParameterHelper::PARAMS_EXAMPLE, SwaggerParameterHelper::PARAMS_EXCLUSIVE_MAXIMUM, SwaggerParameterHelper::PARAMS_EXCLUSIVE_MINIMUM, SwaggerParameterHelper::PARAMS_FORMAT, SwaggerParameterHelper::PARAMS_LIST, SwaggerParameterHelper::PARAMS_MAXIMUM, SwaggerParameterHelper::PARAMS_MAX_LENGTH, SwaggerParameterHelper::PARAMS_MINIMUM, SwaggerParameterHelper::PARAMS_MIN_LENGTH, SwaggerParameterHelper::PRIMITIVE_TYPES, SwaggerParameterHelper::PRIMITIVE_TYPES_FOR_NON_BODY, SwaggerParameterHelper::TYPE_ARRAY, SwaggerParameterHelper::TYPE_BOOLEAN, SwaggerParameterHelper::TYPE_BYTE, SwaggerParameterHelper::TYPE_DATE, SwaggerParameterHelper::TYPE_DATE_TIME, SwaggerParameterHelper::TYPE_DOUBLE, SwaggerParameterHelper::TYPE_FILE, SwaggerParameterHelper::TYPE_FLOAT, SwaggerParameterHelper::TYPE_INTEGER, SwaggerParameterHelper::TYPE_LONG, SwaggerParameterHelper::TYPE_NUMBER, SwaggerParameterHelper::TYPE_PASSWORD, SwaggerParameterHelper::TYPE_STRING
Instance Attribute Summary collapse
-
#extends ⇒ Object
readonly
Returns the value of attribute extends.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
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
Methods included from SwaggerConfigurationUtilities
#check_name, #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.
24 25 26 27 28 29 30 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 24 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 Attribute Details
#extends ⇒ Object (readonly)
Returns the value of attribute extends.
14 15 16 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 14 def extends @extends end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
14 15 16 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 14 def properties @properties end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
14 15 16 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 14 def required @required end |
Instance Method Details
#check_attribute_empty_or_bad(type_name, type_content, attribute_name, attribute_class) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 75 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 60 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
83 84 85 86 87 88 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 83 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
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 32 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 45 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
117 118 119 120 121 122 123 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 117 def to_s { :properties => @properties, :required => @required, :example => @example, }.to_json end |
#to_swagger ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type.rb', line 90 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 |