Class: SwaggerApi::Generator

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

Instance Method Summary collapse

Instance Method Details

#configObject



30
31
32
# File 'lib/swagger_api.rb', line 30

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

#createObject



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

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

#custom_responsesObject



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

def custom_responses
  custom_responses_path = "#{Rails.root.to_s}/config/swagger_custom_responses.json"
  return unless File.exist?(custom_responses_path)
  @custom_responses ||= JSON.parse(File.read(custom_responses_path), object_class: OpenStruct)
end

#default_responsesObject



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
89
90
91
92
93
# File 'lib/swagger_api.rb', line 54

def default_responses
  {
    NotFound: {
      description: "The specified resource was not found",
      content: {
        'application/json; charset=utf-8' => {
          schema: {
            type: 'string',
            example: 'Not Found'
          }
        }
      }
    },
    Unauthorized: {
      description: "Unauthorized",
      content: {
        'application/json; charset=utf-8' => {
          schema: {
            type: 'string',
            example: 'Not Authorized'
          }
        }
      }
    },
    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

#infoObject



95
96
97
98
99
100
101
# File 'lib/swagger_api.rb', line 95

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

#jsonObject



10
11
12
# File 'lib/swagger_api.rb', line 10

def json
  create.to_json
end

#prettifyObject



6
7
8
# File 'lib/swagger_api.rb', line 6

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

#responsesObject



44
45
46
# File 'lib/swagger_api.rb', line 44

def responses
  @responses ||= custom_responses || default_responses
end

#security_schemesObject



34
35
36
37
38
39
40
41
42
# File 'lib/swagger_api.rb', line 34

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

#server_urlObject



103
104
105
106
# File 'lib/swagger_api.rb', line 103

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