weak_swagger_parameters 0.3.0 → 0.4.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: cecd75ac8a8c7da9b795eeaaf4830bcb99670970
4
- data.tar.gz: 115a0037332d2fe62126774588676c6fa98ce27d
3
+ metadata.gz: df79ed08bcdb90cad48b9466283861b1cfd61199
4
+ data.tar.gz: cdbb4a90317df9dcf9695fee1f2a5ec9de516900
5
5
  SHA512:
6
- metadata.gz: 3b23c81c52f4ffe42c1d9a670af103a90fe22338a20cd09477317fe8e90c2fddddd705daaef2fb86f129e2e8227bf197d040acfd7c8117f38ffd244f2bfd19a1
7
- data.tar.gz: 4391e29cdbd8066d920838a26a6439e9d0904b60a1eabfbbe6c1065450f70264bc13eaad71aed114757b980e4808acefca9bc8544ac053045b890d6e5ddf1cd9
6
+ metadata.gz: 8899fb0b89164feceb4fe573dae3447774ba98bbab12ea4dc9b434dfa6866d0e9db8f55995ba1e37f4131a63ee90391721a8ec1915e3d3e07e3f8fe343d6a533
7
+ data.tar.gz: 0507ae0fdadc9294c8c90eef106e3c2b8065d2103b1f149fb2fdfcf72244044d2cffd735261eec289a489c93d8557ce05f59e671e3b08d2e0b07aefd29f495bc
@@ -6,6 +6,7 @@ require 'swagger/blocks'
6
6
 
7
7
  require 'weak_swagger_parameters/version'
8
8
  require 'weak_swagger_parameters/definitions'
9
+ require 'weak_swagger_parameters/services'
9
10
  require 'weak_swagger_parameters/controller'
10
11
  require 'weak_swagger_parameters/model'
11
12
 
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
- require 'weak_swagger_parameters/definitions/path_params/integer'
3
- require 'weak_swagger_parameters/definitions/path_params/string'
4
- require 'weak_swagger_parameters/definitions/query_params/string'
2
+ require 'weak_swagger_parameters/definitions/leaf_definition'
5
3
  require 'weak_swagger_parameters/definitions/param_container'
6
4
  require 'weak_swagger_parameters/definitions/property_container'
7
5
  require 'weak_swagger_parameters/definitions/hash_property'
8
6
  require 'weak_swagger_parameters/definitions/collection_property'
9
7
  require 'weak_swagger_parameters/definitions/model_property'
8
+ require 'weak_swagger_parameters/definitions/parameter'
10
9
  require 'weak_swagger_parameters/definitions/property'
11
10
  require 'weak_swagger_parameters/definitions/collection_ref'
12
11
  require 'weak_swagger_parameters/definitions/hash_ref'
@@ -1,31 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
  module WeakSwaggerParameters
3
3
  module Definitions
4
- class Body < ParamContainer
5
- def initialize(&block)
6
- @required_fields = []
7
- super
8
- end
4
+ class Body
5
+ include WeakSwaggerParameters::Definitions::PropertyContainer
9
6
 
10
- def string(name, description, options = {})
11
- @required_fields << name if options.try(:[], :required)
12
- @child_definitions << WeakSwaggerParameters::Definitions::Property.new(:string, name, description, options)
13
- end
14
-
15
- def boolean(name, description, options = {})
16
- @required_fields << name if options.try(:[], :required)
17
- @child_definitions << WeakSwaggerParameters::Definitions::Property.new(:boolean, name, description, options)
7
+ def initialize(&block)
8
+ instance_eval(&block) if block.present?
18
9
  end
19
10
 
20
- def integer(name, description, options = {})
21
- @required_fields << name if options.try(:[], :required)
22
- @child_definitions << WeakSwaggerParameters::Definitions::Property.new(:integer, name, description, options)
11
+ def apply_validations(parent_node)
12
+ child_definitions.each { |definition| definition.apply_validations(parent_node) }
23
13
  end
24
14
 
25
15
  def apply_docs(parent_node)
