Class: Rage::OpenAPI::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/openapi/builder.rb

Overview

Build OpenAPI specification for the app. Consists of three steps:

  • Rage::OpenAPI::Builder - build a tree of action nodes;
  • Rage::OpenAPI::Parser - parse OpenAPI tags and save the result into the nodes;
  • Rage::OpenAPI::Converter - convert the tree into an OpenAPI spec;

Defined Under Namespace

Classes: ParsingError

Instance Method Summary collapse

Constructor Details

#initialize(namespace: nil) ⇒ Builder

Returns a new instance of Builder.



14
15
16
17
18
19
20
# File 'lib/rage/openapi/builder.rb', line 14

def initialize(namespace: nil)
  @namespace = namespace.to_s if namespace

  @collectors_cache = {}
  @nodes = Rage::OpenAPI::Nodes::Root.new
  @routes = Rage.__router.routes.group_by { |route| route[:meta][:controller_class] }
end

Instance Method Details

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rage/openapi/builder.rb', line 22

def run
  parser = Rage::OpenAPI::Parser.new

  @routes.each do |controller, routes|
    next if skip_controller?(controller)

    parent_nodes = fetch_ancestors(controller).map do |klass|
      @nodes.new_parent_node(klass) { |node| parser.parse_dangling_comments(node, parse_class(klass).dangling_comments) }
    end

    routes.each do |route|
      action = route[:meta][:action]

      method_comments = fetch_ancestors(controller).filter_map { |klass|
        parse_class(klass).method_comments(action)
      }.first

      method_node = @nodes.new_method_node(controller, action, parent_nodes)
      method_node.http_method, method_node.http_path = route[:method], route[:path]

      parser.parse_method_comments(method_node, method_comments)
    end

  rescue ParsingError
    Rage::OpenAPI.__log_warn "skipping #{controller.name} because of parsing error"
    next
  end

  Rage::OpenAPI::Converter.new(@nodes).run
end