sinatra-swagger-exposer 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb +118 -0
  4. data/lib/sinatra/swagger-exposer/configuration/swagger-endpoint-response.rb +64 -0
  5. data/lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb +88 -0
  6. data/lib/sinatra/swagger-exposer/configuration/swagger-info.rb +72 -0
  7. data/lib/sinatra/swagger-exposer/configuration/swagger-parameter-validation-helper.rb +106 -0
  8. data/lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb +82 -0
  9. data/lib/sinatra/swagger-exposer/configuration/swagger-type.rb +127 -0
  10. data/lib/sinatra/swagger-exposer/configuration/swagger-types.rb +51 -0
  11. data/lib/sinatra/swagger-exposer/processing/swagger-array-value-preprocessor.rb +46 -0
  12. data/lib/sinatra/swagger-exposer/processing/swagger-base-value-preprocessor.rb +48 -0
  13. data/lib/sinatra/swagger-exposer/processing/swagger-parameter-preprocessor.rb +47 -0
  14. data/lib/sinatra/swagger-exposer/processing/swagger-preprocessor-dispatcher.rb +45 -0
  15. data/lib/sinatra/swagger-exposer/processing/swagger-primitive-value-preprocessor.rb +165 -0
  16. data/lib/sinatra/swagger-exposer/processing/swagger-request-preprocessor.rb +64 -0
  17. data/lib/sinatra/swagger-exposer/processing/swagger-type-value-preprocessor.rb +37 -0
  18. data/lib/sinatra/swagger-exposer/swagger-content-creator.rb +3 -2
  19. data/lib/sinatra/swagger-exposer/swagger-exposer.rb +18 -20
  20. data/lib/sinatra/swagger-exposer/swagger-parameter-helper.rb +1 -1
  21. data/lib/sinatra/swagger-exposer/swagger-preprocessor-creator.rb +137 -0
  22. data/lib/sinatra/swagger-exposer/swagger-utilities.rb +1 -1
  23. data/lib/sinatra/swagger-exposer/version.rb +1 -1
  24. metadata +18 -10
  25. data/lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb +0 -197
  26. data/lib/sinatra/swagger-exposer/swagger-endpoint-response.rb +0 -63
  27. data/lib/sinatra/swagger-exposer/swagger-endpoint.rb +0 -94
  28. data/lib/sinatra/swagger-exposer/swagger-info.rb +0 -70
  29. data/lib/sinatra/swagger-exposer/swagger-parameter-preprocessor.rb +0 -187
  30. data/lib/sinatra/swagger-exposer/swagger-request-preprocessor.rb +0 -56
  31. data/lib/sinatra/swagger-exposer/swagger-type-property.rb +0 -72
  32. data/lib/sinatra/swagger-exposer/swagger-type.rb +0 -125
@@ -45,7 +45,7 @@ module Sinatra
45
45
  elsif @type.is_a? Array
46
46
  @items = type_to_s(get_array_type(@type))
47
47
  check_type(@items, possible_values)
48
- @type = 'array'
48
+ @type = TYPE_ARRAY
49
49
  else
50
50
  raise SwaggerInvalidException.new("Type [#{@type}] of has an unknown type, should be a class, a string or an array")
51
51
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module SwaggerExposer
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-swagger-exposer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Kirch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-26 00:00:00.000000000 Z
11
+ date: 2015-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -107,18 +107,26 @@ files:
107
107
  - LICENSE.txt
108
108
  - README.md
109
109
  - Rakefile
