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
@@ -25,16 +25,16 @@ module Sinatra
25
25
  TYPE_ARRAY = 'array'
26
26
 
27
27
  PRIMITIVE_TYPES = [
28
- TYPE_INTEGER,
29
- TYPE_LONG,
30
- TYPE_FLOAT,
31
- TYPE_DOUBLE,
32
- TYPE_STRING,
33
- TYPE_BYTE,
34
- TYPE_BOOLEAN,
35
- TYPE_DATE,
36
- TYPE_DATE_TIME,
37
- TYPE_PASSWORD,
28
+ TYPE_INTEGER,
29
+ TYPE_LONG,
30
+ TYPE_FLOAT,
31
+ TYPE_DOUBLE,
32
+ TYPE_STRING,
33
+ TYPE_BYTE,
34
+ TYPE_BOOLEAN,
35
+ TYPE_DATE,
36
+ TYPE_DATE_TIME,
37
+ TYPE_PASSWORD,
38
38
  ]
39
39
 
40
40
  TYPE_FILE = 'file'
@@ -56,15 +56,15 @@ module Sinatra
56
56
  PARAMS_MAX_LENGTH = :maxLength
57
57
 
58
58
  PARAMS_LIST = [
59
- PARAMS_FORMAT,
60
- PARAMS_DEFAULT,
61
- PARAMS_EXAMPLE,
62
- PARAMS_MINIMUM,
63
- PARAMS_MAXIMUM,
64
- PARAMS_EXCLUSIVE_MINIMUM,
65
- PARAMS_EXCLUSIVE_MAXIMUM,
66
- PARAMS_MIN_LENGTH,
67
- PARAMS_MAX_LENGTH,
59
+ PARAMS_FORMAT,
60
+ PARAMS_DEFAULT,
61
+ PARAMS_EXAMPLE,
62
+ PARAMS_MINIMUM,
63
+ PARAMS_MAXIMUM,
64
+ PARAMS_EXCLUSIVE_MINIMUM,
65
+ PARAMS_EXCLUSIVE_MAXIMUM,
66
+ PARAMS_MIN_LENGTH,
67
+ PARAMS_MAX_LENGTH,
68
68
  ]
69
69
 
70
70
  end
