Class: Explicit::Documentation::Output::Swagger

Inherits:
Object
  • Object
show all
Defined in:
lib/explicit/documentation/output/swagger.rb

Constant Summary collapse

InconsistentBasePathError =
Class.new(RuntimeError)
InconsistentBaseURLError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ Swagger

Returns a new instance of Swagger.



10
11
12
# File 'lib/explicit/documentation/output/swagger.rb', line 10

def initialize(builder)
  @builder = builder
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



8
9
10
# File 'lib/explicit/documentation/output/swagger.rb', line 8

def builder
  @builder
end

Instance Method Details

#call(env) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/explicit/documentation/output/swagger.rb', line 46

def call(env)
  return respond_cors_preflight_request if env["REQUEST_METHOD"] == "OPTIONS"

  @swagger_document ||= swagger_document

  headers = cors_access_control_headers.merge({
    "Content-Type" => "application/json"
  })

  [ 200, headers, [ @swagger_document.to_json ] ]
end

#inspectObject



58
59
60
# File 'lib/explicit/documentation/output/swagger.rb', line 58

def inspect
  "#{self.class.name}#call"
end

#swagger_documentObject



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
# File 'lib/explicit/documentation/output/swagger.rb', line 14

def swagger_document
  paths = build_paths_from_requests

  securitySchemes = {}.tap do |hash|
    requests = paths.flat_map { |path, methods| methods.values }

    if requests.filter { _1.dig(:security, 0, :basicAuth) }.any?
      hash[:basicAuth] = { type: "http", scheme: "basic" }
    end

    if requests.filter { _1.dig(:security, 0, :bearerAuth) }.any?
      hash[:bearerAuth] = { type: "http", scheme: "bearer" }
    end
  end

  {
    openapi: "3.0.1",
    info: {
      title: builder.get_page_title,
      version: builder.get_version
    },
    servers: [
      {
        url: get_base_url
      }
    ],
    tags: build_tags_from_sections,
    paths: build_paths_from_requests,
    components: { securitySchemes: }
  }
end