sinatra-swagger-exposer 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/Gemfile +0 -1
  4. data/README.md +1 -0
  5. data/lib/sinatra/swagger-exposer/configuration/swagger-configuration-utilities.rb +124 -0
  6. data/lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb +15 -20
  7. data/lib/sinatra/swagger-exposer/configuration/swagger-endpoint-response.rb +39 -7
  8. data/lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb +21 -8
  9. data/lib/sinatra/swagger-exposer/configuration/swagger-hash-like.rb +45 -0
  10. data/lib/sinatra/swagger-exposer/configuration/swagger-info.rb +9 -8
  11. data/lib/sinatra/swagger-exposer/configuration/swagger-response-header.rb +68 -0
  12. data/lib/sinatra/swagger-exposer/configuration/swagger-response-headers.rb +33 -0
  13. data/lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb +7 -6
  14. data/lib/sinatra/swagger-exposer/configuration/swagger-type.rb +10 -9
  15. data/lib/sinatra/swagger-exposer/configuration/swagger-types.rb +4 -20
  16. data/lib/sinatra/swagger-exposer/processing/swagger-array-value-processor.rb +46 -0
  17. data/lib/sinatra/swagger-exposer/processing/{swagger-base-value-preprocessor.rb → swagger-base-value-processor.rb} +9 -7
  18. data/lib/sinatra/swagger-exposer/processing/{swagger-parameter-preprocessor.rb → swagger-parameter-processor.rb} +9 -9
  19. data/lib/sinatra/swagger-exposer/processing/{swagger-primitive-value-preprocessor.rb → swagger-primitive-value-processor.rb} +46 -46
  20. data/lib/sinatra/swagger-exposer/processing/{swagger-preprocessor-dispatcher.rb → swagger-processor-dispatcher.rb} +11 -11
  21. data/lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb +123 -0
  22. data/lib/sinatra/swagger-exposer/processing/swagger-response-processor.rb +47 -0
  23. data/lib/sinatra/swagger-exposer/processing/swagger-type-value-processor.rb +37 -0
  24. data/lib/sinatra/swagger-exposer/swagger-content-creator.rb +3 -7
  25. data/lib/sinatra/swagger-exposer/swagger-exposer.rb +99 -33
  26. data/lib/sinatra/swagger-exposer/swagger-parameter-helper.rb +19 -19
  27. data/lib/sinatra/swagger-exposer/swagger-request-processor-creator.rb +180 -0
  28. data/lib/sinatra/swagger-exposer/version.rb +1 -1
  29. data/sinatra-swagger-exposer.gemspec +9 -8
  30. metadata +29 -11
  31. data/lib/sinatra/swagger-exposer/processing/swagger-array-value-preprocessor.rb +0 -46
  32. data/lib/sinatra/swagger-exposer/processing/swagger-request-preprocessor.rb +0 -64
  33. data/lib/sinatra/swagger-exposer/processing/swagger-type-value-preprocessor.rb +0 -37
  34. data/lib/sinatra/swagger-exposer/swagger-preprocessor-creator.rb +0 -137
  35. data/lib/sinatra/swagger-exposer/swagger-utilities.rb +0 -108
