Class: SwaggerApi::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_api.rb

Instance Method Summary collapse

Instance Method Details

#componentsObject



27
28
29
30
31
32
33
34
# File 'lib/swagger_api.rb', line 27

def components
  {
    responses: responses,
    schemas: Components.new(controllers: config.controllers).create,
    requestBodies: RequestBodies.new(controllers: config.controllers).create,
    securitySchemes: security_schemes
  }
end

#configObject



36
37
38
# File 'lib/swagger_api.rb', line 36

def config
  @yaml_config ||= JSON.parse(YAML.load_file("#{Rails.root}/config/swagger.yml").to_json, object_class: OpenStruct)
end

#createObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/swagger_api.rb', line 16

def create
  @config ||= {
    openapi: '3.0.0',
    security: [{ api_key: [] }],
    info: info,
    servers: [{ url: server_url }],
    paths: Paths.new(controllers: config.controllers).create,
    components: components
  }
end

#default_bad_request_responseObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/swagger_api.rb', line 90

def default_bad_request_response
  {
    BadRequest: {
      description: 'Bad Request',
      content: {
        'application/json; charset=utf-8' => {
          schema: {
            example: ['The field name is invalid.', 'The id must be present'],
            type: 'array',
            items: {
              type: 'string'
            }
          }
        }
      }
    }
  }
end

#default_not_found_responseObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/swagger_api.rb', line 58

def default_not_found_response
  {
    NotFound: {
      description: 'The specified resource was not found',
      content: {
        'application/json; charset=utf-8' => {
          schema: {
            type: 'string',
            example: 'Not Found'
          }
        }
      }
    }
  }
end

#default_responsesObject



54
55
56
# File 'lib/swagger_api.rb', line 54

def default_responses
  {}.merge(default_not_found_response).merge(default_unauthorized_response).merge(default_bad_request_response)
end

#default_unauthorized_responseObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/swagger_api.rb', line 74

def default_unauthorized_response
  {
    Unauthorized: {
      description: 'Unauthorized',
      content: {
        'application/json; charset=utf-8' => {
          schema: {
            type: 'string',
            example: 'Not Authorized'
          }
        }
      }
    }
  }
end

#infoObject



109
110
111
112
113
114
115
# File 'lib/swagger_api.rb', line 109

def info
  {
    version: config.info.version,
    title: config.info.title,
    description: config.info.description
  }
end

#jsonObject



12
13
14
# File 'lib/swagger_api.rb', line 12

def json
  create.to_json
end

#prettifyObject



8
9
10
# File 'lib/swagger_api.rb', line 8

def prettify
  JSON.pretty_generate(JSON.parse(json))
end

#responsesObject



50
51
52
# File 'lib/swagger_api.rb', line 50

def responses
  @responses ||= default_responses
end

#security_schemesObject



40
41
42
43
44
45
46
47
48
# File 'lib/swagger_api.rb', line 40

def security_schemes
  {
    api_key: {
      type: 'apiKey',
      name: 'Authorization',
      in: 'header'
    }
  }
end

#server_urlObject



117
118
119
120
# File 'lib/swagger_api.rb', line 117

def server_url
  return unless config.servers.respond_to?(Rails.env)
  config.servers.send(Rails.env).url
end