weak_swagger_parameters 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85e46ed943f82522b4affcca0fbe25da225527c8
4
- data.tar.gz: e8f515e3aa857121ec296d75c7783cdb0758334c
3
+ metadata.gz: cecd75ac8a8c7da9b795eeaaf4830bcb99670970
4
+ data.tar.gz: 115a0037332d2fe62126774588676c6fa98ce27d
5
5
  SHA512:
6
- metadata.gz: db40e7a3e5b89864cc151dc2f516d0769a1134bed82a3d6308506de488b720f1eae1052286a058546695f78dcaaa64c3d0ba10409b32e5b46b5de1a79043524c
7
- data.tar.gz: 5e261df19f41b3c7a99e0c381d21b695df5997ae306390b25fa3913918399effd8fc0c9d9b88a9746fb41fbe2786df500fbc15e081ce8f1b672874d35f5d21e4
6
+ metadata.gz: 3b23c81c52f4ffe42c1d9a670af103a90fe22338a20cd09477317fe8e90c2fddddd705daaef2fb86f129e2e8227bf197d040acfd7c8117f38ffd244f2bfd19a1
7
+ data.tar.gz: 4391e29cdbd8066d920838a26a6439e9d0904b60a1eabfbbe6c1065450f70264bc13eaad71aed114757b980e4808acefca9bc8544ac053045b890d6e5ddf1cd9
@@ -50,9 +50,7 @@ module WeakSwaggerParameters
50
50
 
51
51
  def render_docs(docs_section)
52
52
  ensure_loaded
53
- controllers = controllers_in_docs_section(docs_section)
54
-
55
- render json: Swagger::Blocks.build_root_json(controllers)
53
+ render json: Swagger::Blocks.build_root_json(doc_components_for(docs_section))
56
54
  end
57
55
 
58
56
  def handle_param_validation_error(e)
@@ -61,26 +59,20 @@ module WeakSwaggerParameters
61
59
 
62
60
  private
63
61
 
64
- def controllers_in_docs_section(docs_section)
65
- controllers = []
62
+ def doc_components_for(docs_section)
63
+ doc_components = []
66
64
  ObjectSpace.each_object(Class) do |klass|
67
- if controller?(klass) && in_docs_section?(klass, docs_section)
68
- controllers << klass
69
- end
65
+ doc_components << klass if in_docs_section?(klass, docs_section)
70
66
  end
71
- controllers
67
+ doc_components
72
68
  end
73
69
 
74
70
  def ensure_loaded
75
71
  Rails.application.eager_load! if !Rails.configuration.eager_load || !Rails.configuration.cache_classes
76
72
  end
77
73
 
78
- def controller?(klass)
79
- klass.ancestors.include?(AbstractController::Base)
80
- end
81
-
82
74
  def in_docs_section?(klass, docs_section)
83
- klass.respond_to?(:in_doc_section?) && klass.in_doc_section?(docs_section)
75
+ klass.methods.include?(:in_doc_section?) && klass.respond_to?(:in_doc_section?) && klass.in_doc_section?(docs_section)
84
76
  end
85
77
  end
86
78
  end
@@ -24,8 +24,8 @@ module WeakSwaggerParameters
24
24
  @param_definition = WeakSwaggerParameters::Definitions::Params.new(&block)
25
25
  end
26
26
 
27
- def response(status_code, description = '')
28
- @response_definitions << WeakSwaggerParameters::Definitions::Response.new(status_code, description)
27
+ def response(status_code, description = '', &block)
28
+ @response_definitions << WeakSwaggerParameters::Definitions::Response.new(status_code, description, &block)
29
29
  end
30
30
 
31
31
  def apply_validations(controller_class)
@@ -42,7 +42,7 @@ module WeakSwaggerParameters
42
42
  def apply_docs(controller_class)
43
43
  this = self
44
44
  http_method = @http_method
45
- operation_params = operation_params(http_method, controller_class)
45
+ operation_params = operation_params(@action, controller_class)
46
46
 
