swagger-blocks 1.4.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -6
  3. data/lib/swagger/blocks.rb +28 -775
  4. data/lib/swagger/blocks/class_methods.rb +65 -0
  5. data/lib/swagger/blocks/errors.rb +8 -0
  6. data/lib/swagger/blocks/internal_helpers.rb +52 -0
  7. data/lib/swagger/blocks/node.rb +64 -0
  8. data/lib/swagger/blocks/nodes/all_of_node.rb +42 -0
  9. data/lib/swagger/blocks/nodes/contact_node.rb +9 -0
  10. data/lib/swagger/blocks/nodes/example_node.rb +9 -0
  11. data/lib/swagger/blocks/nodes/external_docs_node.rb +9 -0
  12. data/lib/swagger/blocks/nodes/header_node.rb +12 -0
  13. data/lib/swagger/blocks/nodes/info_node.rb +16 -0
  14. data/lib/swagger/blocks/nodes/items_node.rb +14 -0
  15. data/lib/swagger/blocks/nodes/license_node.rb +9 -0
  16. data/lib/swagger/blocks/nodes/operation_node.rb +30 -0
  17. data/lib/swagger/blocks/nodes/parameter_node.rb +16 -0
  18. data/lib/swagger/blocks/nodes/path_node.rb +24 -0
  19. data/lib/swagger/blocks/nodes/properties_node.rb +11 -0
  20. data/lib/swagger/blocks/nodes/property_node.rb +17 -0
  21. data/lib/swagger/blocks/nodes/response_node.rb +24 -0
  22. data/lib/swagger/blocks/nodes/root_node.rb +53 -0
  23. data/lib/swagger/blocks/nodes/schema_node.rb +30 -0
  24. data/lib/swagger/blocks/nodes/scopes_node.rb +9 -0
  25. data/lib/swagger/blocks/nodes/security_requirement_node.rb +9 -0
  26. data/lib/swagger/blocks/nodes/security_scheme_node.rb +14 -0
  27. data/lib/swagger/blocks/nodes/tag_node.rb +15 -0
  28. data/lib/swagger/blocks/nodes/xml_node.rb +9 -0
  29. data/lib/swagger/blocks/root.rb +25 -0
  30. data/lib/swagger/blocks/version.rb +1 -1
  31. data/spec/lib/swagger_v2_blocks_spec.rb +0 -5
  32. metadata +28 -9
  33. data/README_v1_2.md +0 -143
  34. data/spec/lib/swagger_api_declaration.json +0 -201
  35. data/spec/lib/swagger_blocks_spec.rb +0 -349
  36. data/spec/lib/swagger_resource_listing.json +0 -60
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cdfadc9db4d5a4357533c00d7c1cb23d281d41f6
4
- data.tar.gz: 06820847ade582beb6525e9d3bb9ca253ec0d161
3
+ metadata.gz: a714aa2b6fbb1add356e052e03c827226ef6a401
4
+ data.tar.gz: 36c7a3f585dcbcd8e4b90994a8e99053e651e035
5
5
  SHA512:
