Class: Sinatra::SwaggerExposer::SwaggerEndpoint

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

Overview

An endpoint

Constant Summary collapse

REGEX_PATH_PARAM_MIDDLE =
/\A(.*\/)\:([a-z]+)\/(.+)\z/
REGEX_PATH_PARAM_END =
/\A(.*)\/:([a-z]+)\z/

Constants included from SwaggerUtilities

Sinatra::SwaggerExposer::SwaggerUtilities::PRIMITIVE_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SwaggerUtilities

#get_type, #hash_to_swagger, #type_to_s, #white_list_params

Constructor Details

#initialize(type, path, parameters, responses, summary, description, tags) ⇒ SwaggerEndpoint

Returns a new instance of SwaggerEndpoint.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 14

def initialize(type, path, parameters, responses, summary, description, tags)
  @type = type
  @path = sinatra_path_to_swagger_path(path)

  @parameters = parameters
  @responses = responses

  @attributes = {}
  if summary
    @attributes[:summary] = summary
  end
  if description
    @attributes[:description] = description
  end
  if tags
    @attributes[:tags] = tags
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#sinatra_path_to_swagger_path(path) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 52

def sinatra_path_to_swagger_path(path)
  while (m = REGEX_PATH_PARAM_MIDDLE.match(path))
    path = "#{m[1]}{#{m[2]}}/#{m[3]}"
  end
  if (m = REGEX_PATH_PARAM_END.match(path))
    path = "#{m[1]}/{#{m[2]}}"
  end
  path
end

#to_sObject



62
63
64
65
66
67
68
69
70
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 62

def to_s
  {
      :type => @type,
      :path => @path,
      :attributes => @attributes,
      :parameters => @parameters,
      :responses => @responses,
  }.to_json
end

#to_swaggerObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 33

def to_swagger
  result = {
      produces: ['application/json'],
  }.merge(@attributes)

  unless @parameters.empty?
    result[:parameters] = @parameters.collect { |parameter| parameter.to_swagger }
  end

  unless @responses.empty?
    result[:responses] = hash_to_swagger(@responses)
  end

  result
end