Class: Sinatra::SwaggerExposer::SwaggerType

Inherits:
Object
  • Object
show all
Includes:
SwaggerUtilities
Defined in:
lib/sinatra/swagger-exposer/swagger-type.rb

Overview

A type

Constant Summary

Constants included from SwaggerUtilities

Sinatra::SwaggerExposer::SwaggerUtilities::PRIMITIVE_TYPES

Instance Method Summary collapse

Methods included from SwaggerUtilities

#get_type, #hash_to_swagger, #type_to_s, #white_list_params

Constructor Details

#initialize(type_name, type_content, known_types) ⇒ SwaggerType

Returns a new instance of SwaggerType.



14
15
16
17
18
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 14

def initialize(type_name, type_content, known_types)
  @properties = process_properties(type_name, type_content, known_types)
  @required = process_required(type_name, type_content, @properties.keys)
  @example = process_example(type_name, type_content, @properties.keys)
end

Instance Method Details

#check_attribute_empty_or_bad(type_name, type_content, attribute_name, attribute_class) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 63

def check_attribute_empty_or_bad(type_name, type_content, attribute_name, attribute_class)
  if !type_content.key?(attribute_name)
    attribute_class.new
  elsif !type_content[attribute_name].is_a? attribute_class
    raise SwaggerInvalidException.new("Attribute [#{attribute_name}] of #{type_name} is not an hash: #{type_content[attribute_name]}")
  end
end

#process_example(type_name, type_content, properties_names) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 48

def process_example(type_name, type_content, properties_names)
  possible_value = check_attribute_empty_or_bad(type_name, type_content, :example, Hash)
  if possible_value
    possible_value
  else
    type_content[:example].each_pair do |property_name, property_value|
      property_name = property_name.to_s
      unless properties_names.include? property_name
        raise SwaggerInvalidException.new("Example property [#{property_name}] with value [#{property_value}] of [#{type_name}] is unknown, known properties: #{properties_names.join(', ')}")
      end
    end
    type_content[:example]
  end
end

#process_properties(type_name, type_content, known_types) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 20

def process_properties(type_name, type_content, known_types)
  possible_value = check_attribute_empty_or_bad(type_name, type_content, :properties, Hash)
  if possible_value
    possible_value
  else
    result = {}
    type_content[:properties].each_pair do |property_name, property_properties|
      result[property_name.to_s] = SwaggerTypeProperty.new(type_name, property_name, property_properties, known_types)
    end
    result
  end
end

#process_required(type_name, type_content, properties_names) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 33

def process_required(type_name, type_content, properties_names)
  possible_value = check_attribute_empty_or_bad(type_name, type_content, :required, Array)
  if possible_value
    possible_value
  else
    type_content[:required].each do |property_name|
      property_name = property_name.to_s
      unless properties_names.include? property_name
        raise SwaggerInvalidException.new("Required property [#{property_name}] of [#{type_name}] is unknown, known properties: #{properties_names.join(', ')}")
      end
    end
    type_content[:required]
  end
end

#to_sObject



89
90
91
92
93
94
95
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 89

def to_s
  {
      :properties => @properties,
      :required => @required,
      :example => @example,
  }.to_json
end

#to_swaggerObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sinatra/swagger-exposer/swagger-type.rb', line 71

def to_swagger
  result = {}

  unless @properties.empty?
    result[:properties] = hash_to_swagger(@properties)
  end

  unless @required.empty?
    result[:required] = @required
  end

  unless @example.empty?
    result[:example] = @example
  end

  result
end