6
- metadata.gz: a8945bccc9050739d17f83c2b673fd85cba4b02ed21471e5bc361473779ee138f24aa1fff98095de83579e66b3df0d9e2a8a36b7fd2cb3bb4d4117c108901b65
7
- data.tar.gz: 42f9773eb3c9a71f2851b7a7eeed46d6819130b168c44fa398f23cbbc510c31efe66e13cbfc38982ab11c8a26f2d8f3947831a600e2c6432efd35a1f9f23af84
6
+ metadata.gz: 3cabc6e8842e9e75dfc4d5f5dace3db91d49a37cbac643d0b341abdbd814f34f5f2f51b8621df2e0417f86bc969685bddbbc0aee6cac8e185911128e7f95e516
7
+ data.tar.gz: 9cbf2507bc76247123d13527a6e28f3833029d80d04b58f411fef9a3705be2e0becc8430fe034672dba74d328be71c02f16379b68a5a45176f2357a62412ce8a
data/README.md CHANGED
@@ -11,7 +11,7 @@ It helps you write API docs in the [Swagger](https://helloreverb.com/developers/
11
11
 
12
12
  * Supports **live updating** by design. Change code, refresh your API docs.
13
13
  * **Works with all Ruby web frameworks** including Rails, Sinatra, etc.
14
- * **100% support** for all features of the [Swagger 2.0](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md) and [Swagger 1.2](https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md) specs.
14
+ * **100% support** for all features of the [Swagger 2.0](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md) spec.
15
15
  * Flexible—you can use Swagger::Blocks anywhere, split up blocks to fit your style preferences, etc. Since it's pure Ruby and serves definitions dynamically, you can easily use initializers/config objects to change values or even **show different APIs based on environment**.
16
16
  * 1:1 naming with the Swagger spec—block names and nesting should match almost exactly with the swagger spec, with rare exceptions to make things more convenient.
17
17
 
@@ -31,7 +31,7 @@ Or install directly with `gem install swagger-blocks`.
31
31
 
32
32
  ## Swagger 2.0 example (Rails)
33
33
 
34
- This is a simplified example based on the objects in the Petstore [Swagger Sample App](http://petstore.swagger.wordnik.com/#!/pet). For a more complex and complete example, see the [swagger_v2_blocks_spec.rb](https://github.com/fotinakis/swagger-blocks/blob/master/spec/lib/swagger_v2_blocks_spec.rb) file, or see the [v1.2 docs](https://github.com/fotinakis/swagger-blocks/blob/master/README_v1_2.md).
34
+ This is a simplified example based on the objects in the Petstore [Swagger Sample App](http://petstore.swagger.wordnik.com/#!/pet). For a more complex and complete example, see the [swagger_v2_blocks_spec.rb](https://github.com/fotinakis/swagger-blocks/blob/master/spec/lib/swagger_v2_blocks_spec.rb) file.
35
35
 
36
36
  Also note that **Rails is not required**, you can use Swagger::Blocks in plain Ruby objects.
37
37
 
@@ -439,14 +439,14 @@ operation :post do
439
439
  end
440
440
  ```
441
441
 
442
- ### Swagger 1.2 example (Rails)
443
-
444
- See the [v1.2 docs](https://github.com/fotinakis/swagger-blocks/blob/master/README_v1_2.md).
445
-
446
442
  ## Reference
447
443
 
448
444
  See the [swagger_v2_blocks_spec.rb](https://github.com/fotinakis/swagger-blocks/blob/master/spec/lib/swagger_v2_blocks_spec.rb) for examples of more complex features and declarations possible.
449
445
 
446
+ ### Swagger 1.2
447
+
448
+ The old [Swagger 1.2](https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md) spec is not supported in swagger-blocks >= 2.0.0, but you may use [1.4.0](https://github.com/fotinakis/swagger-blocks/tree/v1.4.0).
449
+
450
450
  ## Contributing
451
451
 
452
452
  1. Fork it ( https://github.com/fotinakis/swagger-blocks/fork )
@@ -471,6 +471,7 @@ Throw a ★ on it! :)
471
471
 
472
472
  ## Release notes
473
473
 
474
+ * v2.0.0: Code cleanup, drop support for Swagger 1.2 spec.
474
475
  * v1.4.0: Allow parameters to be defined once in swagger_root and reused.
475
476
  * v1.3.4: Fix support for fully-qualified URIs in `$ref` values.
476
477
  * v1.3.3: Bugfix to allow `parameter` inside `swagger_path`.
@@ -1,781 +1,34 @@
1
- require 'json'
2
- require 'swagger/blocks/version'
1
+ require 'swagger/blocks/root'
2
+ require 'swagger/blocks/internal_helpers'
3
+ require 'swagger/blocks/class_methods'
4
+ require 'swagger/blocks/errors'
3
5
 
4
6
  module Swagger
5
7
  module Blocks
6
-
7
- # Some custom error classes.
8
- class Error < Exception; end
9
- class DeclarationError < Error; end
10
- class NotFoundError < Error; end
11
- class NotSupportedError < Error; end
12
-
13
- # Inject the swagger_root, swagger_api_root, and swagger_model class methods.
14
- def self.included(base)
15
- base.extend(ClassMethods)
16
- end
17
-
18
- def self.build_root_json(swaggered_classes)
19
- data = Swagger::Blocks::InternalHelpers.parse_swaggered_classes(swaggered_classes)
20
-
21
- if data[:root_node].is_swagger_2_0?
22
- data[:root_node].key(:paths, data[:path_nodes]) # Required, so no empty check.
23
- if data[:schema_nodes] && !data[:schema_nodes].empty?
24
- data[:root_node].key(:definitions, data[:schema_nodes])
25
- end
26
- end
27
-
28
- data[:root_node].as_json
29
- end
30
-
31
- def self.build_api_json(resource_name, swaggered_classes)
32
- data = Swagger::Blocks::InternalHelpers.parse_swaggered_classes(swaggered_classes)
33
- if !data[:root_node].is_swagger_1_2?
34
- raise NotSupportedError.new(
35
- 'build_api_json only supports Swagger 1.2, you do not need to call this method ' +
36
- 'for Swagger >= 2.0 definitions.'
37
- )
38
- end
39
-
40
- api_node = data[:api_node_map][resource_name.to_sym]
41
- raise Swagger::Blocks::NotFoundError.new(
42
- "Not found: swagger_api_root named #{resource_name}") if !api_node
43
-
44
- # Aggregate all model definitions into a new ModelsNode tree and add it to the JSON.
45
- temp_models_node = Swagger::Blocks::ModelsNode.call(name: 'models') { }
46
- data[:models_nodes].each { |models_node| temp_models_node.merge!(models_node) }
47
- result = api_node.as_json
48
- result.merge!(temp_models_node.as_json) if temp_models_node
49
- result
50
- end
51
-
52
- module InternalHelpers
53
- # Return [root_node, api_node_map] from all of the given swaggered_classes.
54
- def self.parse_swaggered_classes(swaggered_classes)
55
- root_nodes = []
56
-
57
- api_node_map = {}
58
- models_nodes = []
59
-
60
- path_node_map = {}
61
- schema_node_map = {}
62
- swaggered_classes.each do |swaggered_class|
63
- next unless swaggered_class.respond_to?(:_swagger_nodes, true)
64
- swagger_nodes = swaggered_class.send(:_swagger_nodes)
65
- root_node = swagger_nodes[:root_node]
66
- root_nodes << root_node if root_node
67
-
68
- # 2.0
69
- if swagger_nodes[:path_node_map]
70
- path_node_map.merge!(swagger_nodes[:path_node_map])
71
- end
72
- if swagger_nodes[:schema_node_map]
73
- schema_node_map.merge!(swagger_nodes[:schema_node_map])
74
- end
75
-
76
- # 1.2
77
- if swagger_nodes[:api_node_map]
78
- api_node_map.merge!(swagger_nodes[:api_node_map])
79
- end
80
- if swagger_nodes[:models_node]
81
- models_nodes << swagger_nodes[:models_node]
82
- end
83
- end
84
- data = {root_node: self.limit_root_node(root_nodes)}
85
- if data[:root_node].is_swagger_2_0?
86
- data[:path_nodes] = path_node_map
87
- data[:schema_nodes] = schema_node_map
88
- else
89
- data[:api_node_map] = api_node_map
90
- data[:models_nodes] = models_nodes
91
- end
92
- data
93
- end
94
-
95
- # Make sure there is exactly one root_node and return it.
96
- # TODO should this merge the contents of the root nodes instead?
97
- def self.limit_root_node(root_nodes)
98
- if root_nodes.length == 0
99
- raise Swagger::Blocks::DeclarationError.new(
100
- 'swagger_root must be declared')
101
- elsif root_nodes.length > 1
102
- raise Swagger::Blocks::DeclarationError.new(
103
- 'Only one swagger_root declaration is allowed.')
104
- end
105
- root_nodes.first
106
- end
107
- end
108
-
109
- module ClassMethods
110
- private
111
-
112
- # v1.2: Defines a Swagger Resource Listing.
113
- # v1.2: http://goo.gl/PvwUXj#51-resource-listing
114
- # v2.0: Defines a Swagger Object
115
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#swagger-object
116
- def swagger_root(inline_keys = nil, &block)
117
- @swagger_root_node ||= Swagger::Blocks::RootNode.call(inline_keys: inline_keys, &block)
118
- end
119
-
120
- # v1.2: Defines a Swagger API Declaration.
121
- # v1.2: http://goo.gl/PvwUXj#52-api-declaration
122
- # v1.2:
123
- # v1.2: @param resource_name [Symbol] An identifier for this API. All swagger_api_root declarations
124
- # v1.2: with the same resource_name will be into a single API root node.
125
- def swagger_api_root(resource_name, inline_keys = nil, &block)
126
- resource_name = resource_name.to_sym
127
-
128
- # Map of path names to ApiDeclarationNodes.
129
- @swagger_api_root_node_map ||= {}
130
-
131
- # Grab a previously declared node if it exists, otherwise create a new ApiDeclarationNode.
132
- # This merges all declarations of swagger_api_root with the same resource_name key.
133
- api_node = @swagger_api_root_node_map[resource_name]
134
- if api_node
135
- # Merge this swagger_api_root declaration into the previous one by the same resource_name.
136
- api_node.instance_eval(&block)
137
- else
138
- # First time we've seen this `swagger_api_root :resource_name`.
139
- api_node = Swagger::Blocks::ApiDeclarationNode.call(version: '1.2', inline_keys: inline_keys, &block)
140
- end
141
-
142
- # Add it into the resource_name to node map (may harmlessly overwrite the same object).
143
- @swagger_api_root_node_map[resource_name] = api_node
144
- end
145
-
146
- # v2.0: Defines a Swagger Path Item object
147
- # https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#path-item-object
148
- def swagger_path(path, &block)
149
- path = path.to_sym
150
-
151
- # TODO enforce that path name begins with a '/'
152
- # (or x- , but need to research Vendor Extensions first)
153
-
154
- @swagger_path_node_map ||= {}
155
-
156
- path_node = @swagger_path_node_map[path]
157
- if path_node
158
- # Merge this path declaration into the previous one
159
- path_node.instance_eval(&block)
160
- else
161
- # First time we've seen this path
162
- @swagger_path_node_map[path] = Swagger::Blocks::PathNode.call(version: '2.0', &block)
163
- end
164
- end
165
-
166
- # v1.2: Defines a Swagger Model.
167
- # v1.2: http://goo.gl/PvwUXj#526-models-object
168
- def swagger_model(name, inline_keys = nil, &block)
169
- @swagger_models_node ||= Swagger::Blocks::ModelsNode.new
170
- @swagger_models_node.version = '1.2'
171
- @swagger_models_node.model(name, inline_keys, &block)
172
- end
173
-
174
- # v2.0: Defines a Swagger Definition Schema,
175
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#definitionsObject and
176
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#schema-object
177
- def swagger_schema(name, inline_keys = nil, &block)
178
- @swagger_schema_node_map ||= {}
179
-
180
- schema_node = @swagger_schema_node_map[name]
181
- if schema_node
182
- # Merge this schema_node declaration into the previous one
183
- schema_node.instance_eval(&block)
184
- else
185
- # First time we've seen this schema_node
186
- @swagger_schema_node_map[name] = Swagger::Blocks::SchemaNode.call(version: '2.0', inline_keys: inline_keys, &block)
187
- end
188
- end
189
-
190
- def _swagger_nodes
191
- # Avoid initialization warnings.
192
- @swagger_root_node ||= nil
193
- @swagger_path_node_map ||= {}
194
- @swagger_schema_node_map ||= nil
195
- @swagger_api_root_node_map ||= {}
196
- @swagger_models_node ||= nil
197
-
198
- data = {root_node: @swagger_root_node}
199
- data[:path_node_map] = @swagger_path_node_map
200
- data[:schema_node_map] = @swagger_schema_node_map
201
- data[:api_node_map] = @swagger_api_root_node_map
202
- data[:models_node] = @swagger_models_node
203
- data
204
- end
205
-
206
- end
207
-
208
- # -----
209
-
210
- # Base node for representing every object in the Swagger DSL.
211
- class Node
212
- attr_accessor :name
213
- attr_writer :version
214
-
215
- def self.call(options = {}, &block)
216
- # Create a new instance and evaluate the block into it.
217
- instance = new
218
- instance.name = options[:name] if options[:name]
219
- instance.version = options[:version]
220
- instance.keys options[:inline_keys]
221
- instance.instance_eval(&block) if block
222
- instance
223
- end
224
-
225
- def as_json
226
- result = {}
227
-
228
- self.data.each do |key, value|
229
- if value.is_a?(Node)
230
- result[key] = value.as_json
231
- elsif value.is_a?(Array)
232
- result[key] = []
233
- value.each { |v| result[key] << (v.respond_to?(:as_json) ? v.as_json : v) }
234
- elsif is_swagger_2_0? && value.is_a?(Hash)
235
- result[key] = {}
236
- value.each_pair {|k, v| result[key][k] = (v.respond_to?(:as_json) ? v.as_json : v) }
237
- elsif is_swagger_2_0? && key.to_s.eql?('$ref') && (value.to_s !~ %r{^#/|https?://})
238
- result[key] = "#/definitions/#{value}"
239
- else
240
- result[key] = value
241
- end
242
- end
243
- return result if !name
244
- # If 'name' is given to this node, wrap the data with a root element with the given name.
245
- {name => result}
246
- end
247
-
248
- def data
249
- @data ||= {}
250
- end
251
-
252
- def keys(data)
253
- self.data.merge!(data) if data
254
- end
255
-
256
- def key(key, value)
257
- self.data[key] = value
258
- end
259
-
260
- def version
261
- return @version if instance_variable_defined?('@version') && @version
262
- if data.has_key?(:swagger) && data[:swagger] == '2.0'
263
- '2.0'
264
- elsif data.has_key?(:swaggerVersion) && data[:swaggerVersion] == '1.2'
265
- '1.2'
266
- else
267
- raise DeclarationError.new("You must specify swaggerVersion '1.2' or swagger '2.0'")
268
- end
269
- end
270
-
271
- def is_swagger_1_2?
272
- version == '1.2'
273
- end
274
-
275
- def is_swagger_2_0?
276
- version == '2.0'
277
- end
278
- end
279
-
280
- class RootNode < Node
281
- def initialize(*args)
282
- # An internal list of the user-defined names that uniquely identify each API tree.
283
- # Only used in Swagger 1.2, but when initializing a root node we haven't seen the
284
- # swaggerVersion/swagger key yet
285
- @api_paths = []
286
- super
287
- end
288
-
289
- def has_api_path?(api_path)
290
- raise NotSupportedError unless is_swagger_1_2?
291
-
292
- api_paths = self.data[:apis].map { |x| x.data[:path] }
293
- api_paths.include?(api_path)
294
- end
295
-
296
- def authorization(name, inline_keys = nil, &block)
297
- raise NotSupportedError unless is_swagger_1_2?
298
-
299
- self.data[:authorizations] ||= Swagger::Blocks::ResourceListingAuthorizationsNode.new
300
- self.data[:authorizations].version = version
301
- self.data[:authorizations].authorization(name, inline_keys, &block)
302
- end
303
-
304
- def info(inline_keys = nil, &block)
305
- self.data[:info] = Swagger::Blocks::InfoNode.call(version: version, inline_keys: inline_keys, &block)
306
- end
307
-
308
- def api(inline_keys = nil, &block)
309
- raise NotSupportedError unless is_swagger_1_2?
310
-
311
- self.data[:apis] ||= []
312
- self.data[:apis] << Swagger::Blocks::ResourceNode.call(version: version, inline_keys: inline_keys ,&block)
313
- end
314
-
315
- def parameter(param, inline_keys = nil, &block)
316
- raise NotSupportedError unless is_swagger_2_0?
317
-
318
- # TODO validate 'param' is as per spec
319
- self.data[:parameters] ||= {}
320
- self.data[:parameters][param] = Swagger::Blocks::ParameterNode.call(version: version, inline_keys: inline_keys, &block)
321
- end
322
-
323
- def response(resp, inline_keys = nil, &block)
324
- raise NotSupportedError unless is_swagger_2_0?
325
-
326
- # TODO validate 'resp' is as per spec
327
- self.data[:responses] ||= {}
328
- self.data[:responses][resp] = Swagger::Blocks::ResponseNode.call(version: version, inline_keys: inline_keys, &block)
329
- end
330
-
331
- def security_definition(name, inline_keys = nil, &block)
332
- raise NotSupportedError unless is_swagger_2_0?
333
-
334
- self.data[:securityDefinitions] ||= {}
335
- self.data[:securityDefinitions][name] = Swagger::Blocks::SecuritySchemeNode.call(version: version, inline_keys: inline_keys, &block)
336
- end
337
-
338
- def security(inline_keys = nil, &block)
339
- raise NotSupportedError unless is_swagger_2_0?
340
-
341
- self.data[:security] ||= []
342
- self.data[:security] << Swagger::Blocks::SecurityRequirementNode.call(version: version, inline_keys: inline_keys, &block)
343
- end
344
-
345
- def tag(inline_keys = nil, &block)
346
- raise NotSupportedError unless is_swagger_2_0?
347
-
348
- self.data[:tags] ||= []
349
- self.data[:tags] << Swagger::Blocks::TagNode.call(version: version, inline_keys: inline_keys, &block)
350
- end
351
-
352
- # Use 'tag' instead.
353
- # @deprecated
354
- alias_method :tags, :tag
355
- end
356
-
357
- # v1.2: http://goo.gl/PvwUXj#512-resource-object
358
- class ResourceNode < Node; end
359
-
360
- # v1.2: NOTE: in the spec this is different than API Declaration authorizations.
361
- # v1.2: http://goo.gl/PvwUXj#514-authorizations-object
362
- class ResourceListingAuthorizationsNode < Node
363
- def authorization(name, inline_keys = nil, &block)
364
- self.data[name] = Swagger::Blocks::ResourceListingAuthorizationNode.call(version: version, inline_keys: inline_keys, &block)
365
- end
366
- end
367
-
368
- # v1.2: NOTE: in the spec this is different than API Declaration authorization.
369
- # v1.2: http://goo.gl/PvwUXj#515-authorization-object
370
- class ResourceListingAuthorizationNode < Node
371
- GRANT_TYPES = [:implicit, :authorization_code].freeze
372
-
373
- def scope(inline_keys = nil, &block)
374
- self.data[:scopes] ||= []
375
- self.data[:scopes] << Swagger::Blocks::ScopeNode.call(version: version, inline_keys: inline_keys, &block)
376
- end
377
-
378
- def grant_type(name, inline_keys = nil, &block)
379
- raise ArgumentError.new("#{name} not in #{GRANT_TYPES}") if !GRANT_TYPES.include?(name)
380
- self.data[:grantTypes] ||= Swagger::Blocks::GrantTypesNode.new
381
- self.data[:grantTypes].version = version
382
- self.data[:grantTypes].implicit(inline_keys, &block) if name == :implicit
383
- self.data[:grantTypes].authorization_code(inline_keys, &block) if name == :authorization_code
384
- end
385
- end
386
-
387
- # v1.2: http://goo.gl/PvwUXj#513-info-object
388
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#infoObject
389
- class InfoNode < Node
390
- def contact(inline_keys = nil, &block)
391
- raise NotSupportedError unless is_swagger_2_0?
392
-
393
- self.data[:contact] = Swagger::Blocks::ContactNode.call(version: version, inline_keys: inline_keys, &block)
394
- end
395
-
396
- def license(inline_keys = nil, &block)
397
- raise NotSupportedError unless is_swagger_2_0?
398
-
399
- self.data[:license] = Swagger::Blocks::LicenseNode.call(version: version, inline_keys: inline_keys, &block)
400
- end
401
- end
402
-
403
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#contact-object
404
- class ContactNode < Node; end
405
-
406
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#license-object
407
- class LicenseNode < Node; end
408
-
409
- # v1.2: http://goo.gl/PvwUXj#516-scope-object
410
- class ScopeNode < Node; end
411
-
412
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#scopes-object
413
- class ScopesNode < Node; end
414
-
415
- # v1.2: http://goo.gl/PvwUXj#517-grant-types-object
416
- class GrantTypesNode < Node
417
- def implicit(inline_keys, &block)
418
- self.data[:implicit] = Swagger::Blocks::ImplicitNode.call(inline_keys: inline_keys, version: version, &block)
419
- end
420
-
421
- def authorization_code(inline_keys, &block)
422
- self.data[:authorization_code] = Swagger::Blocks::AuthorizationCodeNode.call(inline_keys: inline_keys, version: version, &block)
423
- end
424
- end
425
-
426
- # v1.2: http://goo.gl/PvwUXj#518-implicit-object
427
- class ImplicitNode < Node
428
- def login_endpoint(&block)
429
- self.data[:loginEndpoint] = Swagger::Blocks::LoginEndpointNode.call(version: version, &block)
430
- end
431
- end
432
-
433
- # v1.2: http://goo.gl/PvwUXj#5110-login-endpoint-object
434
- class LoginEndpointNode < Node; end
435
-
436
- # v1.2: http://goo.gl/PvwUXj#519-authorization-code-object
437
- class AuthorizationCodeNode < Node
438
- def token_request_endpoint(inline_keys = nil, &block)
439
- self.data[:tokenRequestEndpoint] = Swagger::Blocks::TokenRequestEndpointNode.call(version: version, inline_keys: inline_keys, &block)
440
- end
441
-
442
- def token_endpoint(inline_keys = nil, &block)
443
- self.data[:tokenEndpoint] = Swagger::Blocks::TokenEndpointNode.call(version: version, inline_keys: inline_keys, &block)
444
- end
445
- end
446
-
447
- # v1.2: http://goo.gl/PvwUXj#5111-token-request-endpoint-object
448
- class TokenRequestEndpointNode < Node; end
449
-
450
- # v1.2: http://goo.gl/PvwUXj#5112-token-endpoint-object
451
- class TokenEndpointNode < Node; end
452
-
453
- # -----
454
- # v1.2: Nodes for API Declarations.
455
- # -----
456
-
457
- # v1.2: http://goo.gl/PvwUXj#52-api-declaration
458
- class ApiDeclarationNode < Node
459
- def api(inline_keys = nil, &block)
460
- self.data[:apis] ||= []
461
-
462
- # Important: to conform with the Swagger spec, merge with any previous API declarations
463
- # that have the same :path key. This ensures that operations affecting the same resource
464
- # are all in the same operations node.
465
- #
466
- # http://goo.gl/PvwUXj#522-api-object
467
- # - The API Object describes one or more operations on a single path. In the apis array,
468
- # there MUST be only one API Object per path.
469
- temp_api_node = Swagger::Blocks::ApiNode.call(version: version, inline_keys: inline_keys, &block)
470
- api_node = self.data[:apis].select do |api|
471
- api.data[:path] == temp_api_node.data[:path]
472
- end[0] # Embrace Ruby wtfs.
473
-
474
- if api_node
475
- # Merge this block with the previous ApiNode by the same path key.
476
- api_node.instance_eval(&block)
477
- else
478
- # First time we've seen an api block with the given path key.
479
- self.data[:apis] << temp_api_node
480
- end
481
- end
482
- end
483
-
484
- # v1.2: http://goo.gl/PvwUXj#522-api-object
485
- class ApiNode < Node
486
- def operation(inline_keys = nil, &block)
487
- self.data[:operations] ||= []
488
- self.data[:operations] << Swagger::Blocks::OperationNode.call(version: version, inline_keys: inline_keys, &block)
489
- end
490
- end
491
-
492
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#path-item-object
493
- class PathNode < Node
494
- OPERATION_TYPES = [:get, :put, :post, :delete, :options, :head, :patch].freeze
495
-
496
- # TODO support ^x- Vendor Extensions
497
- def operation(op, inline_keys = nil, &block)
498
- op = op.to_sym
499
- raise ArgumentError.new("#{name} not in #{OPERATION_TYPES}") if !OPERATION_TYPES.include?(op)
500
- self.data[op] = Swagger::Blocks::OperationNode.call(version: version, inline_keys: inline_keys, &block)
501
- end
502
-
503
- def parameter(inline_keys = nil, &block)
504
- inline_keys = {'$ref' => "#/parameters/#{inline_keys}"} if inline_keys.is_a?(Symbol)
505
-
506
- self.data[:parameters] ||= []
507
- self.data[:parameters] << Swagger::Blocks::ParameterNode.call(version: version, inline_keys: inline_keys, &block)
508
- end
509
- end
510
-
511
- # v1.2: http://goo.gl/PvwUXj#523-operation-object
512
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#operation-object
513
- class OperationNode < Node
514
- def parameter(inline_keys = nil, &block)
515
- inline_keys = {'$ref' => "#/parameters/#{inline_keys}"} if inline_keys.is_a?(Symbol)
516
-
517
- self.data[:parameters] ||= []
518
- self.data[:parameters] << Swagger::Blocks::ParameterNode.call(version: version, inline_keys: inline_keys, &block)
519
- end
520
-
521
- def response_message(inline_keys = nil, &block)
522
- raise NotSupportedError unless is_swagger_1_2?
523
-
524
- self.data[:responseMessages] ||= []
525
- self.data[:responseMessages] << Swagger::Blocks::Node.call(version: version, inline_keys: inline_keys, &block)
526
- end
527
-
528
- def authorization(name, inline_keys = nil, &block)
529
- raise NotSupportedError unless is_swagger_1_2?
530
-
531
- self.data[:authorizations] ||= Swagger::Blocks::ApiAuthorizationsNode.new
532
- self.data[:authorizations].version = version
533
- self.data[:authorizations].authorization(name, inline_keys, &block)
534
- end
535
-
536
- def items(inline_keys = nil, &block)
537
- raise NotSupportedError unless is_swagger_1_2?
538
-
539
- self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block)
540
- end
541
-
542
- def response(resp, inline_keys = nil, &block)
543
- raise NotSupportedError unless is_swagger_2_0?
544
-
545
- # TODO validate 'resp' is as per spec
546
- self.data[:responses] ||= {}
547
- self.data[:responses][resp] = Swagger::Blocks::ResponseNode.call(version: version, inline_keys: inline_keys, &block)
548
- end
549
-
550
- def externalDocs(inline_keys = nil, &block)
551
- raise NotSupportedError unless is_swagger_2_0?
552
-
553
- self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, inline_keys: inline_keys, &block)
554
- end
555
-
556
- def security(inline_keys = nil, &block)
557
- raise NotSupportedError unless is_swagger_2_0?
558
-
559
- self.data[:security] ||= []
560
- self.data[:security] << Swagger::Blocks::SecurityRequirementNode.call(version: version, inline_keys: inline_keys, &block)
561
- end
562
- end
563
-
564
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#externalDocumentationObject
565
- class ExternalDocsNode < Node; end
566
-
567
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#securityRequirementObject
568
- class SecurityRequirementNode < Node; end
569
-
570
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#security-scheme-object
571
- class SecuritySchemeNode < Node
572
- # TODO support ^x- Vendor Extensions
573
-
574
- def scopes(inline_keys = nil, &block)
575
- self.data[:scopes] = Swagger::Blocks::ScopesNode.call(version: version, inline_keys: inline_keys, &block)
576
- end
577
- end
578
-
579
- # v1.2: NOTE: in the spec this is different than Resource Listing's authorizations.
580
- # v1.2: http://goo.gl/PvwUXj#514-authorizations-object
581
- class ApiAuthorizationsNode < Node
582
- def authorization(name, inline_keys, &block)
583
- self.data[name] ||= Swagger::Blocks::ApiAuthorizationNode.call(version: version, inline_keys: inline_keys, &block)
584
- end
585
- end
586
-
587
- # v1.2: NOTE: in the spec this is different than Resource Listing's authorization.
588
- # v1.2: http://goo.gl/PvwUXj#515-authorization-object
589
- class ApiAuthorizationNode < Node
590
- def as_json
591
- # Special case: the API Authorization object is weirdly the only array of hashes.
592
- # Override the default hash behavior and return an array.
593
- self.data[:_scopes] ||= []
594
- self.data[:_scopes].map { |s| s.as_json }
595
- end
596
-
597
- def scope(inline_keys = nil, &block)
598
- self.data[:_scopes] ||= []
599
- self.data[:_scopes] << Swagger::Blocks::ApiAuthorizationScopeNode.call(version: version, inline_keys: inline_keys, &block)
600
- end
601
- end
602
-
603
- # v1.2: NOTE: in the spec this is different than Resource Listing's scope object.
604
- # v1.2: http://goo.gl/PvwUXj#5211-scope-object
605
- class ApiAuthorizationScopeNode < Node; end
606
-
607
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#responseObject
608
- class ResponseNode < Node
609
- def schema(inline_keys = nil, &block)
610
- self.data[:schema] = Swagger::Blocks::SchemaNode.call(version: version, inline_keys: inline_keys, &block)
611
- end
612
-
613
- def header(head, inline_keys = nil, &block)
614
- # TODO validate 'head' is as per spec
615
- self.data[:headers] ||= {}
616
- self.data[:headers][head] = Swagger::Blocks::HeaderNode.call(version: version, inline_keys: inline_keys, &block)
617
- end
618
-
619
- def example(exam, inline_keys = nil, &block)
620
- # TODO validate 'exam' is as per spec
621
- self.data[:examples] ||= {}
622
- self.data[:examples][exam] = Swagger::Blocks::ExampleNode.call(version: version, inline_keys: inline_keys, &block)
623
- end
624
- end
625
-
626
- class AllOfNode < Node
627
- def as_json
628
- result = []
629
-
630
- self.data.each do |value|
631
- if value.is_a?(Node)
632
- result << value.as_json
633
- elsif value.is_a?(Array)
634
- r = []
635
- value.each { |v| r << (v.respond_to?(:as_json) ? v.as_json : v) }
636
- result << r
637
- elsif is_swagger_2_0? && value.is_a?(Hash)
638
- r = {}
639
- value.each_pair {|k, v| r[k] = (v.respond_to?(:as_json) ? v.as_json : v) }
640
- result << r
641
- else
642
- result = value
643
- end
644
- end
645
- return result if !name
646
- # If 'name' is given to this node, wrap the data with a root element with the given name.
647
- {name => result}
648
- end
649
-
650
- def data
651
- @data ||= []
652
- end
653
-
654
- def key(key, value)
655
- raise NotSupportedError
656
- end
657
-
658
- def schema(inline_keys = nil, &block)
659
- data << Swagger::Blocks::SchemaNode.call(version: version, inline_keys: inline_keys, &block)
660
- end
661
- end
662
-
663
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#schema-object
664
- class SchemaNode < Node
665
- def items(inline_keys = nil, &block)
666
- self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block)
667
- end
668
-
669
- def allOf(&block)
670
- self.data[:allOf] = Swagger::Blocks::AllOfNode.call(version: version, &block)
671
- end
672
-
673
- def property(name, inline_keys = nil, &block)
674
- self.data[:properties] ||= Swagger::Blocks::PropertiesNode.new
675
- self.data[:properties].version = version
676
- self.data[:properties].property(name, inline_keys, &block)
677
- end
678
-
679
- def xml(inline_keys = nil, &block)
680
- self.data[:xml] = Swagger::Blocks::XmlNode.call(version: version, inline_keys: inline_keys, &block)
681
- end
682
-
683
- def externalDocs(inline_keys = nil, &block)
684
- self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, inline_keys: inline_keys, &block)
685
- end
686
- end
687
-
688
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#headerObject
689
- class HeaderNode < Node
690
- def items(inline_keys = nil, &block)
691
- self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block)
692
- end
693
- end
694
-
695
- # v2.0:
696
- class XmlNode < Node; end
697
-
698
- # v2.0:
699
- class ExampleNode < Node; end
700
-
701
- # v1.2:
702
- # v2.0:
703
- class ItemsNode < Node
704
- def property(name, inline_keys = nil, &block)
705
- self.data[:properties] ||= Swagger::Blocks::PropertiesNode.new
706
- self.data[:properties].version = version
707
- self.data[:properties].property(name, inline_keys, &block)
708
- end
709
- end
710
-
711
- # v1.2: http://goo.gl/PvwUXj#524-parameter-object
712
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#parameter-object
713
- class ParameterNode < Node
714
- def schema(inline_keys = nil, &block)
715
- raise NotSupportedError unless is_swagger_2_0?
716
-
717
- self.data[:schema] = Swagger::Blocks::SchemaNode.call(version: version, inline_keys: inline_keys, &block)
718
- end
719
-
720
- def items(inline_keys = nil, &block)
721
- raise NotSupportedError unless is_swagger_2_0?
722
-
723
- self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block)
724
- end
725
- end
726
-
727
- # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#tag-object
728
- class TagNode < Node
729
-
730
- # TODO support ^x- Vendor Extensions
731
-
732
- def externalDocs(inline_keys = nil, &block)
733
- self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, inline_keys: inline_keys, &block)
734
- end
735
- end
736
-
737
- # -----
738
- # v1.2: Nodes for Models.
739
- # -----
740
-
741
- # v1.2: http://goo.gl/PvwUXj#526-models-object
742
- class ModelsNode < Node
743
- def merge!(other_models_node)
744
- self.data.merge!(other_models_node.data)
745
- end
746
-
747
- def model(name, inline_keys, &block)
748
- self.data[name] ||= Swagger::Blocks::ModelNode.call(version: version, inline_keys: inline_keys, &block)
749
- end
750
- end
751
-
752
- # v1.2: http://goo.gl/PvwUXj#527-model-object
753
- class ModelNode < Node
754
- def property(name, inline_keys = nil, &block)
755
- self.data[:properties] ||= Swagger::Blocks::PropertiesNode.new
756
- self.data[:properties].version = version
757
- self.data[:properties].property(name, inline_keys, &block)
758
- end
759
- end
760
-
761
- # v1.2: http://goo.gl/PvwUXj#527-model-object
762
- class PropertiesNode < Node
763
- def property(name, inline_keys = nil, &block)
764
- self.data[name] = Swagger::Blocks::PropertyNode.call(version: version, inline_keys: inline_keys, &block)
765
- end
766
- end
767
-
768
- # v1.2: http://goo.gl/PvwUXj#527-model-object
769
- class PropertyNode < Node
770
- def items(inline_keys = nil, &block)
771
- self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block)
772
- end
773
-
774
- def property(name, inline_keys = nil, &block)
775
- self.data[:properties] ||= Swagger::Blocks::PropertiesNode.new
776
- self.data[:properties].version = version
777
- self.data[:properties].property(name, inline_keys, &block)
778
- end
8
+ autoload :Node, 'swagger/blocks/node'
9
+
10
+ module Nodes
11
+ autoload :AllOfNode, 'swagger/blocks/nodes/all_of_node'
12
+ autoload :ContactNode, 'swagger/blocks/nodes/contact_node'
13
+ autoload :ExampleNode, 'swagger/blocks/nodes/example_node'
14
+ autoload :ExternalDocsNode, 'swagger/blocks/nodes/external_docs_node'
15
+ autoload :HeaderNode, 'swagger/blocks/nodes/header_node'
16
+ autoload :InfoNode, 'swagger/blocks/nodes/info_node'
17
+ autoload :ItemsNode, 'swagger/blocks/nodes/items_node'
18
+ autoload :LicenseNode, 'swagger/blocks/nodes/license_node'
19
+ autoload :OperationNode, 'swagger/blocks/nodes/operation_node'
20
+ autoload :ParameterNode, 'swagger/blocks/nodes/parameter_node'
21
+ autoload :PathNode, 'swagger/blocks/nodes/path_node'
22
+ autoload :PropertiesNode, 'swagger/blocks/nodes/properties_node'
23
+ autoload :PropertyNode, 'swagger/blocks/nodes/property_node'
24
+ autoload :ResponseNode, 'swagger/blocks/nodes/response_node'
25
+ autoload :RootNode, 'swagger/blocks/nodes/root_node'
26
+ autoload :SchemaNode, 'swagger/blocks/nodes/schema_node'
27
+ autoload :ScopesNode, 'swagger/blocks/nodes/scopes_node'
28
+ autoload :SecurityRequirementNode, 'swagger/blocks/nodes/security_requirement_node'
29
+ autoload :SecuritySchemeNode, 'swagger/blocks/nodes/security_scheme_node'
30
+ autoload :TagNode, 'swagger/blocks/nodes/tag_node'
31
+ autoload :XmlNode, 'swagger/blocks/nodes/xml_node'
779
32
  end
780
33
  end
781
34
  end