Class: Sinatra::SwaggerExposer::SwaggerInfo

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

Overview

The info declaration

Constant Summary collapse

INFO_FIELDS =

Known fields for the info field

{
    :version => String,
    :title => String,
    :description => String,
    :termsOfService => String,
    :contact => {:name => String, :email => String, :url => String},
    :license => {:name  => String, :url => String},
}

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ SwaggerInfo

Returns a new instance of SwaggerInfo.



10
11
12
# File 'lib/sinatra/swagger-exposer/swagger-info.rb', line 10

def initialize(values)
  @values = process(values, 'info', INFO_FIELDS, values)
end

Instance Method Details

#process(current_hash, current_field_name, current_fields, top_level_hash) ⇒ Object

Recursive function



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
# File 'lib/sinatra/swagger-exposer/swagger-info.rb', line 25

def process(current_hash, current_field_name, current_fields, top_level_hash)
  result = {}

  current_hash.each_pair do |current_key, current_value|
    key_sym = current_key.to_sym
    if current_fields.key? key_sym

      field_content = current_fields[key_sym]
      if field_content == String
        if current_value.is_a? String
          result[key_sym] = current_value
        else
          raise SwaggerInvalidException.new("Property [#{current_key}] value [#{current_value}] should be a String for #{current_field_name}: #{top_level_hash}")
        end
      else
        if current_value.is_a? Hash
          sub_params = process(current_value, current_field_name, field_content, top_level_hash)
          if sub_params
            result[key_sym] = sub_params
          end
        else
          raise SwaggerInvalidException.new("Property [#{current_key}] value [#{current_value}] should be a Hash for #{current_field_name}: #{top_level_hash}")
        end
      end
    else
      raise SwaggerInvalidException.new("Unknown property [#{current_key}] for #{current_field_name}, possible keys are: #{current_fields.keys.join(', ')}: #{top_level_hash}")
    end
  end
  result.empty? ? nil : result
end

#to_sObject



60
61
62
# File 'lib/sinatra/swagger-exposer/swagger-info.rb', line 60

def to_s
  @values.to_json
end

#to_swaggerObject



56
57
58
# File 'lib/sinatra/swagger-exposer/swagger-info.rb', line 56

def to_swagger
  @values
end