Class: SoberSwag::Compiler
- Inherits:
-
Object
- Object
- SoberSwag::Compiler
- Defined in:
- lib/sober_swag/compiler.rb,
lib/sober_swag/compiler/path.rb,
lib/sober_swag/compiler/type.rb,
lib/sober_swag/compiler/error.rb,
lib/sober_swag/compiler/paths.rb,
lib/sober_swag/compiler/primitive.rb
Overview
Compiler for an entire API.
This compiler has a lot of state as we need to get
Defined Under Namespace
Classes: Error, Path, Paths, Primitive, Type
Instance Attribute Summary collapse
Instance Method Summary collapse
- #add_dry_type(type) ⇒ Object private
- #add_reporting_type(type) ⇒ Object private
-
#add_route(route) ⇒ Compiler
Add a path to be compiled.
-
#add_type(type) ⇒ SoberSwag::Compiler
Add a type in the types reference dictionary, essentially.
-
#body_for(type) ⇒ Hash
Get the request body definition for a type.
-
#initialize ⇒ Compiler
constructor
A new instance of Compiler.
-
#object_schemas ⇒ Hash
Get the schema of each object type defined in this Compiler.
-
#path_params_for(type) ⇒ Hash
Compile a type to a new, path-params list.
-
#path_schemas ⇒ Hash
The path section of the swagger schema.
-
#query_params_for(type) ⇒ Hash
Get the query params list for a type.
-
#response_for(type) ⇒ Object
Get the definition of a response type.
-
#schema_for(type) ⇒ Hash?
Get the existing schema for a given type.
-
#to_swagger ⇒ Object
Convert a compiler to the overall type definition.
- #with_dry_types_discovered(type) ⇒ Object private
- #with_reporting_types_discovered(type) ⇒ Object private
- #with_types_discovered(type) ⇒ Object private
Constructor Details
Instance Attribute Details
#reporting_types ⇒ SoberSwag::Reporting::Compiler::Schema (readonly)
34 35 36 |
# File 'lib/sober_swag/compiler.rb', line 34 def reporting_types @reporting_types end |
Instance Method Details
#add_dry_type(type) ⇒ Object (private)
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sober_swag/compiler.rb', line 144 def add_dry_type(type) type_compiler = Type.new(type) ## # Do nothing if we already have a type return self if @types.include?(type_compiler) @types.add(type_compiler) if type_compiler.standalone? type_compiler.found_types.each do |ft| add_type(ft) end end |
#add_reporting_type(type) ⇒ Object (private)
158 159 160 |
# File 'lib/sober_swag/compiler.rb', line 158 def add_reporting_type(type) reporting_types.compile(type) end |
#add_route(route) ⇒ Compiler
Add a path to be compiled.
40 41 42 |
# File 'lib/sober_swag/compiler.rb', line 40 def add_route(route) tap { @paths.add_route(route) } end |
#add_type(type) ⇒ SoberSwag::Compiler
Add a type in the types reference dictionary, essentially.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sober_swag/compiler.rb', line 130 def add_type(type) # use tap here to avoid an explicit self at the end of this # which makes this method chainable tap do if type.is_a?(SoberSwag::Reporting::Input::Interface) || type.is_a?(SoberSwag::Reporting::Output::Interface) add_reporting_type(type) else add_dry_type(type) end end end |
#body_for(type) ⇒ Hash
Get the request body definition for a type. This will always be a ref.
100 101 102 103 104 105 106 |
# File 'lib/sober_swag/compiler.rb', line 100 def body_for(type) add_type(type) return reporting_types.compile(type) if type.respond_to?(:swagger_schema) Type.new(type).schema_stub end |
#object_schemas ⇒ Hash
Get the schema of each object type defined in this Compiler.
48 49 50 51 52 |
# File 'lib/sober_swag/compiler.rb', line 48 def object_schemas @types.map { |v| [v.ref_name, v.object_schema] }.to_h.merge( reporting_types.references ) end |
#path_params_for(type) ⇒ Hash
Compile a type to a new, path-params list. This will add all subtypes to the found types list.
68 69 70 71 72 73 74 75 76 |
# File 'lib/sober_swag/compiler.rb', line 68 def path_params_for(type) compiler = with_types_discovered(type) if compiler.respond_to?(:swagger_path_schema) compiler.swagger_path_schema else compiler.path_schema end end |
#path_schemas ⇒ Hash
The path section of the swagger schema.
58 59 60 |
# File 'lib/sober_swag/compiler.rb', line 58 def path_schemas @paths.paths_list(self) end |
#query_params_for(type) ⇒ Hash
Get the query params list for a type. All found types will be added to the reference dictionary.
84 85 86 87 88 89 90 91 92 |
# File 'lib/sober_swag/compiler.rb', line 84 def query_params_for(type) compiler = with_types_discovered(type) if compiler.respond_to?(:swagger_query_schema) compiler.swagger_query_schema else compiler.query_schema end end |
#response_for(type) ⇒ Object
Get the definition of a response type.
This is an alias of #body_for
113 114 115 |
# File 'lib/sober_swag/compiler.rb', line 113 def response_for(type) body_for(type) end |
#schema_for(type) ⇒ Hash?
Get the existing schema for a given type.
122 123 124 |
# File 'lib/sober_swag/compiler.rb', line 122 def schema_for(type) @types.find { |type_comp| type_comp.type == type }&.object_schema end |
#to_swagger ⇒ Object
Convert a compiler to the overall type definition.
23 24 25 26 27 28 29 30 |
# File 'lib/sober_swag/compiler.rb', line 23 def to_swagger { paths: path_schemas, components: { schemas: object_schemas } } end |
#with_dry_types_discovered(type) ⇒ Object (private)
174 175 176 177 178 |
# File 'lib/sober_swag/compiler.rb', line 174 def with_dry_types_discovered(type) Type.new(type).tap do |type_compiler| type_compiler.found_types.each { |ft| add_type(ft) } end end |
#with_reporting_types_discovered(type) ⇒ Object (private)
170 171 172 |
# File 'lib/sober_swag/compiler.rb', line 170 def with_reporting_types_discovered(type) type.tap { |t| reporting_types.compile(t) } end |
#with_types_discovered(type) ⇒ Object (private)
162 163 164 165 166 167 168 |
# File 'lib/sober_swag/compiler.rb', line 162 def with_types_discovered(type) if type.respond_to?(:swagger_schema) with_reporting_types_discovered(type) else with_dry_types_discovered(type) end end |