110
+ - lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb
111
+ - lib/sinatra/swagger-exposer/configuration/swagger-endpoint-response.rb
112
+ - lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb
113
+ - lib/sinatra/swagger-exposer/configuration/swagger-info.rb
114
+ - lib/sinatra/swagger-exposer/configuration/swagger-parameter-validation-helper.rb
115
+ - lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb
116
+ - lib/sinatra/swagger-exposer/configuration/swagger-type.rb
117
+ - lib/sinatra/swagger-exposer/configuration/swagger-types.rb
118
+ - lib/sinatra/swagger-exposer/processing/swagger-array-value-preprocessor.rb
119
+ - lib/sinatra/swagger-exposer/processing/swagger-base-value-preprocessor.rb
120
+ - lib/sinatra/swagger-exposer/processing/swagger-parameter-preprocessor.rb
121
+ - lib/sinatra/swagger-exposer/processing/swagger-preprocessor-dispatcher.rb
122
+ - lib/sinatra/swagger-exposer/processing/swagger-primitive-value-preprocessor.rb
123
+ - lib/sinatra/swagger-exposer/processing/swagger-request-preprocessor.rb
124
+ - lib/sinatra/swagger-exposer/processing/swagger-type-value-preprocessor.rb
110
125
  - lib/sinatra/swagger-exposer/swagger-content-creator.rb
111
- - lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb
112
- - lib/sinatra/swagger-exposer/swagger-endpoint-response.rb
113
- - lib/sinatra/swagger-exposer/swagger-endpoint.rb
114
126
  - lib/sinatra/swagger-exposer/swagger-exposer.rb
115
- - lib/sinatra/swagger-exposer/swagger-info.rb
116
127
  - lib/sinatra/swagger-exposer/swagger-invalid-exception.rb
117
128
  - lib/sinatra/swagger-exposer/swagger-parameter-helper.rb
118
- - lib/sinatra/swagger-exposer/swagger-parameter-preprocessor.rb
119
- - lib/sinatra/swagger-exposer/swagger-request-preprocessor.rb
120
- - lib/sinatra/swagger-exposer/swagger-type-property.rb
121
- - lib/sinatra/swagger-exposer/swagger-type.rb
129
+ - lib/sinatra/swagger-exposer/swagger-preprocessor-creator.rb
122
130
  - lib/sinatra/swagger-exposer/swagger-utilities.rb
123
131
  - lib/sinatra/swagger-exposer/version.rb
124
132
  - sinatra-swagger-exposer.gemspec