26
- param_definitions = @child_definitions
16
+ param_definitions = child_definitions
27
17
  schema_options = {}
28
- schema_options[:required] = @required_fields unless @required_fields.empty?
18
+ schema_options[:required] = required_fields unless required_fields.empty?
29
19
 
30
20
  parent_node.instance_eval do
31
21
  parameter name: :body, in: :body, required: true do
@@ -11,6 +11,17 @@ module WeakSwaggerParameters
11
11
  instance_eval(&block) if block.present?
12
12
  end
13
13
 
14
+ def apply_validations(parent_node)
15
+ name = @name
16
+ param_definitions = child_definitions
17
+
18
+ parent_node.instance_eval do
19
+ hash name, strong: true do
20
+ param_definitions.each { |definition| definition.apply_validations(parent_node) }
21
+ end
22
+ end
23
+ end
24
+
14
25
  def apply_docs(parent_node)
15
26
  name = @name
16
27
  description = @description
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ module LeafDefinition
5
+ def apply_validations(parent_node)
6
+ type = @options[:type]
7
+ name = @options[:name]
8
+
9
+ validation_options = WeakSwaggerParameters::Services::WeakParametersOptionsAdapter.adapt(@options)
10
+ parent_node.instance_eval { send type, name, validation_options }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -10,6 +10,10 @@ module WeakSwaggerParameters
10
10
  instance_eval(&block) if block.present?
11
11
  end
12
12
 
13
+ def apply_validations(parent_node)
14
+ child_definitions.each { |definition| definition.apply_validations(parent_node) }
15
+ end
16
+
13
17
  def apply_docs(parent_node)
14
18
  model_name = @model_name
15
19
  definitions = child_definitions
@@ -8,6 +8,17 @@ module WeakSwaggerParameters
8
8
  @model_class = model_class
9
9
  end
10
10
 
11
+ def apply_validations(parent_node)
12
+ name = @name
13
+ node = @model_class.wsp_node
14
+
15
+ parent_node.instance_eval do
16
+ hash name, strong: true do
17
+ node.apply_validations(parent_node)
18
+ end
19
+ end
20
+ end
21
+
11
22
  def apply_docs(parent_node)
12
23
  name = @name
13
24
  description = @description
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Definitions
4
+ class Parameter
5
+ include WeakSwaggerParameters::Definitions::LeafDefinition
6
+
7
+ def initialize(location, type, name, description, options = {})
8
+ @options = options.merge(location: location, type: type, name: name, description: description)
9
+ end
10
+
11
+ def apply_docs(parent_node)
12
+ parameter_options = WeakSwaggerParameters::Services::SwaggerOptionsAdapter.adapt(@options)
13
+
14
+ parent_node.instance_eval { parameter parameter_options }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -2,12 +2,18 @@
2
2
  module WeakSwaggerParameters
3
3
  module Definitions
4
4
  class Path < ParamContainer
5
- def string(name, description)
6
- @child_definitions << WeakSwaggerParameters::Definitions::PathParams::String.new(name, description)
5
+ def string(name, description, options = {})
6
+ register_definition(:string, name, description, options)
7
7
  end
8
8
 
9
- def integer(name, description)
10
- @child_definitions << WeakSwaggerParameters::Definitions::PathParams::Integer.new(name, description)
9
+ def integer(name, description, options = {})
10
+ register_definition(:integer, name, description, options)
11
+ end
12
+
13
+ private
14
+
15
+ def register_definition(type, name, description, options)
16
+ @child_definitions << WeakSwaggerParameters::Definitions::Parameter.new(:path, type, name, description, options)
11
17
  end
12
18
  end
13
19
  end
@@ -2,54 +2,18 @@
2
2
  module WeakSwaggerParameters
3
3
  module Definitions
4
4
  class Property
5
- def initialize(type, name, description, options = {})
6
- @type = type
7
- @name = name
8
- @description = description
9
- @options = options || {}
10
- end
5
+ include WeakSwaggerParameters::Definitions::LeafDefinition
11
6
 
