Class: Sinatra::SwaggerExposer::SwaggerEndpointParameter

Inherits:
Object
  • Object
show all
Includes:
SwaggerParameterHelper, SwaggerUtilities
Defined in:
lib/sinatra/swagger-exposer/swagger-endpoint-parameter.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

Methods included from SwaggerUtilities

#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

Returns a new instance of SwaggerEndpointParameter.



16
17
18
19
20
21
22
23
24
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
# File 'lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb', line 16

def initialize(name, description, how_to_pass, required, type, params, known_types)
  unless name.is_a?(String) || name.is_a?(Symbol)
    raise SwaggerInvalidException.new("Name [#{name}] should be a string or a symbol")
  end
  name = name.to_s
  if name.empty?
    raise SwaggerInvalidException.new('Name should not be empty')
  end
  @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(params)
  @params = params
end

Instance Method Details

#preprocessorSinatra::SwaggerExposer::SwaggerParameterPreprocessor

Create the corresponding SwaggerParameterPreprocessor



61
62
63
# File 'lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb', line 61

def preprocessor
  SwaggerParameterPreprocessor.new(@name, @how_to_pass, @required, @type, @params[:default], @params)
end

#to_sObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb', line 103

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)


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
97
98
99
100
101
# File 'lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb', line 67

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

  if @type
    if @type == 'array'
      result[: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

#validate_params(params) ⇒ Object

Validate parameters

Parameters:

  • params (Hash)


54
55
56
57
# File 'lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb', line 54

def validate_params(params)
  validate_limit_parameters(params)
  validate_length_parameters(params)
end