class SoberSwag::Compiler

Compiler for an entire API.

This compiler has a lot of state as we need to get

Public Class Methods

new() click to toggle source
# File lib/sober_swag/compiler.rb, line 13
def initialize
  @types = Set.new
  @paths = Paths.new
end

Public Instance Methods

add_route(route) click to toggle source

Add a path to be compiled. @param route [SoberSwag::Controller::Route] the route to add. @return [Compiler] self

# File lib/sober_swag/compiler.rb, line 35
def add_route(route)
  tap { @paths.add_route(route) }
end
add_type(type) click to toggle source

Add a type in the types reference dictionary, essentially. @param type [Class] the type to compiler @return [SoberSwag::Compiler] self

# File lib/sober_swag/compiler.rb, line 108
def add_type(type)
  # use tap here to avoid an explicit self at the end of this
  # which makes this method chainable
  tap do
    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
end
body_for(type) click to toggle source

Get the request body definition for a type. This will always be a ref.

@param type [Class] the type to get the body definition for @return [Hash]

# File lib/sober_swag/compiler.rb, line 81
def body_for(type)
  add_type(type)
  Type.new(type).schema_stub
end
object_schemas() click to toggle source

Get the schema of each object type defined in this Compiler.

@return [Hash]

# File lib/sober_swag/compiler.rb, line 43
def object_schemas
  @types.map { |v| [v.ref_name, v.object_schema] }.to_h
end
path_params_for(type) click to toggle source

Compile a type to a new, path-params list. This will add all subtypes to the found types list.

@param type [Class] the type to get a path_params definition for @return [Hash]

# File lib/sober_swag/compiler.rb, line 61
def path_params_for(type)
  with_types_discovered(type).path_schema
end
path_schemas() click to toggle source

The path section of the swagger schema.

@return [Hash]

# File lib/sober_swag/compiler.rb, line 51
def path_schemas
  @paths.paths_list(self)
end
query_params_for(type) click to toggle source

Get the query params list for a type. All found types will be added to the reference dictionary.

@param type [Class] the type to get the query_params definitions for @return [Hash]

# File lib/sober_swag/compiler.rb, line 71
def query_params_for(type)
  with_types_discovered(type).query_schema
end
response_for(type) click to toggle source

Get the definition of a response type.

This is an alias of {#body_for} @see body_for

# File lib/sober_swag/compiler.rb, line 91
def response_for(type)
  body_for(type)
end
schema_for(type) click to toggle source

Get the existing schema for a given type.

@param type [Class] the type to get the schema for @return [Hash,nil] the swagger schema for this object, or nil if it was not found.

# File lib/sober_swag/compiler.rb, line 100
def schema_for(type)
  @types.find { |type_comp| type_comp.type == type }&.object_schema
end
to_swagger() click to toggle source

Convert a compiler to the overall type definition.

@return Hash the swagger definition.

# File lib/sober_swag/compiler.rb, line 22
def to_swagger
  {
    paths: path_schemas,
    components: {
      schemas: object_schemas
    }
  }
end

Private Instance Methods

with_types_discovered(type) click to toggle source
# File lib/sober_swag/compiler.rb, line 128
def with_types_discovered(type)
  Type.new(type).tap do |type_compiler|
    type_compiler.found_types.each { |ft| add_type(ft) }
  end
end