Class: ReeRoda::BuildSwaggerFromRoutes

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_roda/package/ree_roda/services/build_swagger_from_routes.rb

Instance Method Summary collapse

Instance Method Details

#call(routes, title, description, version, api_url, **opts) ⇒ Object



14
15
16
17
18
19
20
21
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
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ree_lib/packages/ree_roda/package/ree_roda/services/build_swagger_from_routes.rb', line 14

def call(routes, title, description, version, api_url, **opts)
  # Defaults to the global switch (see Ree.hide_internal_swagger_endpoints),
  # so one codebase can serve a public and an internal document.
  include_internal = opts.fetch(:include_internal) {
    !Ree.hide_internal_swagger_endpoints?
  }

  routes = routes.select(&:public?) if !include_internal

  endpoints = routes.map do |route|
    method_decorator = Ree::Contracts.get_method_decorator(
      route.action.klass, :call, scope: :instance
    )

    response_status = case route.request_method
    when :post
      201
    when :put, :delete, :patch
      204
    else
      200
    end

    caster = if route.action.klass.const_defined?(:ActionCaster)
      route.action.klass.const_get(:ActionCaster)
    end

    EndpointDto.new(
      method: route.request_method,
      sections: route.sections,
      respond_to: route.respond_to,
      path: route.path.start_with?("/") ? route.path : "/#{route.path}",
      caster: caster,
      serializer: route.serializer&.klass&.new,
      summary: route.summary,
      authenticate: route.warden_scope != :visitor,
      description: method_decorator&.doc || "",
      response_status: response_status,
      response_description: nil,
      errors: build_route_errors(route)
    )
  end

  build_schema(
    title: title,
    description: description,
    version: version,
    api_url: api_url,
    endpoints: endpoints
  )
end