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.

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:



53
54
55
56
57
# File 'lib/swagger/blocks/node.rb', line 53

def version
  return @version if instance_variable_defined?('@version') && @version
  return '2.0' if data.has_key?(:swagger) && data[:swagger] == '2.0'
  raise DeclarationError, "You must specify swagger '2.0'"
end

Class Method Details

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



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

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_jsonObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/swagger/blocks/node.rb', line 18

def as_json
  result = {}

  self.data.each do |key, value|
    if value.is_a?(Node)
      result[key] = value.as_json
    elsif value.is_a?(Array)
      result[key] = []
      value.each { |v| result[key] << (v.respond_to?(:as_json) ? v.as_json : v) }
    elsif is_swagger_2_0? && value.is_a?(Hash)
      result[key] = {}
      value.each_pair {|k, v| result[key][k] = (v.respond_to?(:as_json) ? v.as_json : v) }
    elsif is_swagger_2_0? && key.to_s.eql?('$ref') && (value.to_s !~ %r{^#/|https?://})
      result[key] = "#/definitions/#{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



41
42
43
# File 'lib/swagger/blocks/node.rb', line 41

def data
  @data ||= {}
end

#is_swagger_2_0?Boolean

Returns:

  • (Boolean)


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

def is_swagger_2_0?
  version == '2.0'
end

#key(key, value) ⇒ Object



49
50
51
# File 'lib/swagger/blocks/node.rb', line 49

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

#keys(data) ⇒ Object



45
46
47
# File 'lib/swagger/blocks/node.rb', line 45

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