12
- def apply_validations(parent_node)
13
- type = @type
14
- name = @name
15
- validation_options = {}
16
- validation_options[:strong] = true
17
- validation_options[:required] = @options.key?(:required)
18
- validation_options[:only] = @options[:enum] if @options.key?(:enum)
19
-
20
- parent_node.instance_eval { send type, name, validation_options }
7
+ def initialize(type, name, description, options = {})
8
+ @options = options.merge(name: name, type: type, description: description)
21
9
  end
22
10
 
23
11
  def apply_docs(parent_node)
24
- name = @name
25
- property_options = { description: @description }
26
- property_options[:default] = @options[:default] if @options.key?(:default)
27
- property_options[:enum] = @options[:enum] if @options.key?(:enum)
28
- property_options.merge!(swagger_type_options)
12
+ name = @options[:name]
13
+ property_options = WeakSwaggerParameters::Services::SwaggerOptionsAdapter.adapt(@options.except(:name, :required))
29
14
 
30
15
  parent_node.instance_eval { property name, property_options }
31
16
  end
32
-
33
- private
34
-
35
- # rubocop:disable MethodLength
36
- def swagger_type_options
37
- known_types = {
38
- integer: { type: :integer, format: :int32 },
39
- long: { type: :integer, format: :int64 },
40
- float: { type: :number, format: :float },
41
- double: { type: :number, format: :double },
42
- string: { type: :string },
43
- byte: { type: :string, format: :byte },
44
- binary: { type: :string, format: :binary },
45
- boolean: { type: :boolean },
46
- date: { type: :string, format: :date },
47
- dateTime: { type: :string, format: :'date-time' },
48
- password: { type: :string, format: :password }
49
- }
50
-
51
- known_types[@type]
52
- end
53
17
  end
54
18
  end
55
19
  end
@@ -7,27 +7,27 @@ module WeakSwaggerParameters
7
7
  # rubocop:disable Metrics/BlockLength
8
8
  included do
9
9
  def string(name, description, options = {})
10
- register_definition name, options, WeakSwaggerParameters::Definitions::Property.new(:string, name, description, options)
10
+ register_definition(name, options, WeakSwaggerParameters::Definitions::Property.new(:string, name, description, options))
11
11
  end
12
12
 
13
13
  def boolean(name, description, options = {})
14
- register_definition name, options, WeakSwaggerParameters::Definitions::Property.new(:boolean, name, description, options)
14
+ register_definition(name, options, WeakSwaggerParameters::Definitions::Property.new(:boolean, name, description, options))
15
15
  end
16
16
 
17
17
  def integer(name, description, options = {})
18
- register_definition name, options, WeakSwaggerParameters::Definitions::Property.new(:integer, name, description, options)
18
+ register_definition(name, options, WeakSwaggerParameters::Definitions::Property.new(:integer, name, description, options))
19
19
  end
20
20
 
21
21
  def hash(name, description, options = {}, &block)
22
- register_definition name, options, WeakSwaggerParameters::Definitions::HashProperty.new(name, description, &block)
22
+ register_definition(name, options, WeakSwaggerParameters::Definitions::HashProperty.new(name, description, &block))
23
23
  end
24
24
 
25
25
  def model(name, description, model_class, options = {})
26
- register_definition name, options, WeakSwaggerParameters::Definitions::ModelProperty.new(name, description, model_class)
26
+ register_definition(name, options, WeakSwaggerParameters::Definitions::ModelProperty.new(name, description, model_class))
27
27
  end
28
28
 
29
29
  def collection(name, description, model_class, options = {})
30
- register_definition name, options, WeakSwaggerParameters::Definitions::CollectionProperty.new(name, description, model_class)
30
+ register_definition(name, options, WeakSwaggerParameters::Definitions::CollectionProperty.new(name, description, model_class))
31
31
  end
32
32
 
33
33
  private
@@ -2,8 +2,8 @@
2
2
  module WeakSwaggerParameters
3
3
  module Definitions
4
4
  class Query < ParamContainer
5
- def string(name, description)
6
- @child_definitions << WeakSwaggerParameters::Definitions::QueryParams::String.new(name, description)
5
+ def string(name, description, options = {})
6
+ @child_definitions << WeakSwaggerParameters::Definitions::Parameter.new(:query, :string, name, description, options)
7
7
  end