@@ -1,37 +0,0 @@
1
- require_relative 'swagger-base-value-preprocessor'
2
-
3
- module Sinatra
4
-
5
- module SwaggerExposer
6
-
7
- module Processing
8
-
9
- # A preprocessor for a type parameter
10
- class SwaggerTypeValuePreprocessor < SwaggerBaseValuePreprocessor
11
-
12
- attr_reader :attributes_preprocessors
13
-
14
- # Initialize
15
- # @param name [String] the name
16
- # @param required [TrueClass] if the parameter is required
17
- # @param attributes_preprocessors [Array[Sinatra::SwaggerExposer::Processing::SwaggerBaseValuePreprocessor]] the attributes preprocessors
18
- def initialize(name, required, attributes_preprocessors)
19
- super(name, required)
20
- @attributes_preprocessors = attributes_preprocessors
21
- end
22
-
23
- def useful?
24
- super || (!(@attributes_preprocessors.empty?))
25
- end
26
-
27
- def validate_param_value(value)
28
- @attributes_preprocessors.each do |attribute_preprocessor|
29
- attribute_preprocessor.process(value)
30
- end
31
- value
32
- end
33
-
34
- end
35
- end
36
- end
37
- end
@@ -1,137 +0,0 @@
1
- require_relative 'swagger-utilities'
2
- require_relative 'processing/swagger-array-value-preprocessor'
3
- require_relative 'processing/swagger-preprocessor-dispatcher'
4
- require_relative 'processing/swagger-primitive-value-preprocessor'
5
- require_relative 'processing/swagger-request-preprocessor'
6
- require_relative 'processing/swagger-type-value-preprocessor'
7
-
8
- module Sinatra
9
-
10
- module SwaggerExposer
11
-
12
- # Create processor from configuration
13
- class SwaggerPreprocessorCreator
14
-
15
- include Sinatra::SwaggerExposer::SwaggerUtilities
16
-
17
- # Initialize
18
- # @param types [Sinatra::SwaggerExposer::SwaggerTypes]
19
- def initialize(types)
20
- @types = types
21
- end
22
-
23
- # Create an endpoint processor
24
- # @param swagger_endpoint [Sinatra::SwaggerExposer::Configuration::SwaggerEndpoint] the endpoint
25
- # @return [Sinatra::SwaggerExposer::Processing::SwaggerRequestPreprocessor]
26
- def create_endpoint_processor(swagger_endpoint)
27
- request_preprocessor = Sinatra::SwaggerExposer::Processing::SwaggerRequestPreprocessor.new
28
- swagger_endpoint.parameters.each do |parameter|
29
- preprocessor = create_value_preprocessor(parameter)
30
- dispatcher = Sinatra::SwaggerExposer::Processing::SwaggerPreprocessorDispatcher.new(parameter.how_to_pass, preprocessor)
31
- if dispatcher.useful?
32
- request_preprocessor.add_dispatcher(dispatcher)
33
- end
34
- end
35
- request_preprocessor
36
- end
37
-
38
- private
39
-
40
- # Create a parameter preprocessor for a parameter
41
- # @param parameter [Sinatra::SwaggerExposer::Configuration::SwaggerEndpointParameter]
42
- def create_value_preprocessor(parameter)
43
- type_name = parameter.type
44
- if type_name == TYPE_ARRAY
45
- if PRIMITIVE_TYPES.include? parameter.items
46
- preprocessor_for_values = Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValuePreprocessor.new(
47
- parameter.name,
48
- false,
49
- parameter.items,
50
- parameter.default,
51
- parameter.params
52
- )
53
- else
54
- preprocessor_for_values = create_preprocessor_for_type(parameter.name, parameter.items, false)
55
- end
56
- Sinatra::SwaggerExposer::Processing::SwaggerArrayValuePreprocessor.new(parameter.name, parameter.required, preprocessor_for_values)
57
- elsif PRIMITIVE_TYPES.include? type_name
58
- Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValuePreprocessor.new(
59
- parameter.name,
60
- parameter.required,
61
- type_name,
62
- parameter.default,
63
- parameter.params
64
- )
65
- else
66
- create_preprocessor_for_type(parameter.name, parameter.type, parameter.required)
67
- end
68
- end
69
-
70
- # Create a type parameter preprocessor for a type parameter
71
- # @param parameter_name [String] the parameter name
72
- # @param parameter_type [String] the parameter type
73
- # @param parameter_required [TrueClass] if the parameter is required
74
- # @return [Sinatra::SwaggerExposer::Processing::SwaggerTypeValuePreprocessor]
75
- def create_preprocessor_for_type(parameter_name, parameter_type, parameter_required)
76
- attributes_preprocessors = create_attributes_preprocessors_for_type(parameter_type)
77
- Sinatra::SwaggerExposer::Processing::SwaggerTypeValuePreprocessor.new(
78
- parameter_name,
79
- parameter_required,
80
- attributes_preprocessors
81
- )
82
- end
83
-
84
- # Get attributes preprocessor for a type
85
- # @param type_name [String] the type name
86
- # @return [Array[Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValuePreprocessor]]
87
- def create_attributes_preprocessors_for_type(type_name)
88
- type = @types[type_name]
89
- attributes_preprocessors = []
90
- type.properties.each_pair do |property_name, property|
91
- attributes_preprocessors <<
92
- create_preprocessor_for_property(
93
- property_name,
94
- property,
95
- type.required.include?(property.name)
96
- )
97
- end
98
- if type.extends
99
- attributes_preprocessors = attributes_preprocessors + create_attributes_preprocessors_for_type(type.extends)
100
- end
101
- attributes_preprocessors
102
- end
103
-
104
- # Create a processor for a type property
105
- # @param type_property [Sinatra::SwaggerExposer::Configuration::SwaggerTypeProperty]
106
- def create_preprocessor_for_property(name, type_property, required)
107
- property_type = type_property.type
108
- if property_type == TYPE_ARRAY
109
- if PRIMITIVE_TYPES.include? type_property.items
110
- preprocessor_for_values = Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValuePreprocessor.new(
111
- name,
112
- false,
113
- type_property.items,
114
- type_property.properties[:default],
115
- type_property.properties
116
- )
117
- else
118
- preprocessor_for_values = create_preprocessor_for_type(name, type_property.items, false)
119
- end
120
- Sinatra::SwaggerExposer::Processing::SwaggerArrayValuePreprocessor.new(name, required, preprocessor_for_values)
121
- elsif PRIMITIVE_TYPES.include? property_type
122
- Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValuePreprocessor.new(
123
- name,
124
- required,
125
- property_type,
126
- type_property.properties[:default],
127
- type_property.properties
128
- )
129
- else
130
- create_preprocessor_for_type(name, property_type, required)
131
- end
132
- end
133
-
134
- end
135
-
136
- end
137
- end
@@ -1,108 +0,0 @@
1
- require_relative 'swagger-invalid-exception'
2
- require_relative 'swagger-parameter-helper'
3
-
4
- module Sinatra
5
-
6
- module SwaggerExposer
7
-
8
- module SwaggerUtilities
9
-
10
- include ::Sinatra::SwaggerExposer::SwaggerParameterHelper
11
-
12
- def ref_to_type(type)
13
- {'$ref' => "#/definitions/#{type}"}
14
- end
15
-
16
- def hash_to_swagger(hash)
17
- result = {}
18
- hash.each_pair do |key, value|
19
- result[key] = value.to_swagger
20
- end
21
- result
22
- end
23
-
24
- # Transform a type into a String
25
- # @return [String]
26
- def type_to_s(value)
27
- if [TrueClass, FalseClass].include? value
28
- TYPE_BOOLEAN
29
- elsif value == DateTime
30
- TYPE_DATE_TIME
31
- elsif value.is_a? Class
32
- value.to_s.downcase
33
- else
34
- value
35
- end
36
- end
37
-
38
- def get_type(type, possible_values)
39
- @type = type
40
- if type.nil?
41
- raise SwaggerInvalidException.new('Type is nil')
42
- elsif type.is_a?(String) || @type.is_a?(Class)
43
- @type = type_to_s(@type)
44
- check_type(@type, possible_values)
45
- elsif @type.is_a? Array
46
- @items = type_to_s(get_array_type(@type))
47
- check_type(@items, possible_values)
48
- @type = TYPE_ARRAY
49
- else
50
- raise SwaggerInvalidException.new("Type [#{@type}] of has an unknown type, should be a class, a string or an array")
51
- end
52
- end
53
-
54
- # Validate if a parameter is in a list of available values
55
- # @param params [Hash] the parameters
56
- # @param allowed_values [Enumerable, #include?] the allowed values
57
- # @param ignored_values [Enumerable, #include?] values to ignore
58
- # @return [Hash] the filtered hash
59
- def white_list_params(params, allowed_values, ignored_values = [])
60
- result = {}
61
- params.each_pair do |key, value|
62
- if allowed_values.include? key
63
- result[key] = value
64
- elsif !ignored_values.include?(key)
65
- raise SwaggerInvalidException.new("Unknown property [#{key}] with value [#{value}]#{list_or_none(allowed_values, 'properties')}")
66
- end
67
- end
68
- result
69
- end
70
-
71
- def list_or_none(list, name)
72
- if list.empty?
73
- ", no available #{name}"
74
- else
75
- ", possible #{name} are #{list.join(', ')}"
76
- end
77
- end
78
-
79
- private
80
-
81
- def get_array_type(array)
82
- if array.empty?
83
- raise SwaggerInvalidException.new('Type is an empty array, you should specify a type as the array content')
84
- elsif array.length > 1
85
- raise SwaggerInvalidException.new("Type [#{array}] has more than one entry, it should only have one")
86
- else
87
- type_to_s(array[0])
88
- end
89
- end
90
-
91
- # Validate if a type is in a list of available values
92
- # @param type [String] the parameter
93
- # @param allowed_values [Enumerable, #include?] the allowed values
94
- # @return [NilClass]
95
- def check_type(type, allowed_values)
96
- if allowed_values.empty?
97
- raise SwaggerInvalidException.new("Unknown type [#{type}], no available type")
98
- elsif !allowed_values.include?(type)
99
- raise SwaggerInvalidException.new("Unknown type [#{type}]#{list_or_none(allowed_values, 'types')}")
100
- end
101
- end
102
-
103
- end
104
-
105
- end
106
-
107
- end
108
-