Module: Sinatra::SwaggerExposer::SwaggerUtilities

Includes:
SwaggerParameterHelper
Included in:
SwaggerContentCreator, SwaggerEndpoint, SwaggerEndpointParameter, SwaggerEndpointResponse, SwaggerInfo, SwaggerType, SwaggerTypeProperty
Defined in:
lib/sinatra/swagger-exposer/swagger-utilities.rb

Constant Summary

Constants included from SwaggerParameterHelper

Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_BODY, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_HEADER, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_PATH, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_QUERY, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_DEFAULT, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXAMPLE, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXCLUSIVE_MAXIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXCLUSIVE_MINIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_FORMAT, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_LIST, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MAXIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MAX_LENGTH, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MINIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MIN_LENGTH, Sinatra::SwaggerExposer::SwaggerParameterHelper::PRIMITIVE_TYPES, Sinatra::SwaggerExposer::SwaggerParameterHelper::PRIMITIVE_TYPES_FOR_NON_BODY, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_BOOLEAN, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_BYTE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DATE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DATE_TIME, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DOUBLE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_FILE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_FLOAT, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_INTEGER, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_LONG, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_NUMBER, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_PASSWORD, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_STRING

Instance Method Summary collapse

Instance Method Details

#get_type(type, possible_values) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sinatra/swagger-exposer/swagger-utilities.rb', line 38

def get_type(type, possible_values)
  @type = type
  if type.nil?
    raise SwaggerInvalidException.new('Type is nil')
  elsif type.is_a?(String) || @type.is_a?(Class)
    @type = type_to_s(@type)
    check_type(@type, possible_values)
  elsif @type.is_a? Array
    @items = type_to_s(get_array_type(@type))
    check_type(@items, possible_values)
    @type = 'array'
  else
    raise SwaggerInvalidException.new("Type [#{@type}] of has an unknown type, should be a class, a string or an array")
  end
end

#hash_to_swagger(hash) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/sinatra/swagger-exposer/swagger-utilities.rb', line 16

def hash_to_swagger(hash)
  result = {}
  hash.each_pair do |key, value|
    result[key] = value.to_swagger
  end
  result
end

#list_or_none(list, name) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/sinatra/swagger-exposer/swagger-utilities.rb', line 71

def list_or_none(list, name)
  if list.empty?
    ", no available #{name}"
  else
    ", possible #{name} are #{list.join(', ')}"
  end
end

#ref_to_type(type) ⇒ Object



12
13
14
# File 'lib/sinatra/swagger-exposer/swagger-utilities.rb', line 12

def ref_to_type(type)
  {'$ref' => "#/definitions/#{type}"}
end

#type_to_s(value) ⇒ String

Transform a type into a String

Returns:

  • (String)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sinatra/swagger-exposer/swagger-utilities.rb', line 26

def type_to_s(value)
  if [TrueClass, FalseClass].include? value
    TYPE_BOOLEAN
  elsif value == DateTime
    TYPE_DATE_TIME
  elsif value.is_a? Class
    value.to_s.downcase
  else
    value
  end
end

#white_list_params(params, allowed_values, ignored_values = []) ⇒ Hash

Validate if a parameter is in a list of available values

Parameters:

  • params (Hash)

    the parameters

  • allowed_values (Enumerable, #include?)

    the allowed values

  • ignored_values (Enumerable, #include?) (defaults to: [])

    values to ignore

Returns:

  • (Hash)

    the filtered hash



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sinatra/swagger-exposer/swagger-utilities.rb', line 59

def white_list_params(params, allowed_values, ignored_values = [])
  result = {}
  params.each_pair do |key, value|
    if allowed_values.include? key
      result[key] = value
    elsif !ignored_values.include?(key)
      raise SwaggerInvalidException.new("Unknown property [#{key}] with value [#{value}]#{list_or_none(allowed_values, 'properties')}")
    end
  end
  result
end