Class: Swagui::JsonSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/swagui/json_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_hash, name) ⇒ JsonSchema

Returns a new instance of JsonSchema.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/swagui/json_schema.rb', line 7

def initialize(schema_hash, name)
  @nested_objects = []

  @name = name
  @schema_hash = schema_hash

  if @schema_hash['type'] == 'array'
    @type = 'array'
    nested_object_name = name
    @nested_objects << JsonSchema.new(@schema_hash['items'], nested_object_name)
    @schema_hash['items'] = {'$ref' => nested_object_name }
  else
    @type = 'class'
    (@schema_hash['properties'] ||= []).each do |pname, pattributes|
      if pattributes['type'] == 'object' || (pattributes['type'] == 'array' && !pattributes['items'].nil? && (pattributes['items']['type'] == 'object'))
        nested_object_name = "#{@name}-#{pname}"
        if pattributes['type'] == 'object'
          @schema_hash['properties'][pname] = {'$ref' => nested_object_name }
        else
          @schema_hash['properties'][pname] = { 'type' => 'array', 'items' => {'$ref' => nested_object_name } }
        end
        @nested_objects << JsonSchema.new(pattributes, nested_object_name)
      end
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/swagui/json_schema.rb', line 5

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/swagui/json_schema.rb', line 4

def type
  @type
end

Instance Method Details

#all_objectsObject



47
48
49
# File 'lib/swagui/json_schema.rb', line 47

def all_objects
  ([self].select {|x| !x.properties.nil?} + @nested_objects.map {|x| x.all_objects}).flatten
end

#array?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/swagui/json_schema.rb', line 51

def array?
  type == 'array'
end

#modelsObject



38
39
40
41
42
43
44
45
# File 'lib/swagui/json_schema.rb', line 38

def models
  all_objects.map do |schema|
    {
      'id' => schema.name,
      'properties' => schema.properties
    }
  end
end

#propertiesObject



34
35
36
# File 'lib/swagui/json_schema.rb', line 34

def properties
  @schema_hash['properties']
end