Class: Sinatra::SwaggerExposer::SwaggerContentCreator

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

Overview

Create the swagger content

Constant Summary

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(swagger_info, swagger_types, swagger_endpoints) ⇒ SwaggerContentCreator

Returns a new instance of SwaggerContentCreator.



12
13
14
15
16
# File 'lib/sinatra/swagger-exposer/swagger-content-creator.rb', line 12

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



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-content-creator.rb', line 18

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

  unless @swagger_types.empty?
    result[:definitions] = hash_to_swagger(@swagger_types)
  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