Class: Sinatra::SwaggerExposer::SwaggerEndpointParameter

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

Constant Summary collapse

HOW_TO_PASS_BODY =
'body'
HOW_TO_PASS =
['path', 'query', 'header', 'formData'] + [HOW_TO_PASS_BODY]
PRIMITIVE_TYPES_FOR_NON_BODY =
['string', 'number', 'integer', 'boolean']

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(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
51
52
# 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}], registered types are #{HOW_TO_PASS.join(', ')}")
  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

  if params
    white_list_params(params, [:format])
  end
  @params = params

end

Instance Method Details

#to_sObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb', line 90

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

#to_swaggerObject



54
55
56
57
58
59
60
61
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
# File 'lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb', line 54

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' => "#/definitions/#{@items}"}
        end
      end
    else
      if PRIMITIVE_TYPES.include? @type
        result[:type] = @type
      else
        result[:schema] = {'$ref' => "#/definitions/#{@type}"}
      end
    end
  end

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

  result
end