47
47
  controller_class.instance_eval do
48
48
  swagger_path this.path do
@@ -54,7 +54,15 @@ module WeakSwaggerParameters
54
54
  end
55
55
 
56
56
  def child_definitions
57
- (validation_definitions + @response_definitions).compact
57
+ result = validation_definitions
58
+
59
+ if @response_definitions.empty?
60
+ result << WeakSwaggerParameters::Definitions::Response.new(200, 'Success')
61
+ else
62
+ result += @response_definitions
63
+ end
64
+
65
+ result
58
66
  end
59
67
 
60
68
  private
@@ -63,11 +71,11 @@ module WeakSwaggerParameters
63
71
  [@param_definition].compact
64
72
  end
65
73
 
66
- def operation_params(method, controller_class)
74
+ def operation_params(action, controller_class)
67
75
  name = resource_name(controller_class)
68
76
  {
69
77
  summary: @summary,
70
- operationId: operation_id(method, controller_class),
78
+ operationId: operation_id(action, controller_class),
71
79
  tags: [name]
72
80
  }.tap do |h|
73
81
  h[:description] = @description unless @description.blank?
@@ -78,8 +86,8 @@ module WeakSwaggerParameters
78
86
  controller_class.controller_name.humanize
79
87
  end
80
88
 
81
- def operation_id(method, controller_class)
82
- "#{method}_#{controller_class.controller_name}".camelize(:lower)
89
+ def operation_id(action, controller_class)
90
+ "#{action}_#{controller_class.controller_name}".camelize(:lower)
83
91
  end
84
92
  end
85
93
  end
@@ -9,28 +9,28 @@ module WeakSwaggerParameters
9
9
 
10
10
  def string(name, description, options = {})
11
11
  @required_fields << name if options.try(:[], :required)
12
- @child_definitions << WeakSwaggerParameters::Definitions::BodyParam.new(:string, name, description, options)
12
+ @child_definitions << WeakSwaggerParameters::Definitions::Property.new(:string, name, description, options)
13
13
  end
14
14
 
15
15
  def boolean(name, description, options = {})
16
16
  @required_fields << name if options.try(:[], :required)
17
- @child_definitions << WeakSwaggerParameters::Definitions::BodyParam.new(:boolean, name, description, options)
17
+ @child_definitions << WeakSwaggerParameters::Definitions::Property.new(:boolean, name, description, options)
18
18
  end
19
19
 
20
20
  def integer(name, description, options = {})
21
21
  @required_fields << name if options.try(:[], :required)
22
- @child_definitions << WeakSwaggerParameters::Definitions::BodyParam.new(:integer, name, description, options)
22
+ @child_definitions << WeakSwaggerParameters::Definitions::Property.new(:integer, name, description, options)
23
23
  end
24
24
 
25
25
  def apply_docs(parent_node)
26
26
  param_definitions = @child_definitions
27
- required_fields = @required_fields
27
+ schema_options = {}
28
+ schema_options[:required] = @required_fields unless @required_fields.empty?
28
29
 
29
30
  parent_node.instance_eval do
30
31
  parameter name: :body, in: :body, required: true do
31
- schema required: required_fields do
32
- schema_node = self
33
- param_definitions.each { |definition| definition.apply_docs(schema_node) }
32
+ schema schema_options do
33
+ param_definitions.each { |definition| definition.apply_docs(self) }
34
34
  end
35
35
  end