@@ -1,197 +0,0 @@
1
- require_relative 'swagger-invalid-exception'
2
- require_relative 'swagger-parameter-helper'
3
- require_relative 'swagger-parameter-preprocessor'
4
- require_relative 'swagger-type-property'
5
- require_relative 'swagger-utilities'
6
-
7
- module Sinatra
8
-
9
- module SwaggerExposer
10
-
11
- class SwaggerEndpointParameter
12
-
13
- include SwaggerUtilities
14
- include SwaggerParameterHelper
15
-
16
- def initialize(name, description, how_to_pass, required, type, params, known_types)
17
- unless name.is_a?(String) || name.is_a?(Symbol)
18
- raise SwaggerInvalidException.new("Name [#{name}] should be a string or a symbol")
19
- end
20
- name = name.to_s
21
- if name.empty?
22
- raise SwaggerInvalidException.new('Name should not be empty')
23
- end
24
- @name = name
25
-
26
- if description
27
- @description = description
28
- end
29
-
30
- how_to_pass = how_to_pass.to_s
31
- unless HOW_TO_PASS.include? how_to_pass
32
- raise SwaggerInvalidException.new("Unknown how to pass value [#{how_to_pass}]#{list_or_none(HOW_TO_PASS, 'registered types')}")
33
- end
34
- @how_to_pass = how_to_pass
35
-
36
- if @how_to_pass == HOW_TO_PASS_BODY
37
- get_type(type, PRIMITIVE_TYPES + known_types)
38
- else
39
- get_type(type, PRIMITIVE_TYPES_FOR_NON_BODY)
40
- end
41
-
42
- unless [true, false].include? required
43
- raise SwaggerInvalidException.new("Required should be a boolean instead of [#{required}]")
44
- end
45
- @required = required
46
-
47
- params = white_list_params(params, PARAMS_LIST, SwaggerTypeProperty::PROPERTIES)
48
- validate_params(params)
49
- @params = params
50
- end
51
-
52
- # Validate parameters
53
- # @param params [Hash]
54
- def validate_params(params)
55
- validate_limit_parameters(params)
56
- validate_length_parameters(params)
57
- end
58
-
59
- # Create the corresponding SwaggerParameterPreprocessor
60
- # @return [Sinatra::SwaggerExposer::SwaggerParameterPreprocessor]
61
- def preprocessor
62
- SwaggerParameterPreprocessor.new(@name, @how_to_pass, @required, @type, @params[:default], @params)
63
- end
64
-
65
- # Return the swagger version
66
- # @return [Hash]
67
- def to_swagger
68
- result = {
69
- :name => @name,
70
- :in => @how_to_pass,
71
- :required => @required
72
- }
73
-
74
- if @type
75
- if @type == 'array'
76
- result[:type] = 'array'
77
- if @items
78
- if PRIMITIVE_TYPES.include? @items
79
- result[:items] = {:type => @items}
80
- else
81
- result[:schema] = ref_to_type(@items)
82
- end
83
- end
84
- else
85
- if PRIMITIVE_TYPES.include? @type
86
- result[:type] = @type
87
- else
88
- result[:schema] = ref_to_type(@type)
89
- end
90
- end
91
- end
92
-
93
- if @description
94
- result[:description] = @description
95
- end
96
- unless @params.empty?
97
- result.merge!(@params)
98
- end
99
-
100
- result
101
- end
102
-
103
- def to_s
104
- {
105
- :name => @name,
106
- :in => @how_to_pass,
107
- :required => @required,
108
- :type => @type,
109
- :items => @items,
110
- :description => @description,
111
- :params => @params,
112
- }.to_json
113
- end
114
-
115
- private
116
-
117
- # Test if a parameter is a boolean
118
- # @param name the parameter's name
119
- # @param value value the parameter's value
120
- # @return [NilClass]
121
- def check_boolean(name, value)
122
- unless [true, false].include? value
123
- raise SwaggerInvalidException.new("Invalid boolean value [#{value}] for [#{name}]")
124
- end
125
- end
126
-
127
- # Validate the limit parameters
128
- # @param params [Hash] the parameters
129
- def validate_limit_parameters(params)
130
- max = validate_limit_parameter(params, PARAMS_MAXIMUM, PARAMS_EXCLUSIVE_MAXIMUM)
131
- min = validate_limit_parameter(params, PARAMS_MINIMUM, PARAMS_EXCLUSIVE_MINIMUM)
132
- if min && max && (max < min)
133
- raise SwaggerInvalidException.new("Minimum value [#{min}] can't be more than maximum value [#{max}]")
134
- end
135
- end
136
-
137
- # Validate a limit param like maximum and exclusiveMaximum
138
- # @param params [Hash] the parameters
139
- # @param limit_param_name [Symbol] the limit parameter name
140
- # @param exclusive_limit_param_name [Symbol] the exclusive limit parameter name
141
- def validate_limit_parameter(params, limit_param_name, exclusive_limit_param_name)
142
- parameter_value = nil
143
- if params.key? limit_param_name
144
- unless [TYPE_INTEGER, TYPE_NUMBER].include? @type
145
- raise SwaggerInvalidException.new("Parameter #{limit_param_name} can only be specified for types #{TYPE_INTEGER} or #{TYPE_NUMBER} and not for [#{@type}]")
146
- end
147
- parameter_value = params[limit_param_name]
148
- unless parameter_value.is_a? Numeric
149
- raise SwaggerInvalidException.new("Parameter #{limit_param_name} must be a numeric and can not be [#{parameter_value}]")
150
- end
151
- end
152
-
153
- if params.key? exclusive_limit_param_name
154
- check_boolean(exclusive_limit_param_name, params[exclusive_limit_param_name])
155
- unless params.key? limit_param_name
156
- raise SwaggerInvalidException.new("You can't have a #{exclusive_limit_param_name} value without a #{limit_param_name}")
157
- end
158
- end
159
- parameter_value
160
- end
161
-
162
- # Validate the length parameters minLength and maxLength
163
- # @param params [Hash] the parameters
164
- def validate_length_parameters(params)
165
- min_length = validate_length_parameter(params, PARAMS_MIN_LENGTH)
166
- max_length = validate_length_parameter(params, PARAMS_MAX_LENGTH)
167
-
168
- if min_length && max_length && (max_length < min_length)
169
- raise SwaggerInvalidException.new("Minimum length #{min_length} can't be more than maximum length #{max_length}")
170
- end
171
- end
172
-
173
- # Validate a length param like minLength and maxLength
174
- # @param params [Hash] the parameters
175
- # @param length_param_name [Symbol] the length parameter name
176
- # @return [Integer] the parameter value if it is present
177
- def validate_length_parameter(params, length_param_name)
178
- if params.key? length_param_name
179
- if @type == TYPE_STRING
180
- parameter_value = params[length_param_name]
181
- unless parameter_value.is_a? Integer
182
- raise SwaggerInvalidException.new("Parameter #{length_param_name} must be an integer and can not be [#{parameter_value}]")
183
- end
184
- parameter_value
185
- else
186
- raise SwaggerInvalidException.new("Parameter #{length_param_name} can only be specified for type #{TYPE_STRING} and not for [#{@type}]")
187
- end
188
-
189
- else
190
- nil
191
- end
192
- end
193
-
194
- end
195
-
196
- end
197
- end
@@ -1,63 +0,0 @@
1
- require_relative 'swagger-utilities'
2
- require_relative 'swagger-invalid-exception'
3
-
4
- module Sinatra
5
-
6
- module SwaggerExposer
7
-
8
- class SwaggerEndpointResponse
9
-
10
- include SwaggerUtilities
11
-
12
- RESPONSE_PRIMITIVES_FILES = PRIMITIVE_TYPES + [TYPE_FILE]
13
-
14
- def initialize(type, description, known_types)
15
- get_type(type, known_types + RESPONSE_PRIMITIVES_FILES)
16
- if description
17
- @description = description
18
- end
19
- end
20
-
21
- def to_swagger
22
- result = {}
23
-
24
- if @type
25
- if @type == 'array'
26
- schema = {:type => 'array'}
27
- if @items
28
- if RESPONSE_PRIMITIVES_FILES.include? @items
29
- schema[:items] = {:type => @items}
30
- else
31
- schema[:items] = ref_to_type(@items)
32
- end
33
- end
34
- result[:schema] = schema
35
- else
36
- if RESPONSE_PRIMITIVES_FILES.include? @type
37
- result[:schema] = {:type => @type}
38
- else
39
- result[:schema] = ref_to_type(@type)
40
- end
41
- end
42
- end
43
-
44
- if @description
45
- result[:description] = @description
46
- end
47
-
48
- result
49
- end
50
-
51
- def to_s
52
- {
53
- :type => @type,
54
- :items => @items,
55
- :description => @description,
56
- }.to_json
57
- end
58
-
59
-
60
- end
61
-
62
- end
63
- end
@@ -1,94 +0,0 @@
1
- require_relative 'swagger-request-preprocessor'
2
- require_relative 'swagger-utilities'
3
-
4
- module Sinatra
5
-
6
- module SwaggerExposer
7
-
8
- # An endpoint
9
- class SwaggerEndpoint
10
-
11
- include SwaggerUtilities
12
-
13
- attr_reader :path, :type, :request_preprocessor
14
-
15
- def initialize(type, sinatra_path, parameters, responses, summary, description, tags, explicit_path, produces)
16
- @type = type
17
- @path = swagger_path(sinatra_path, explicit_path)
18
- @request_preprocessor = SwaggerRequestPreprocessor.new
19
-
20
- @parameters = parameters
21
- @parameters.each do |parameter|
22
- preprocessor = parameter.preprocessor
23
- if preprocessor.useful?
24
- @request_preprocessor.add_preprocessor preprocessor
25
- end
26
- end
27
-
28
- @responses = responses
29
-
30
- @attributes = {}
31
- if summary
32
- @attributes[:summary] = summary
33
- end
34
- if description
35
- @attributes[:description] = description
36
- end
37
- if tags
38
- @attributes[:tags] = tags
39
- end
40
- if produces
41
- @attributes[:produces] = produces
42
- end
43
- end
44
-
45
- def to_swagger
46
- result = @attributes.clone
47
-
48
- unless @parameters.empty?
49
- result[:parameters] = @parameters.collect { |parameter| parameter.to_swagger }
50
- end
51
-
52
- unless @responses.empty?
53
- result[:responses] = hash_to_swagger(@responses)
54
- end
55
-
56
- result
57
- end
58
-
59
- REGEX_PATH_PARAM_MIDDLE = /\A(.*\/)\:([a-z]+)\/(.+)\z/
60
- REGEX_PATH_PARAM_END = /\A(.*)\/:([a-z]+)\z/
61
-
62
- # Get the endpoint swagger path
63
- # @param sinatra_path the path declared in the sinatra app
64
- # @param explicit_path an explicit path the user can specify
65
- def swagger_path(sinatra_path, explicit_path)
66
- if explicit_path
67
- explicit_path
68
- elsif sinatra_path.is_a? String
69
- while (m = REGEX_PATH_PARAM_MIDDLE.match(sinatra_path))
70
- sinatra_path = "#{m[1]}{#{m[2]}}/#{m[3]}"
71
- end
72
- if (m = REGEX_PATH_PARAM_END.match(sinatra_path))
73
- sinatra_path = "#{m[1]}/{#{m[2]}}"
74
- end
75
- sinatra_path
76
- else
77
- raise SwaggerInvalidException.new("You need to specify a path when using a non-string path [#{sinatra_path}]")
78
- end
79
- end
80
-
81
- def to_s
82
- {
83
- :type => @type,
84
- :path => @path,
85
- :attributes => @attributes,
86
- :parameters => @parameters,
87
- :responses => @responses,
88
- }.to_json
89
- end
90
-
91
- end
92
-
93
- end
94
- end
@@ -1,70 +0,0 @@
1
- require_relative 'swagger-invalid-exception'
2
- require_relative 'swagger-utilities'
3
-
4
- module Sinatra
5
-
6
- module SwaggerExposer
7
-
8
- # The info declaration
9
- class SwaggerInfo
10
-
11
- include SwaggerUtilities
12
-
13
- def initialize(values)
14
- @values = process(values, 'info', INFO_FIELDS, values)
15
- end
16
-
17
- # Known fields for the info field
18
- INFO_FIELDS = {
19
- :version => String,
20
- :title => String,
21
- :description => String,
22
- :termsOfService => String,
23
- :contact => {:name => String, :email => String, :url => String},
24
- :license => {:name => String, :url => String},
25
- }
26
-
27
- # Recursive function
28
- def process(current_hash, current_field_name, current_fields, top_level_hash)
29
- result = {}
30
-
31
- current_hash.each_pair do |current_key, current_value|
32
- key_sym = current_key.to_sym
33
- if current_fields.key? key_sym
34
-
35
- field_content = current_fields[key_sym]
36
- if field_content == String
37
- if current_value.is_a? String
38
- result[key_sym] = current_value
39
- else
40
- raise SwaggerInvalidException.new("Property [#{current_key}] value [#{current_value}] should be a String for #{current_field_name}: #{top_level_hash}")
41
- end
42
- else
43
- if current_value.is_a? Hash
44
- sub_params = process(current_value, current_field_name, field_content, top_level_hash)
45
- if sub_params
46
- result[key_sym] = sub_params
47
- end
48
- else
49
- raise SwaggerInvalidException.new("Property [#{current_key}] value [#{current_value}] should be a Hash for #{current_field_name}: #{top_level_hash}")
50
- end
51
- end
52
- else
53
- raise SwaggerInvalidException.new("Unknown property [#{current_key}] for #{current_field_name}#{list_or_none(current_fields.keys, 'values')}")
54
- end
55
- end
56
- result.empty? ? nil : result
57
- end
58
-
59
- def to_swagger
60
- @values
61
- end
62
-
63
- def to_s
64
- @values.to_json
65
- end
66
-
67
- end
68
-
69
- end
70
- end