8
8
  end
9
9
  end
@@ -14,8 +14,12 @@ module WeakSwaggerParameters
14
14
  end
15
15
 
16
16
  def model(&block)
17
- api_docs = WeakSwaggerParameters::Definitions::Model.new(docs_model_name, &block)
18
- api_docs.apply_docs(self)
17
+ @wsp_node = WeakSwaggerParameters::Definitions::Model.new(docs_model_name, &block)
18
+ @wsp_node.apply_docs(self)
19
+ end
20
+
21
+ def wsp_node
22
+ @wsp_node
19
23
  end
20
24
 
21
25
  def docs_model_name
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ require 'weak_swagger_parameters/services/swagger_options_adapter'
3
+ require 'weak_swagger_parameters/services/weak_parameters_options_adapter'
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Services
4
+ class SwaggerOptionsAdapter
5
+ KNOWN_TYPES = {
6
+ integer: { type: :integer, format: :int32 },
7
+ long: { type: :integer, format: :int64 },
8
+ float: { type: :number, format: :float },
9
+ double: { type: :number, format: :double },
10
+ string: { type: :string },
11
+ byte: { type: :string, format: :byte },
12
+ binary: { type: :string, format: :binary },
13
+ boolean: { type: :boolean },
14
+ date: { type: :string, format: :date },
15
+ dateTime: { type: :string, format: :'date-time' },
16
+ password: { type: :string, format: :password }
17
+ }.freeze
18
+
19
+ def self.adapt(options)
20
+ result = {}
21
+ result = result.merge(about_options(options))
22
+ result = result.merge(enum_options(options))
23
+ result = result.merge(default_options(options))
24
+ result = result.merge(type_options(options))
25
+ result = result.merge(required_options(options))
26
+ result = result.merge(range_options(options))
27
+ result
28
+ end
29
+
30
+ def self.type_options(options)
31
+ KNOWN_TYPES[options[:type]]
32
+ end
33
+
34
+ def self.about_options(options)
35
+ result = {}
36
+ result = result.merge(name: options[:name]) if options[:name].present?
37
+ result = result.merge(description: options[:description]) if options[:description].present?
38
+ result = result.merge(in: options[:location]) if options[:location].present?
39
+ result
40
+ end
41
+
42
+ def self.required_options(options)
43
+ result = {}
44
+ result = result.merge(required: true) if options[:location] == :path
45
+ result = result.merge(required: options[:required]) if options[:required]
46
+ result
47
+ end
48
+
49
+ def self.range_options(options)
50
+ result = {}
51
+ result = result.merge(minimum: options[:min]) if options[:min].present?
52
+ result = result.merge(maximum: options[:max]) if options[:max].present?
53
+ result
54
+ end
55
+
56
+ def self.default_options(options)
57
+ options[:default].present? ? { default: options[:default] } : {}
58
+ end
59
+
60
+ def self.enum_options(options)
61
+ options[:enum].present? ? { enum: options[:enum] } : {}
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ module WeakSwaggerParameters
3
+ module Services
4
+ class WeakParametersOptionsAdapter
5
+ MAX_VALUE = (2**(0.size * 8 - 2) - 1)
6
+ MIN_VALUE = -(2**(0.size * 8 - 2))
7
+
8
+ def self.adapt(options)
9
+ result = {}
10
+ result[:strong] = true
11
+ result[:required] = options[:required]
12
+ result[:only] = options[:enum] if options[:enum].present?
13
+ result = result.merge(range_options(options))
14
+
15
+ result
16
+ end
17
+
18
+ def self.range_options(options)
19
+ return {} unless options.key?(:min) || options.key?(:max)
20
+
21
+ max = options[:max] || MAX_VALUE
22
+ min = options[:min] || MIN_VALUE
23
+ { only: min...max }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module WeakSwaggerParameters
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  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.3.0
4
+ version: 0.4.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-28 00:00:00.000000000 Z
11
+ date: 2016-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -190,20 +190,22 @@ files:
190
190
  - lib/weak_swagger_parameters/definitions/collection_ref.rb
