Class: Swagger::Blocks::Node

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

Overview

Base node for representing every object in the Swagger DSL.

Constant Summary collapse

VERSION_2 =
'2.0'
VERSION_3 =
'3.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name


5
6
7
# File 'lib/swagger/blocks/node.rb', line 5

def name
  @name
end

#versionObject

Raises:


86
87
88
89
90
91
# File 'lib/swagger/blocks/node.rb', line 86

def version
  return @version if instance_variable_defined?('@version') && @version
  return VERSION_2 if data.has_key?(:swagger) && data[:swagger] == VERSION_2
  return VERSION_3 if data.has_key?(:openapi) && data[:openapi] == VERSION_3
  raise DeclarationError, "You must specify swagger '#{VERSION_2}' or openapi '#{VERSION_3}'"
end

Class Method Details

.call(options = {}, &block) ⇒ Object


10
11
12
13
14
15
16
17
18
# File 'lib/swagger/blocks/node.rb', line 10

def self.call(options = {}, &block)
  # Create a new instance and evaluate the block into it.
  instance = new
  instance.name = options[:name] if options[:name]
  instance.version = options[:version]
  instance.keys options[:inline_keys]
  instance.instance_eval(&block) if block
  instance
end

Instance Method Details

#as_json(options = {}) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/swagger/blocks/node.rb', line 20

def as_json(options = {})
  version = options.fetch(:version, VERSION_2)

  result = {}
  self.data.each do |key, value|
    if value.is_a?(Node)
      result[key] = value.as_json(version: version)
    elsif value.is_a?(Array)
      result[key] = []
      value.each do |v|
        result[key] << value_as_json(v, version)
      end
    elsif value.is_a?(Hash)
      result[key] = {}
      value.each_pair {|k, v| result[key][k] = value_as_json(v, version) }
    elsif version == VERSION_2 && ref?(key) && !static_ref?(value)
      result[key] = "#/definitions/#{value}"
    elsif version == VERSION_3 && ref?(key) && self.is_a?(Swagger::Blocks::Nodes::LinkNode) && !static_ref?(value)
      result[key] = "#/components/links/#{value}"
    elsif version == VERSION_3 && ref?(key) && self.is_a?(Swagger::Blocks::Nodes::ExampleNode) && !static_ref?(value)
      result[key] = "#/components/examples/#{value}"
    elsif version == VERSION_3 && ref?(key) && self.is_a?(Swagger::Blocks::Nodes::ParameterNode) && !static_ref?(value)
      result[key] = "#/components/parameters/#{value}"
    elsif version == VERSION_3 && ref?(key) && self.is_a?(Swagger::Blocks::Nodes::RequestBodyNode) && !static_ref?(value)
      result[key] = "#/components/requestBodies/#{value}"
    elsif version == VERSION_3 && ref?(key) && self.is_a?(Swagger::Blocks::Nodes::ResponseNode) && !static_ref?(value)
      result[key] = "#/components/responses/#{value}"
    elsif version == VERSION_3 && ref?(key) && !static_ref?(value)
      result[key] = "#/components/schemas/#{value}"
    else
      result[key] = value
    end
  end
  return result if !name
  # If 'name' is given to this node, wrap the data with a root element with the given name.
  {name => result}
end

#dataObject


74
75
76
# File 'lib/swagger/blocks/node.rb', line 74

def data
  @data ||= {}
end

#is_openapi_3_0?Boolean

Returns:

  • (Boolean)

97
98
99
# File 'lib/swagger/blocks/node.rb', line 97

def is_openapi_3_0?
  version == VERSION_3
end

#is_swagger_2_0?Boolean

Returns:

  • (Boolean)

93
94
95
# File 'lib/swagger/blocks/node.rb', line 93

def is_swagger_2_0?
  version == VERSION_2
end

#key(key, value) ⇒ Object


82
83
84
# File 'lib/swagger/blocks/node.rb', line 82

def key(key, value)
  self.data[key] = value
end

#keys(data) ⇒ Object


78
79
80
# File 'lib/swagger/blocks/node.rb', line 78

def keys(data)
  self.data.merge!(data) if data
end

#ref?(key) ⇒ Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/swagger/blocks/node.rb', line 58

def ref?(key)
  key.to_s.eql?('$ref')
end

#static_ref?(value) ⇒ Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/swagger/blocks/node.rb', line 62

def static_ref?(value)
  value.to_s =~ %r{^#/|https?://}
end

#value_as_json(value, version) ⇒ Object


66
67
68
69
70
71
72
# File 'lib/swagger/blocks/node.rb', line 66

def value_as_json(value, version)
  if value.respond_to?(:as_json)
     value.as_json(version: version)
  else
     value
  end
end