Class: SoberSwag::Compiler::Paths

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

Overview

Compile multiple routes into a paths set. This basically just aggregates SoberSwag::Controller::Route objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePaths

Set up a new paths compiler with no routes in it.



9
10
11
# File 'lib/sober_swag/compiler/paths.rb', line 9

def initialize
  @routes = []
end

Instance Attribute Details

#routesArray<SoberSwag::Controller::Route> (readonly)

Returns the routes to document.

Returns:



64
65
66
# File 'lib/sober_swag/compiler/paths.rb', line 64

def routes
  @routes
end

Instance Method Details

#add_route(route) ⇒ SoberSwag::Compiler::Paths

Parameters:

Returns:



18
19
20
21
22
# File 'lib/sober_swag/compiler/paths.rb', line 18

def add_route(route)
  @routes << route

  self
end

#compile_route(route, compiler) ⇒ Object (private)



68
69
70
# File 'lib/sober_swag/compiler/paths.rb', line 68

def compile_route(route, compiler)
  SoberSwag::Compiler::Path.new(route, compiler).schema
end

#found_types {|Class| ... } ⇒ Object

Get a list of all types we discovered when compiling the paths.

Yields:

  • (Class)

    all the types found in all the routes described in here.



52
53
54
55
56
57
58
59
60
# File 'lib/sober_swag/compiler/paths.rb', line 52

def found_types
  return enum_for(:found_types) unless block_given?

  routes.each do |route|
    %i[body_class query_class path_params_class].each do |k|
      yield route.public_send(k) if route.public_send(k)
    end
  end
end

#grouped_pathsObject

In the OpenAPI V3 spec, we group action definitions by their path. This helps us do that.



27
28
29
# File 'lib/sober_swag/compiler/paths.rb', line 27

def grouped_paths
  routes.group_by(&:path)
end

#paths_list(compiler) ⇒ Hash

Slightly weird method that gives you a compiled paths list. Since this is only a compiler for paths, it has no idea how to handle types. So, it takes a compiler which it will use to do that for it.

Parameters:

Returns:

  • (Hash)

    a schema for all contained routes.



39
40
41
42
43
44
45
# File 'lib/sober_swag/compiler/paths.rb', line 39

def paths_list(compiler)
  grouped_paths.transform_values do |values|
    values.map { |route|
      [route.method, compile_route(route, compiler)]
    }.to_h
  end
end