Class: SoberSwag::Compiler::Path
- Inherits:
-
Object
- Object
- SoberSwag::Compiler::Path
- 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
-
#compiler ⇒ SoberSwag::Compiler
readonly
The compiler used for type compilation.
- #route ⇒ SoberSwag::Controller::Route readonly
Instance Method Summary collapse
-
#initialize(route, compiler) ⇒ Path
constructor
A new instance of Path.
-
#params ⇒ Array<Hash>
An array of all parameters, be they in the query or in the path.
-
#path_params ⇒ Array<Hash>
An array of schemas for all path parameters.
-
#query_params ⇒ Array<Hash>
An array of schemas for all query parameters.
-
#request_body ⇒ Hash
The schema for a request body.
-
#responses ⇒ Hash{String => Hash}
An array of "response" objects from swagger.
-
#schema ⇒ Hash
The OpenAPI V3 "path" object for the associated SoberSwag::Controller::Route.
-
#tags ⇒ Array<String>
The tags for this path.
Constructor Details
#initialize(route, compiler) ⇒ Path
Returns a new instance of Path.
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
#compiler ⇒ SoberSwag::Compiler (readonly)
Returns the compiler used for type compilation.
24 25 26 |
# File 'lib/sober_swag/compiler/path.rb', line 24 def compiler @compiler end |
#route ⇒ SoberSwag::Controller::Route (readonly)
20 21 22 |
# File 'lib/sober_swag/compiler/path.rb', line 20 def route @route end |
Instance Method Details
#params ⇒ Array<Hash>
An array of all parameters, be they in the query or in the path. See this page for what that looks like.
69 70 71 |
# File 'lib/sober_swag/compiler/path.rb', line 69 def params query_params + path_params end |
#path_params ⇒ Array<Hash>
An array of schemas for all path parameters.
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_params ⇒ Array<Hash>
An array of schemas for all query parameters.
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_body ⇒ Hash
The schema for a request body. Matches this spec.
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 |
#responses ⇒ Hash{String => Hash}
An array of "response" objects from swagger.
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 |
#schema ⇒ Hash
The OpenAPI V3 "path" object for the associated SoberSwag::Controller::Route
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] = if base end |
#tags ⇒ Array<String>
The tags for this path.
118 119 120 121 122 |
# File 'lib/sober_swag/compiler/path.rb', line 118 def return nil unless route..any? route. end |