grape-swagger-entity 0.2.1 → 0.2.2

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: '029fa174a6e8b862a95e41092e5eb3f88620d55c'
4
- data.tar.gz: e319381ef0cb0e125d838ec535ba7f76961c10b8
3
+ metadata.gz: 4765712e8fa51a31cb4cde4e6cb6c67e2863f3cb
4
+ data.tar.gz: 2012ba6b40ad94a02afc087e8ddcfe9a2fb341f1
5
5
  SHA512:
6
- metadata.gz: c9be3808f26f6024559d88b9ba1824ec4be407a8d79ae1dc78117ceb565b6a5fddf8fd45683977be26a2fe65a490f80d58610c63a3f88f4d31c135b18630a2f4
7
- data.tar.gz: a9bfcc5f55b02133f4f6ffa3bc17c57285ddb957ee839d68fc1f9c42e030be9e8f38cb5c597d2d7dca9916d865b17992cfabd23e06df5d0c11fbc6ae12198c2b
6
+ metadata.gz: bc1f438ae665e682e20603cd9c1693dce8fc41c243b49706aa4939d0c75034f251e6516329a66ac2dbdb09962ab214dc4411ede6e3d72b05b9cc7aec58c92328
7
+ data.tar.gz: 467778758de82752d19f383bd9be651d06cd6116850ec8d71a21f79e337b822531b49bafbc8d4718940ed3d3b900eaa411cb3c7bb65058c993b65be4c404b3ac
data/.rubocop_todo.yml CHANGED
@@ -46,3 +46,4 @@ Style/Documentation:
46
46
  - 'test/**/*'
47
47
  - 'lib/grape-swagger/entity.rb'
48
48
  - 'lib/grape-swagger/entity/parser.rb'
49
+ - 'lib/grape-swagger/entity/attribute_parser.rb'
data/CHANGELOG.md CHANGED
@@ -8,6 +8,12 @@
8
8
 
9
9
  * Your contribution here.
10
10
 