36
36
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ class CollectionProperty
5
+ def initialize(name, description, model_class)
6
+ @name = name
7
+ @description = description
8
+ @model_class = model_class
9
+ end
10
+
11
+ def apply_docs(parent_node)
12
+ name = @name
13
+ description = @description
14
+ model_class = @model_class
15
+
16
+ parent_node.instance_eval do
17
+ property name, description: description, type: :array do
18
+ items do
19
+ key :'$ref', model_class.docs_model_name
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ class CollectionRef
5
+ def initialize(model_class)
6
+ @model_class = model_class
7
+ end
8
+
9
+ def apply_docs(parent_node)
10
+ model_class = @model_class
11
+
12
+ parent_node.instance_eval do
13
+ schema type: :array do
14
+ items do
15
+ key :'$ref', model_class.docs_model_name
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ class HashProperty
5
+ include WeakSwaggerParameters::Definitions::PropertyContainer
6
+
7
+ def initialize(name, description, &block)
8
+ @name = name
9
+ @description = description
10
+
11
+ instance_eval(&block) if block.present?
12
+ end
13
+
14
+ def apply_docs(parent_node)
15
+ name = @name
16
+ description = @description
17
+ definitions = child_definitions
18
+
19
+ parent_node.instance_eval do
20
+ property name, description: description, type: :object do
21
+ hash_node = self
22
+ definitions.each { |definition| definition.apply_docs(hash_node) }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ class HashRef
5
+ include WeakSwaggerParameters::Definitions::PropertyContainer
6
+
7
+ def initialize(&block)
8
+ instance_eval(&block) if block.present?
9
+ end
10
+
11
+ def apply_docs(parent_node)
12
+ definitions = child_definitions
13
+
14
+ parent_node.instance_eval do
15
+ schema do
16
+ definitions.each { |definition| definition.apply_docs(self) }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ class Model
5
+ include WeakSwaggerParameters::Definitions::PropertyContainer
6
+
7
+ def initialize(model_name, &block)
8
+ @model_name = model_name
9
+
10
+ instance_eval(&block) if block.present?
11
+ end
12
+
13
+ def apply_docs(parent_node)
14
+ model_name = @model_name
15
+ definitions = child_definitions
16
+ schema_options = {}
17
+ schema_options[:required] = required_fields unless required_fields.empty?
18
+
19
+ parent_node.instance_eval do
20
+ swagger_schema model_name, schema_options do
21
+ schema_node = self
22
+ definitions.each { |definition| definition.apply_docs(schema_node) }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ class ModelProperty
5
+ def initialize(name, description, model_class)
6
+ @name = name
7
+ @description = description
8
+ @model_class = model_class
9
+ end
10
+
11
+ def apply_docs(parent_node)
12
+ name = @name
13
+ description = @description
14
+ model_class = @model_class
15
+
16
+ parent_node.instance_eval do
17
+ property name, description: description, type: :object do
18
+ key :'$ref', model_class.docs_model_name
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ class ModelRef
5
+ def initialize(model_class)
6
+ @model_class = model_class
7
+ end
8
+
9
+ def apply_docs(parent_node)
10
+ model_class = @model_class
11
+
12
+ parent_node.instance_eval do
13
+ schema do
14
+ key :'$ref', model_class.docs_model_name
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -21,7 +21,7 @@ module WeakSwaggerParameters
21
21
  description = @description
22
22
 
23
23
  parent_node.instance_eval do
24
- parameter name: name, type: :integer, format: :int32, in: :path, description: description
24
+ parameter name: name, in: :path, required: true, description: description, type: :integer, format: :int32
25
25
  end
26
26
  end
27
27
  end
@@ -21,7 +21,7 @@ module WeakSwaggerParameters
21
21
  description = @description
22
22
 
23
23
  parent_node.instance_eval do
24
- parameter name: name, type: :string, in: :path, description: description
24
+ parameter name: name, in: :path, required: true, description: description, type: :string
25
25
  end
26
26
  end
27
27
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  module WeakSwaggerParameters
3
3
  module Definitions
4
- class BodyParam
4
+ class Property
5
5
  def initialize(type, name, description, options = {})
6
6
  @type = type
