Module: Meta::SwaggerDocUtil

Defined in:
lib/meta/swagger_doc.rb

Class Method Summary collapse

Class Method Details

.generate(application, info: {}, servers: []) ⇒ Object



6
7
8
9
# File 'lib/meta/swagger_doc.rb', line 6

def generate(application, info: {}, servers: [])
  paths_and_routes = get_paths_and_routes!(application)
  generate_from_paths_and_routes(paths_and_routes, info: info, servers: servers)
end

.generate_from_paths_and_routes(paths_and_routes, info: {}, servers: []) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/meta/swagger_doc.rb', line 11

def generate_from_paths_and_routes(paths_and_routes, info: {}, servers: [])
  schemas = {}
  paths = paths_and_routes.group_by { |path, route| path }.map { |path, routes| [path, routes.map { |item| item[1] }]}.map do |path, routes|
    operations = routes.map do |route|
      [route.method.downcase.to_sym, generate_operation_object(route, schemas)]
    end.to_h

    # path 需要规范化
    path = path.gsub(/[:*](\w+)/, '{\1}')
    [path, operations]
  end.to_h

  doc = {
    openapi: '3.0.0',
    info: info,
    servers: servers,
    paths: paths
  }
  doc[:components] = { schemas: schemas } unless schemas.empty?
  doc
end

.generate_operation_object(route, schemas) ⇒ Object

生成单个路由的文档



34
35
36
# File 'lib/meta/swagger_doc.rb', line 34

def generate_operation_object(route, schemas)
  route.generate_operation_doc(schemas)
end