@@ -0,0 +1,180 @@
1
+ require_relative 'swagger-parameter-helper'
2
+
3
+ require_relative 'processing/swagger-array-value-processor'
4
+ require_relative 'processing/swagger-processor-dispatcher'
5
+ require_relative 'processing/swagger-primitive-value-processor'
6
+ require_relative 'processing/swagger-request-processor'
7
+ require_relative 'processing/swagger-response-processor'
8
+ require_relative 'processing/swagger-type-value-processor'
9
+
10
+ module Sinatra
11
+
12
+ module SwaggerExposer
13
+
14
+ # Create processor from configuration
15
+ class SwaggerProcessorCreator
16
+
17
+ include Sinatra::SwaggerExposer::SwaggerParameterHelper
18
+
19
+ # Initialize
20
+ # @param types [Sinatra::SwaggerExposer::SwaggerTypes]
21
+ def initialize(types)
22
+ @types = types
23
+ end
24
+
25
+ # Create an endpoint processor
26
+ # @param swagger_endpoint [Sinatra::SwaggerExposer::Configuration::SwaggerEndpoint] the endpoint
27
+ # @return [Sinatra::SwaggerExposer::Processing::SwaggerRequestProcessor]
28
+ def create_request_processor(swagger_endpoint)
29
+ request_processor = Sinatra::SwaggerExposer::Processing::SwaggerRequestProcessor.new(swagger_endpoint.produces)
30
+
31
+ swagger_endpoint.parameters.each do |parameter|
32
+ processor = create_parameter_value_processor(parameter)
33
+ dispatcher = Sinatra::SwaggerExposer::Processing::SwaggerProcessorDispatcher.new(
34
+ parameter.how_to_pass,
35
+ processor
36
+ )
37
+ if dispatcher.useful?
38
+ request_processor.add_dispatcher(dispatcher)
39
+ end
40
+ end
41
+
42
+ swagger_endpoint.responses.each_pair do |code, endpoint_response|
43
+ response_value_processor = create_response_value_processor(endpoint_response)
44
+ response_processor = Sinatra::SwaggerExposer::Processing::SwaggerResponseProcessor.new(
45
+ endpoint_response,
46
+ response_value_processor
47
+ )
48
+ request_processor.add_response_processor(
49
+ code,
50
+ response_processor.useful? ? response_processor : nil
51
+ )
52
+ end
53
+
54
+ request_processor
55
+ end
56
+
57
+ private
58
+
59
+ # Create a response processor
60
+ # @param endpoint_response [Sinatra::SwaggerExposer::Configuration::SwaggerEndpointResponse]
61
+ # @return [Sinatra::SwaggerExposer::Processing::SwaggerTypeValueProcessor]
62
+ def create_response_value_processor(endpoint_response)
63
+ response_type = endpoint_response.type
64
+ if response_type == TYPE_ARRAY
65
+ processor_for_values = create_processor_for_type('Response', endpoint_response.items, false)
66
+ Sinatra::SwaggerExposer::Processing::SwaggerArrayValueProcessor.new('Response', true, processor_for_values)
67
+ elsif response_type == TYPE_FILE
68
+ # Don't validate the files' content
69
+ nil
70
+ elsif response_type
71
+ create_processor_for_type('Response', response_type, false)
72
+ else
73
+ nil
74
+ end
75
+ end
76
+
77
+ # Create a parameter processor for a parameter
78
+ # @param parameter [Sinatra::SwaggerExposer::Configuration::SwaggerEndpointParameter]
79
+ # @return [Sinatra::SwaggerExposer::Processing::SwaggerTypeValueProcessor]
80
+ def create_parameter_value_processor(parameter)
81
+ type_name = parameter.type
82
+ if type_name == TYPE_ARRAY
83
+ if PRIMITIVE_TYPES.include? parameter.items
84
+ processor_for_values = Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValueProcessor.new(
85
+ parameter.name,
86
+ false,
87
+ parameter.items,
88
+ parameter.default,
89
+ parameter.params
90
+ )
91
+ else
92
+ processor_for_values = create_processor_for_type(parameter.name, parameter.items, false)
93
+ end
94
+ Sinatra::SwaggerExposer::Processing::SwaggerArrayValueProcessor.new(
95
+ parameter.name,
96
+ parameter.required,
97
+ processor_for_values
98
+ )
99
+ elsif PRIMITIVE_TYPES.include? type_name
100
+ Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValueProcessor.new(
101
+ parameter.name,
102
+ parameter.required,
103
+ type_name,
104
+ parameter.default,
105
+ parameter.params
106
+ )
107
+ else
108
+ create_processor_for_type(parameter.name, parameter.type, parameter.required)
109
+ end
110
+ end
111
+
112
+ # Create a type parameter processor for a type parameter
113
+ # @param parameter_name [String] the parameter name
114
+ # @param parameter_type [String] the parameter type
115
+ # @param parameter_required [TrueClass] if the parameter is required
116
+ # @return [Sinatra::SwaggerExposer::Processing::SwaggerTypeValueProcessor]
117
+ def create_processor_for_type(parameter_name, parameter_type, parameter_required)
118
+ attributes_processors = create_attributes_processors_for_type(parameter_type)
119
+ Sinatra::SwaggerExposer::Processing::SwaggerTypeValueProcessor.new(
120
+ parameter_name,
121
+ parameter_required,
122
+ attributes_processors
123
+ )
124
+ end
125
+
126
+ # Get attributes processor for a type
127
+ # @param type_name [String] the type name
128
+ # @return [Array<Sinatra::SwaggerExposer::Processing::SwaggerBaseValueProcessor>]
129
+ def create_attributes_processors_for_type(type_name)
130
+ type = @types[type_name]
131
+ attributes_processors = []
132
+ type.properties.each_pair do |property_name, property|
133
+ attributes_processors <<
134
+ create_processor_for_property(
135
+ property_name,
136
+ property,
137
+ type.required.include?(property.name)
138
+ )
139
+ end
140
+ if type.extends
141
+ attributes_processors = attributes_processors + create_attributes_processors_for_type(type.extends)
142
+ end
143
+ attributes_processors
144
+ end
145
+
146
+ # Create a processor for a type property
147
+ # @param type_property [Sinatra::SwaggerExposer::Configuration::SwaggerTypeProperty]
148
+ # @return [Sinatra::SwaggerExposer::Processing::SwaggerBaseValueProcessor]
149
+ def create_processor_for_property(name, type_property, required)
150
+ property_type = type_property.type
151
+ if property_type == TYPE_ARRAY
152
+ if PRIMITIVE_TYPES.include? type_property.items
153
+ processor_for_values = Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValueProcessor.new(
154
+ name,
155
+ false,
156
+ type_property.items,
157
+ type_property.properties[:default],
158
+ type_property.properties
159
+ )
160
+ else
161
+ processor_for_values = create_processor_for_type(name, type_property.items, false)
162
+ end
163
+ Sinatra::SwaggerExposer::Processing::SwaggerArrayValueProcessor.new(name, required, processor_for_values)
164
+ elsif PRIMITIVE_TYPES.include? property_type
165
+ Sinatra::SwaggerExposer::Processing::SwaggerPrimitiveValueProcessor.new(
166
+ name,
167
+ required,
168
+ property_type,
169
+ type_property.properties[:default],
170
+ type_property.properties
171
+ )
172
+ else
173
+ create_processor_for_type(name, property_type, required)
174
+ end
175
+ end
176
+
177
+ end
178
+
179
+ end
180
+ end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module SwaggerExposer
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -6,19 +6,20 @@ require 'sinatra/swagger-exposer/version'
6
6
  excluded_patterns = ['test/', 'example/', '.travis.yml', '.gitignore']