7
7
  @name = name
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ module PropertyContainer
5
+ extend ActiveSupport::Concern
6
+
7
+ # rubocop:disable Metrics/BlockLength
8
+ included do
9
+ def string(name, description, options = {})
10
+ register_definition name, options, WeakSwaggerParameters::Definitions::Property.new(:string, name, description, options)
11
+ end
12
+
13
+ def boolean(name, description, options = {})
14
+ register_definition name, options, WeakSwaggerParameters::Definitions::Property.new(:boolean, name, description, options)
15
+ end
16
+
17
+ def integer(name, description, options = {})
18
+ register_definition name, options, WeakSwaggerParameters::Definitions::Property.new(:integer, name, description, options)
19
+ end
20
+
21
+ def hash(name, description, options = {}, &block)
22
+ register_definition name, options, WeakSwaggerParameters::Definitions::HashProperty.new(name, description, &block)
23
+ end
24
+
25
+ def model(name, description, model_class, options = {})
26
+ register_definition name, options, WeakSwaggerParameters::Definitions::ModelProperty.new(name, description, model_class)
27
+ end
28
+
29
+ def collection(name, description, model_class, options = {})
30
+ register_definition name, options, WeakSwaggerParameters::Definitions::CollectionProperty.new(name, description, model_class)
31
+ end
32
+
33
+ private
34
+
35
+ def register_definition(name, options, definition)
36
+ required_fields << name if options.try(:[], :required)
37
+ child_definitions << definition
38
+ end
39
+
40
+ def required_fields
41
+ @required_fields ||= []
42
+ end
43
+
44
+ def child_definitions
45
+ @child_definitions ||= []
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -2,17 +2,37 @@
2
2
  module WeakSwaggerParameters
3
3
  module Definitions
4
4
  class Response
5
- def initialize(status_code, description)
5
+ def initialize(status_code, description, &block)
6
6
  @status_code = status_code
7
7
  @description = description
8
+ @content_schema = nil
9
+
10
+ instance_eval(&block) if block.present?
11
+ end
12
+
13
+ def hash(&block)
14
+ @content_schema = WeakSwaggerParameters::Definitions::HashRef.new(&block)
15
+ end
16
+
17
+ def model(model_class)
18
+ @content_schema = WeakSwaggerParameters::Definitions::ModelRef.new(model_class)
19
+ end
20
+
21
+ def collection(model_class)
22
+ @content_schema = WeakSwaggerParameters::Definitions::CollectionRef.new(model_class)
8
23
  end
9
24
 
10
25
  def apply_docs(parent_node)
11
26
  status_code = @status_code
12
27
  response_options = { description: @description }
28
+ content_schema = @content_schema
13
29
 
14
30
  parent_node.instance_eval do
15
- response status_code, response_options
31
+ response status_code, response_options do
32
+ response_node = self
33
+
34
+ content_schema.apply_docs(response_node) if content_schema.present?
35
+ end
16
36
  end
17
37
  end
18
38
  end
@@ -1,9 +1,17 @@
1
1
  # frozen_string_literal: true
2
- require 'weak_swagger_parameters/definitions/body_param'
3
2
  require 'weak_swagger_parameters/definitions/path_params/integer'
4
3
  require 'weak_swagger_parameters/definitions/path_params/string'
5
4
  require 'weak_swagger_parameters/definitions/query_params/string'
6
5
  require 'weak_swagger_parameters/definitions/param_container'
6
+ require 'weak_swagger_parameters/definitions/property_container'
7
+ require 'weak_swagger_parameters/definitions/hash_property'
8
+ require 'weak_swagger_parameters/definitions/collection_property'
9
+ require 'weak_swagger_parameters/definitions/model_property'
10
+ require 'weak_swagger_parameters/definitions/property'
11
+ require 'weak_swagger_parameters/definitions/collection_ref'
12
+ require 'weak_swagger_parameters/definitions/hash_ref'
13
+ require 'weak_swagger_parameters/definitions/model_ref'
14
+ require 'weak_swagger_parameters/definitions/model'
7
15
  require 'weak_swagger_parameters/definitions/body'
8
16
  require 'weak_swagger_parameters/definitions/path'
