Class: Swagger::Blocks::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger/blocks.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.



212
213
214
# File 'lib/swagger/blocks.rb', line 212

def name
  @name
end

#versionObject



255
256
257
258
259
260
261
262
263
264
# File 'lib/swagger/blocks.rb', line 255

def version
  return @version if instance_variable_defined?('@version') && @version
  if data.has_key?(:swagger) && data[:swagger] == '2.0'
    '2.0'
  elsif data.has_key?(:swaggerVersion) && data[:swaggerVersion] == '1.2'
    '1.2'
  else
    raise DeclarationError.new("You must specify swaggerVersion '1.2' or swagger '2.0'")
  end
end

Class Method Details

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



215
216
217
218
219
220
221
222
# File 'lib/swagger/blocks.rb', line 215

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.instance_eval(&block)
  instance
end

Instance Method Details

#as_jsonObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/swagger/blocks.rb', line 224

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{^#/definitions/})
      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



247
248
249
# File 'lib/swagger/blocks.rb', line 247

def data
  @data ||= {}
end

#is_swagger_1_2?Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/swagger/blocks.rb', line 266

def is_swagger_1_2?
  version == '1.2'
end

#is_swagger_2_0?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/swagger/blocks.rb', line 270

def is_swagger_2_0?
  version == '2.0'
end

#key(key, value) ⇒ Object



251
252
253
# File 'lib/swagger/blocks.rb', line 251

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