7
7
 
8
8
  Gem::Specification.new do |spec|
9
- spec.name = 'sinatra-swagger-exposer'
10
- spec.version = Sinatra::SwaggerExposer::VERSION
11
- spec.authors = ['Julien Kirch']
9
+ spec.name = 'sinatra-swagger-exposer'
10
+ spec.version = Sinatra::SwaggerExposer::VERSION
11
+ spec.authors = ['Julien Kirch']
12
12
 
13
- spec.summary = %q{Expose swagger API from your Sinatra app}
14
- spec.description = %q{This Sinatra extension enable you to add metadata to your code to expose your API as a Swagger endpoint and to validate and enrich the invocation parameters}
15
- spec.homepage = 'https://github.com/archiloque/sinatra-swagger-exposer'
16
- spec.license = 'MIT'
13
+ spec.summary = %q{Expose swagger API from your Sinatra app}
14
+ spec.description = %q{This Sinatra extension enable you to add metadata to your code to expose your API as a Swagger endpoint and to validate and enrich the invocation parameters}
15
+ spec.homepage = 'https://github.com/archiloque/sinatra-swagger-exposer'
16
+ spec.license = 'MIT'
17
17
 
18
- spec.files = `git ls-files -z`.split("\x0").reject { |f| excluded_patterns.any?{|ep| f.start_with?(ep)}}
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| excluded_patterns.any? { |ep| f.start_with?(ep) } }
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'sinatra', '~> 1.4'
22
+ spec.add_dependency 'mime-types', '~> 2.6.2'
22
23
 
23
24
  spec.add_development_dependency 'bundler'
24
25
  spec.add_development_dependency 'rake', '~> 10.0'
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.3.0
4
+ version: 0.4.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-07-29 00:00:00.000000000 Z
11
+ date: 2016-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mime-types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.6.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.6.2
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -107,27 +121,31 @@ files:
107
121
  - LICENSE.txt
108
122
  - README.md
109
123
  - Rakefile
124
+ - lib/sinatra/swagger-exposer/configuration/swagger-configuration-utilities.rb
110
125
  - lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb
111
126
  - lib/sinatra/swagger-exposer/configuration/swagger-endpoint-response.rb
112
127
  - lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb
128
+ - lib/sinatra/swagger-exposer/configuration/swagger-hash-like.rb
113
129
  - lib/sinatra/swagger-exposer/configuration/swagger-info.rb
114
130
  - lib/sinatra/swagger-exposer/configuration/swagger-parameter-validation-helper.rb
131
+ - lib/sinatra/swagger-exposer/configuration/swagger-response-header.rb
132
+ - lib/sinatra/swagger-exposer/configuration/swagger-response-headers.rb
115
133
  - lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb
116
134
  - lib/sinatra/swagger-exposer/configuration/swagger-type.rb
117
135
  - 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
136
+ - lib/sinatra/swagger-exposer/processing/swagger-array-value-processor.rb
137
+ - lib/sinatra/swagger-exposer/processing/swagger-base-value-processor.rb
138
+ - lib/sinatra/swagger-exposer/processing/swagger-parameter-processor.rb
139
+ - lib/sinatra/swagger-exposer/processing/swagger-primitive-value-processor.rb
140
+ - lib/sinatra/swagger-exposer/processing/swagger-processor-dispatcher.rb
141
+ - lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb
142
+ - lib/sinatra/swagger-exposer/processing/swagger-response-processor.rb
143
+ - lib/sinatra/swagger-exposer/processing/swagger-type-value-processor.rb
125
144
  - lib/sinatra/swagger-exposer/swagger-content-creator.rb
