Class: Sinatra::SwaggerExposer::SwaggerContentCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/swagger-exposer/swagger-content-creator.rb

Overview

Create the swagger content

Instance Method Summary collapse

Constructor Details

#initialize(swagger_info, swagger_types, swagger_endpoints) ⇒ SwaggerContentCreator

Returns a new instance of SwaggerContentCreator.



8
9
10
11
12
# File 'lib/sinatra/swagger-exposer/swagger-content-creator.rb', line 8

def initialize(swagger_info, swagger_types, swagger_endpoints)
  @swagger_info = swagger_info
  @swagger_types = swagger_types
  @swagger_endpoints = swagger_endpoints
end

Instance Method Details

#to_swaggerObject



14
15
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
# File 'lib/sinatra/swagger-exposer/swagger-content-creator.rb', line 14

def to_swagger
  result = {
    swagger: '2.0',
    consumes: ['application/json'],
    produces: ['application/json'],
  }
  if @swagger_info
    result[:info] = @swagger_info.to_swagger
  end

  swagger_types_as_swagger = @swagger_types.to_swagger
  if swagger_types_as_swagger
    result[:definitions] = swagger_types_as_swagger
  end

  unless @swagger_endpoints.empty?
    result_endpoints = {}

    # swagger need the endpoints to be grouped by path
    endpoints_by_path = @swagger_endpoints.group_by { |endpoint| endpoint.path }
    endpoints_by_path.keys.sort.each do |path|
      endpoints = endpoints_by_path[path]

      result_endpoints_for_path = {}
      endpoints.each do |endpoint|
        result_endpoints_for_path[endpoint.type] = endpoint.to_swagger
      end

      result_endpoints[path] = result_endpoints_for_path

    end
    result[:paths] = result_endpoints
  end

  result
end