Class: Sinatra::SwaggerExposer::Configuration::SwaggerEndpointParameter

Inherits:
Object
  • Object
show all
Includes:
SwaggerConfigurationUtilities, SwaggerParameterValidationHelper
Defined in:
lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb

Constant Summary

Constants included from SwaggerParameterHelper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SwaggerParameterValidationHelper

#check_boolean, #validate_length_parameter, #validate_length_parameters, #validate_limit_parameter, #validate_limit_parameters, #validate_params

Methods included from SwaggerConfigurationUtilities

#check_name, #get_type, #hash_to_swagger, #list_or_none, #ref_to_type, #type_to_s, #white_list_params

Constructor Details

#initialize(name, description, how_to_pass, required, type, params, known_types) ⇒ SwaggerEndpointParameter

Create a new instance

Parameters:

  • name (String)

    the name

  • description (String)

    the description

  • how_to_pass (String)

    how to pass the parameter

  • required (TrueClass)

    if the parameter is required

  • type (String)

    the type name

  • params (Hash)

    parameters

  • known_types (Array<String>)

    known custom types names



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
55
56
57
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 28

def initialize(name, description, how_to_pass, required, type, params, known_types)
  check_name(name)
  @name = name

  if description
    @description = description
  end

  how_to_pass = how_to_pass.to_s
  unless HOW_TO_PASS.include? how_to_pass
    raise SwaggerInvalidException.new("Unknown how to pass value [#{how_to_pass}]#{list_or_none(HOW_TO_PASS, 'registered types')}")
  end
  @how_to_pass = how_to_pass

  if @how_to_pass == HOW_TO_PASS_BODY
    get_type(type, PRIMITIVE_TYPES + known_types)
  else
    get_type(type, PRIMITIVE_TYPES_FOR_NON_BODY)
  end

  unless [true, false].include? required
    raise SwaggerInvalidException.new("Required should be a boolean instead of [#{required}]")
  end
  @required = required

  params = white_list_params(params, PARAMS_LIST, SwaggerTypeProperty::PROPERTIES)
  validate_params(@type == TYPE_ARRAY ? @items : @type, params)
  @default = params[PARAMS_DEFAULT]
  @params = params
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



18
19
20
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 18

def default
  @default
end

#how_to_passObject (readonly)

Returns the value of attribute how_to_pass.



18
19
20
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 18

def how_to_pass
  @how_to_pass
end

#itemsObject (readonly)

Returns the value of attribute items.



18
19
20
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 18

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 18

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



18
19
20
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 18

def params
  @params
end

#requiredObject (readonly)

Returns the value of attribute required.



18
19
20
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 18

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 18

def type
  @type
end

Instance Method Details

#to_sObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 98

def to_s
  {
    :name => @name,
    :in => @how_to_pass,
    :required => @required,
    :type => @type,
    :items => @items,
    :description => @description,
    :params => @params,
  }.to_json
end

#to_swaggerHash

Return the swagger version

Returns:

  • (Hash)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint-parameter.rb', line 62

def to_swagger
  result = {
    :name => @name,
    :in => @how_to_pass,
    :required => @required
  }

  if @type
    if @type == TYPE_ARRAY
      result[:type] = TYPE_ARRAY
      if @items
        if PRIMITIVE_TYPES.include? @items
          result[:items] = {:type => @items}
        else
          result[:schema] = ref_to_type(@items)
        end
      end
    else
      if PRIMITIVE_TYPES.include? @type
        result[:type] = @type
      else
        result[:schema] = ref_to_type(@type)
      end
    end
  end

  if @description
    result[:description] = @description
  end
  unless @params.empty?
    result.merge!(@params)
  end

  result
end