126
145
  - lib/sinatra/swagger-exposer/swagger-exposer.rb
127
146
  - lib/sinatra/swagger-exposer/swagger-invalid-exception.rb
128
147
  - lib/sinatra/swagger-exposer/swagger-parameter-helper.rb
129
- - lib/sinatra/swagger-exposer/swagger-preprocessor-creator.rb
130
- - lib/sinatra/swagger-exposer/swagger-utilities.rb
148
+ - lib/sinatra/swagger-exposer/swagger-request-processor-creator.rb
131
149
  - lib/sinatra/swagger-exposer/version.rb
132
150
  - sinatra-swagger-exposer.gemspec
133
151
  homepage: https://github.com/archiloque/sinatra-swagger-exposer
@@ -1,46 +0,0 @@
1
- require_relative '../swagger-parameter-helper'
2
- require_relative '../swagger-invalid-exception'
3
- require_relative 'swagger-base-value-preprocessor'
4
-
5
- module Sinatra
6
-
7
- module SwaggerExposer
8
-
9
- module Processing
10
-
11
- # Validate arrays parameters
12
- class SwaggerArrayValuePreprocessor < SwaggerBaseValuePreprocessor
13
-
14
- include Sinatra::SwaggerExposer::SwaggerParameterHelper
15
-
16
- attr_reader :preprocessor_for_values
17
-
18
- # Initialize
19
- # @param name [String] the name
20
- # @param required [TrueClass] if the parameter is required
21
- # @param preprocessor_for_values [Sinatra::SwaggerExposer::Processing::SwaggerBaseValuePreprocessor] processor for the values
22
- def initialize(name, required, preprocessor_for_values)
23
- super(name, required)
24
- @preprocessor_for_values = preprocessor_for_values
25
- end
26
-
27
- def useful?
28
- true
29
- end
30
-
31
- def validate_param_value(value)
32
- if value
33
- if value.is_a? Array
34
- value.collect { |i| @preprocessor_for_values.validate_param_value(i) }
35
- else
36
- raise SwaggerInvalidException.new("Parameter [#{name}] should be an array but is [#{value}]")
37
- end
38
- else
39
- nil
40
- end
41
- end
42
-
43
- end
44
- end
45
- end
46
- end
@@ -1,64 +0,0 @@
1
- require 'json'
2
-
3
- require_relative '../swagger-invalid-exception'
4
-
5
- module Sinatra
6
-
7
- module SwaggerExposer
8
-
9
- module Processing
10
-
11
- # A preprocessor for a request, apply the parameters preprocessors then execute the query code
12
- class SwaggerRequestPreprocessor
13
-
14
- attr_reader :preprocessors_dispatchers
15
-
16
- def initialize
17
- @preprocessors_dispatchers = []
18
- end
19
-
20
- def add_dispatcher(dispatcher)
21
- @preprocessors_dispatchers << dispatcher
22
- end
23
-
24
- VALID_JSON_CONTENT_TYPES = ['application/json', 'application/json; charset=utf-8']
25
-
26
- # Run the preprocessor the call the route content
27
- # @param app the sinatra app being run
28
- # @params block_params [Array] the block parameters
29
- # @param block the block containing the route content
30
- def run(app, block_params, &block)
31
- parsed_body = {}
32
- if VALID_JSON_CONTENT_TYPES.include? app.env['CONTENT_TYPE']
33
- body = app.request.body.read
34
- unless body.empty?
35
- begin
36
- parsed_body = JSON.parse(body)
37
- rescue JSON::ParserError => e
38
- return [400, {:code => 400, :message => e.message}.to_json]
39
- end
40
- end
41
- end
42
- app.params['parsed_body'] = parsed_body
43
- unless @preprocessors_dispatchers.empty?
44
- @preprocessors_dispatchers.each do |preprocessor_dispatcher|
45
- begin
46
- preprocessor_dispatcher.process(app, parsed_body)
47
- rescue SwaggerInvalidException => e
48
- app.content_type :json
49
- return [400, {:code => 400, :message => e.message}.to_json]
50
- end
51
- end
52
- end
53
- if block
54
- # Execute the block in the context of the app
55
- app.instance_exec(*block_params, &block)
56
- else
57
- ''
58
- end
59
- end
60
-
61
- end
62
- end
63
- end
64
- end