Class: Swagger::Blocks::ApiDeclarationNode

Inherits:
Node
  • Object
show all
Defined in:
lib/swagger/blocks.rb

Overview

Instance Attribute Summary

Attributes inherited from Node

#name

Instance Method Summary collapse

Methods inherited from Node

#as_json, call, #data, #key

Instance Method Details

#api(&block) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/swagger/blocks.rb', line 276

def api(&block)
  self.data[:apis] ||= []

  # Important: to conform with the Swagger spec, merge with any previous API declarations
  # that have the same :path key. This ensures that operations affecting the same resource
  # are all in the same operations node.
  #
  # http://goo.gl/PvwUXj#522-api-object
  # - The API Object describes one or more operations on a single path. In the apis array,
  #   there MUST be only one API Object per path.
  temp_api_node = Swagger::Blocks::ApiNode.call(&block)
  api_node = self.data[:apis].select do |api|
    api.data[:path] == temp_api_node.data[:path]
  end[0]  # Embrace Ruby wtfs.

  if api_node
    # Merge this block with the previous ApiNode by the same path key.
    api_node.instance_eval(&block)
  else
    # First time we've seen an api block with the given path key.
    self.data[:apis] << temp_api_node
  end
end