9
17
  require 'weak_swagger_parameters/definitions/query'
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Model
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def add_to_doc_section(doc_section)
8
+ @doc_sections ||= []
9
+ @doc_sections << doc_section
10
+ end
11
+
12
+ def in_doc_section?(doc_section)
13
+ (@doc_sections || []).include?(doc_section)
14
+ end
15
+
16
+ def model(&block)
17
+ api_docs = WeakSwaggerParameters::Definitions::Model.new(docs_model_name, &block)
18
+ api_docs.apply_docs(self)
19
+ end
20
+
21
+ def docs_model_name
22
+ name.split('::').join('_')
23
+ end
24
+ end
25
+
26
+ included do
27
+ include Swagger::Blocks
28
+ end
29
+ end
30
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module WeakSwaggerParameters
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
@@ -7,7 +7,7 @@ require 'swagger/blocks'
7
7
  require 'weak_swagger_parameters/version'
8
8
  require 'weak_swagger_parameters/definitions'
9
9
  require 'weak_swagger_parameters/controller'
10
+ require 'weak_swagger_parameters/model'
10
11
 
11
12
  module WeakSwaggerParameters
12
- # Your code goes here...
13
13
  end
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency 'rspec-rails', '~> 3.4'
32
32
  spec.add_development_dependency 'rspec-its', '~> 1.2'
33
33
  spec.add_development_dependency 'rubocop', '~> 0.42'
34
+ spec.add_development_dependency 'json-schema', '~> 2.6'
34
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weak_swagger_parameters
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
  - AgileFreaks
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-07 00:00:00.000000000 Z
11
+ date: 2016-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0.42'
153
+ - !ruby/object:Gem::Dependency
154
+ name: json-schema
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.6'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.6'
153
167
  description: Basic integration between swagger-blocks and weak_parameters
154
168
  email:
155
169
  - office@agilefreaks.com
@@ -172,15 +186,24 @@ files:
172
186
  - lib/weak_swagger_parameters/definitions.rb
173
187
  - lib/weak_swagger_parameters/definitions/api.rb
174
188
  - lib/weak_swagger_parameters/definitions/body.rb
175
- - lib/weak_swagger_parameters/definitions/body_param.rb
189
+ - lib/weak_swagger_parameters/definitions/collection_property.rb
190
+ - lib/weak_swagger_parameters/definitions/collection_ref.rb
191
+ - lib/weak_swagger_parameters/definitions/hash_property.rb
192
+ - lib/weak_swagger_parameters/definitions/hash_ref.rb
193
+ - lib/weak_swagger_parameters/definitions/model.rb
194
+ - lib/weak_swagger_parameters/definitions/model_property.rb
195
+ - lib/weak_swagger_parameters/definitions/model_ref.rb
176
196
  - lib/weak_swagger_parameters/definitions/param_container.rb
177
197
  - lib/weak_swagger_parameters/definitions/params.rb
178
198
  - lib/weak_swagger_parameters/definitions/path.rb
179
199
  - lib/weak_swagger_parameters/definitions/path_params/integer.rb
180
200
  - lib/weak_swagger_parameters/definitions/path_params/string.rb
201
+ - lib/weak_swagger_parameters/definitions/property.rb
202
+ - lib/weak_swagger_parameters/definitions/property_container.rb
181
203
  - lib/weak_swagger_parameters/definitions/query.rb
182
204
  - lib/weak_swagger_parameters/definitions/query_params/string.rb
183
205
  - lib/weak_swagger_parameters/definitions/response.rb
206
+ - lib/weak_swagger_parameters/model.rb
184
207
  - lib/weak_swagger_parameters/version.rb
185
208
  - weak_swagger_parameters.gemspec
186
209
  homepage: https://github.com/Agilefreaks/weak_swagger_parameters
@@ -203,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
226
  version: '0'
204
227
  requirements: []
205
228
  rubyforge_project:
206
- rubygems_version: 2.4.5.1
229
+ rubygems_version: 2.5.1
207
230
  signing_key:
208
231
  specification_version: 4
209
232
  summary: Generate docs and Validate request parameters