Class: SoberSwag::Compiler::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/sober_swag/compiler/path.rb

Overview

This compiler transforms a SoberSwag::Controller::Route object into its associated OpenAPI V3 definition. These definitions are called "paths" in the OpenAPI V3 spec, thus the name of this compiler.

It only compiles a single "path" at a time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, compiler) ⇒ Path

Returns a new instance of Path.

Parameters:



13
14
15
16
# File 'lib/sober_swag/compiler/path.rb', line 13

def initialize(route, compiler)
  @route = route
  @compiler = compiler
end

Instance Attribute Details

#compilerSoberSwag::Compiler (readonly)

Returns the compiler used for type compilation.

Returns:



24
25
26
# File 'lib/sober_swag/compiler/path.rb', line 24

def compiler
  @compiler
end

#routeSoberSwag::Controller::Route (readonly)



20
21
22
# File 'lib/sober_swag/compiler/path.rb', line 20

def route
  @route
end

Instance Method Details

#paramsArray<Hash>

An array of all parameters, be they in the query or in the path. See this page for what that looks like.

Returns:

  • (Array<Hash>)


69
70
71
# File 'lib/sober_swag/compiler/path.rb', line 69

def params
  query_params + path_params
end

#path_paramsArray<Hash>

An array of schemas for all path parameters.

Returns:

  • (Array<Hash>)

    the schemas



89
90
91
92
93
94
95
# File 'lib/sober_swag/compiler/path.rb', line 89

def path_params
  if route.path_params_class
    compiler.path_params_for(route.path_params_class)
  else
    []
  end
end

#query_paramsArray<Hash>

An array of schemas for all query parameters.

Returns:

  • (Array<Hash>)

    the schemas



77
78
79
80
81
82
83
# File 'lib/sober_swag/compiler/path.rb', line 77

def query_params
  if route.query_params_class
    compiler.query_params_for(route.query_params_class)
  else
    []
  end
end

#request_bodyHash

The schema for a request body. Matches this spec.

Returns:

  • (Hash)

    the schema



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sober_swag/compiler/path.rb', line 102

def request_body
  return nil unless route.request_body_class

  {
    required: true,
    content: {
      'application/json': {
        schema: compiler.body_for(route.request_body_class)
      }
    }
  }
end

#responsesHash{String => Hash}

An array of "response" objects from swagger.

Returns:

  • (Hash{String => Hash})

    response code to response object.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sober_swag/compiler/path.rb', line 46

def responses # rubocop:disable Metrics/MethodLength
  route.response_serializers.map { |status, serializer|
    [
      status.to_s,
      {
        description: route.response_descriptions[status],
        content: {
          'application/json': {
            schema: compiler.response_for(
              serializer.respond_to?(:swagger_schema) ? serializer : serializer.type
            )
          }
        }
      }
    ]
  }.to_h
end

#schemaHash

The OpenAPI V3 "path" object for the associated SoberSwag::Controller::Route

Returns:

  • (Hash)

    the OpenAPI V3 description



30
31
32
33
34
35
36
37
38
39
# File 'lib/sober_swag/compiler/path.rb', line 30

def schema
  base = {}
  base[:summary] = route.summary if route.summary
  base[:description] = route.description if route.description
  base[:parameters] = params if params.any?
  base[:responses] = responses
  base[:requestBody] = request_body if request_body
  base[:tags] = tags if tags
  base
end

#tagsArray<String>

The tags for this path.

Returns:

  • (Array<String>)

    the tags



118
119
120
121
122
# File 'lib/sober_swag/compiler/path.rb', line 118

def tags
  return nil unless route.tags.any?

  route.tags
end