11
+ ### 0.2.2 (November 3, 2017)
12
+
13
+ #### Features
14
+
15
+ * [#27](https://github.com/ruby-grape/grape-swagger-entity/pull/27): Add support for attribute examples - [@Kukunin](https://github.com/Kukunin).
16
+
11
17
  ### 0.2.1 (Jule 5, 2017)
12
18
 
13
19
  #### Features
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  lib = File.expand_path('../lib', __FILE__)
4
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
3
  require 'grape-swagger/entity/version'
@@ -20,6 +18,6 @@ Gem::Specification.new do |s|
20
18
  s.require_paths = ['lib']
21
19
 
22
20
  s.required_ruby_version = '>= 2.2.6'
23
- s.add_runtime_dependency 'grape-swagger', '>= 0.20.4'
24
21
  s.add_runtime_dependency 'grape-entity', '>= 0.5.0'
22
+ s.add_runtime_dependency 'grape-swagger', '>= 0.20.4'
25
23
  end
@@ -0,0 +1,103 @@
1
+ module GrapeSwagger
2
+ module Entity
3
+ class AttributeParser
4
+ attr_reader :endpoint
5
+
6
+ def initialize(endpoint)
7
+ @endpoint = endpoint
8
+ end
9
+
10
+ def call(entity_options)
11
+ documentation = entity_options[:documentation]
12
+ entity_model = model_from(entity_options)
13
+
14
+ if entity_model
15
+ name = endpoint.nil? ? entity_model.to_s.demodulize : endpoint.send(:expose_params_from_model, entity_model)
16
+ return entity_model_type(name, entity_options)
17
+ else
18
+ param = data_type_from(entity_options)
19
+ return param unless documentation
20
+
21
+ param[:default] = documentation[:default] if documentation[:default]
22
+ add_attribute_example(param, documentation[:example])
23
+
24
+ if (values = documentation[:values])
25
+ param[:enum] = values if values.is_a?(Array)
26
+ end
27
+
28
+ param = { type: :array, items: param } if documentation[:is_array]
29
+ param
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def model_from(entity_options)
36
+ model = entity_options[:using] if entity_options[:using].present?
37
+
38
+ if could_it_be_a_model?(entity_options[:documentation])
39
+ model ||= entity_options[:documentation][:type]
40
+ end
41
+
42
+ model
43
+ end
44
+
45
+ def could_it_be_a_model?(value)
46
+ return false if value.nil?
47
+ direct_model_type?(value[:type]) || ambiguous_model_type?(value[:type])
48
+ end
49
+
50
+ def direct_model_type?(type)
51
+ type.to_s.include?('Entity') || type.to_s.include?('Entities')
52
+ end
53
+
54
+ def ambiguous_model_type?(type)
55
+ type &&
56
+ type.is_a?(Class) &&
57
+ !GrapeSwagger::DocMethods::DataType.primitive?(type.name.downcase) &&
58
+ !type == Array
59
+ end
60
+
61
+ def data_type_from(documentation)
62
+ documented_type = documentation[:type]
63
+ documented_type ||= (documentation[:documentation] && documentation[:documentation][:type])
64
+
65
+ data_type = GrapeSwagger::DocMethods::DataType.call(documented_type)
66
+
67
+ document_data_type(documentation[:documentation], data_type)
68
+ end
69
+
70
+ def document_data_type(documentation, data_type)
71
+ type = if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
72
+ data = GrapeSwagger::DocMethods::DataType.mapping(data_type)
73
+ { type: data.first, format: data.last }
74
+ else
75
+ { type: data_type }
76
+ end
77
+ type[:format] = documentation[:format] if documentation && documentation.key?(:format)
78
+
79
+ type
80
+ end
81
+
82
+ def entity_model_type(name, entity_options)
83
+ if entity_options[:documentation] && entity_options[:documentation][:is_array]
84
+ {
85
+ 'type' => 'array',
86
+ 'items' => {
87
+ '$ref' => "#/definitions/#{name}"
88
+ }
89
+ }
90
+ else
91
+ {
92
+ '$ref' => "#/definitions/#{name}"
93
+ }
94
+ end
95
+ end
96
+
97
+ def add_attribute_example(attribute, example)
98
+ return unless example
99
+ attribute[:example] = example.is_a?(Proc) ? example.call : example
100
+ end
101
+ end
102
+ end
103
+ end
@@ -3,10 +3,12 @@ module GrapeSwagger
3
3
  class Parser
4
4
  attr_reader :model
5
5
  attr_reader :endpoint
6
+ attr_reader :attribute_parser
6
7
 
7
8
  def initialize(model, endpoint)
8
9
  @model = model
9
10
  @endpoint = endpoint
11
+ @attribute_parser = AttributeParser.new(endpoint)
10
12
  end
11
13
 
12
14
  def call
@@ -27,30 +29,12 @@ module GrapeSwagger
27
29
 
28
30
  entity_name = entity_options[:as] if entity_options[:as]
29
31
  documentation = entity_options[:documentation]
30
- entity_model = model_from(entity_options)
31
32
 
32
- if entity_model
33
- name = endpoint.nil? ? entity_model.to_s.demodulize : endpoint.send(:expose_params_from_model, entity_model)
34
- memo[entity_name] = entity_model_type(name, entity_options)
35
- elsif entity_options[:nesting]
36
- memo[entity_name] = parse_nested(entity_name, entity_options, parent_model)
37
- else
38
- memo[entity_name] = data_type_from(entity_options)
39
- next unless documentation
40
-
41
- memo[entity_name][:default] = documentation[:default] if documentation[:default]
42
-
43
- if (values = documentation[:values])
44
- memo[entity_name][:enum] = values if values.is_a?(Array)
45
- end
46
-
47
- if documentation[:is_array]
48
- memo[entity_name] = {
49
- type: :array,
50
- items: memo.delete(entity_name)
51
- }
52
- end
53
- end
33
+ memo[entity_name] = if entity_options[:nesting]
34
+ parse_nested(entity_name, entity_options, parent_model)
35
+ else
36
+ attribute_parser.call(entity_options)
37
+ end
54
38
 
55
39
  if documentation
56
40
  memo[entity_name][:read_only] = documentation[:read_only].to_s == 'true' if documentation[:read_only]
@@ -59,16 +43,6 @@ module GrapeSwagger
59
43
  end
60
44
  end
61
45
 
62
- def model_from(entity_options)
63
- model = entity_options[:using] if entity_options[:using].present?
64
-
65
- if could_it_be_a_model?(entity_options[:documentation])
66
- model ||= entity_options[:documentation][:type]
67
- end
68
-
69
- model
70
- end
71
-
72
46
  def parse_nested(entity_name, entity_options, parent_model = nil)
73
47
  nested_entity = if parent_model.nil?
74
48
  model.root_exposures.find_by(entity_name)
@@ -101,58 +75,6 @@ module GrapeSwagger
101
75
  }