191
191
  - lib/weak_swagger_parameters/definitions/hash_property.rb
192
192
  - lib/weak_swagger_parameters/definitions/hash_ref.rb
193
+ - lib/weak_swagger_parameters/definitions/leaf_definition.rb
193
194
  - lib/weak_swagger_parameters/definitions/model.rb
194
195
  - lib/weak_swagger_parameters/definitions/model_property.rb
195
196
  - lib/weak_swagger_parameters/definitions/model_ref.rb
196
197
  - lib/weak_swagger_parameters/definitions/param_container.rb
198
+ - lib/weak_swagger_parameters/definitions/parameter.rb
197
199
  - lib/weak_swagger_parameters/definitions/params.rb
198
200
  - lib/weak_swagger_parameters/definitions/path.rb
199
- - lib/weak_swagger_parameters/definitions/path_params/integer.rb
200
- - lib/weak_swagger_parameters/definitions/path_params/string.rb
201
201
  - lib/weak_swagger_parameters/definitions/property.rb
202
202
  - lib/weak_swagger_parameters/definitions/property_container.rb
203
203
  - lib/weak_swagger_parameters/definitions/query.rb
204
- - lib/weak_swagger_parameters/definitions/query_params/string.rb
205
204
  - lib/weak_swagger_parameters/definitions/response.rb
206
205
  - lib/weak_swagger_parameters/model.rb
206
+ - lib/weak_swagger_parameters/services.rb
207
+ - lib/weak_swagger_parameters/services/swagger_options_adapter.rb
208
+ - lib/weak_swagger_parameters/services/weak_parameters_options_adapter.rb
207
209
  - lib/weak_swagger_parameters/version.rb
208
210
  - weak_swagger_parameters.gemspec
209
211
  homepage: https://github.com/Agilefreaks/weak_swagger_parameters
@@ -226,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
226
228
  version: '0'
227
229
  requirements: []
228
230
  rubyforge_project:
229
- rubygems_version: 2.5.1
231
+ rubygems_version: 2.4.5.1
230
232
  signing_key:
231
233
  specification_version: 4
232
234
  summary: Generate docs and Validate request parameters
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
- module WeakSwaggerParameters
3
- module Definitions
4
- module PathParams
5
- class Integer
6
- def initialize(name, description)
7
- @name = name
8
- @description = description
9
- end
10
-
11
- def apply_validations(parent_node)
12
- name = @name
13
-
14
- parent_node.instance_eval do
15
- integer name, strong: true
16
- end
17
- end
18
-
19
- def apply_docs(parent_node)
20
- name = @name
21
- description = @description
22
-
23
- parent_node.instance_eval do
24
- parameter name: name, in: :path, required: true, description: description, type: :integer, format: :int32
25
- end
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
- module WeakSwaggerParameters
3
- module Definitions
4
- module PathParams
5
- class String
6
- def initialize(name, description)
7
- @name = name
8
- @description = description
9
- end
10
-
11
- def apply_validations(parent_node)
12
- name = @name
13
-
14
- parent_node.instance_eval do
15
- string name, strong: true
16
- end
17
- end
18
-
19
- def apply_docs(parent_node)
20
- name = @name
21
- description = @description
22
-
23
- parent_node.instance_eval do
24
- parameter name: name, in: :path, required: true, description: description, type: :string
25
- end
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
- module WeakSwaggerParameters
3
- module Definitions
4
- module QueryParams
5
- class String
6
- def initialize(name, description)
7
- @name = name
8
- @description = description
9
- end
10
-
11
- def apply_validations(parent_node)
12
- name = @name
13
-
14
- parent_node.instance_eval do
15
- string name, strong: true
16
- end
17
- end
18
-
19
- def apply_docs(parent_node)
20
- name = @name
21
- description = @description
22
-
23
- parent_node.instance_eval do
24
- parameter name: name, type: :string, in: :query, description: description
25
- end
26
- end
27
- end
28
- end
29
- end
30
- end