102
76
  end
103
77
  end
104
-
105
- def could_it_be_a_model?(value)
106
- return false if value.nil?
107
- direct_model_type?(value[:type]) || ambiguous_model_type?(value[:type])
108
- end
109
-
110
- def direct_model_type?(type)
111
- type.to_s.include?('Entity') || type.to_s.include?('Entities')
112
- end
113
-
114
- def ambiguous_model_type?(type)
115
- type &&
116
- type.is_a?(Class) &&
117
- !GrapeSwagger::DocMethods::DataType.primitive?(type.name.downcase) &&
118
- !type == Array
119
- end
120
-
121
- def data_type_from(documentation)
122
- documented_type = documentation[:type]
123
- documented_type ||= (documentation[:documentation] && documentation[:documentation][:type])
124
-
125
- data_type = GrapeSwagger::DocMethods::DataType.call(documented_type)
126
-
127
- document_data_type(documentation[:documentation], data_type)
128
- end
129
-
130
- def document_data_type(documentation, data_type)
131
- type = if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
132
- data = GrapeSwagger::DocMethods::DataType.mapping(data_type)
133
- { type: data.first, format: data.last }
134
- else
135
- { type: data_type }
136
- end
137
- type[:format] = documentation[:format] if documentation && documentation.key?(:format)
138
-
139
- type
140
- end
141
-
142
- def entity_model_type(name, entity_options)
143
- if entity_options[:documentation] && entity_options[:documentation][:is_array]
144
- {
145
- 'type' => 'array',
146
- 'items' => {
147
- '$ref' => "#/definitions/#{name}"
148
- }
149
- }
150
- else
151
- {
152
- '$ref' => "#/definitions/#{name}"
153
- }
154
- end
155
- end
156
78
  end
157
79
  end
158
80
  end
@@ -1,5 +1,5 @@
1
1
  module GrapeSwagger
2
2
  module Entity
3
- VERSION = '0.2.1'.freeze
3
+ VERSION = '0.2.2'.freeze
4
4
  end
5
5
  end
@@ -2,6 +2,7 @@ require 'grape-swagger'
2
2
  require 'grape-entity'
3
3
 
4
4
  require 'grape-swagger/entity/version'
5
+ require 'grape-swagger/entity/attribute_parser'
5
6
  require 'grape-swagger/entity/parser'
6
7
 
7
8
  module GrapeSwagger
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-swagger-entity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Zaitsev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-05 00:00:00.000000000 Z
11
+ date: 2017-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: grape-swagger
14
+ name: grape-entity
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.20.4
19
+ version: 0.5.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.20.4
26
+ version: 0.5.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: grape-entity
28
+ name: grape-swagger
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.5.0
33
+ version: 0.20.4
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.5.0
40
+ version: 0.20.4
41
41
  description:
42
42
  email:
43
43
  - kirik910@gmail.com
@@ -61,6 +61,7 @@ files:
61
61
  - grape-swagger-entity.gemspec
62
62
  - lib/grape-swagger-entity.rb
63
63
  - lib/grape-swagger/entity.rb
64
+ - lib/grape-swagger/entity/attribute_parser.rb
64
65
  - lib/grape-swagger/entity/parser.rb
65
66
  - lib/grape-swagger/entity/version.rb
66
67
  homepage: https://github.com/ruby-grape/grape-swagger-entity