Class: OpenapiFirst::Builder

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

Overview

Builds parts of a Definition This knows how to read a resolved OpenAPI document and build Request and Response objects.

Constant Summary collapse

REQUEST_METHODS =
%w[get head post put patch delete trace options].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolved, config, openapi_version) ⇒ Builder

Returns a new instance of Builder.



17
18
19
20
21
# File 'lib/openapi_first/builder.rb', line 17

def initialize(resolved, config, openapi_version)
  @resolved = resolved
  @config = config
  @openapi_version = openapi_version
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/openapi_first/builder.rb', line 23

def config
  @config
end

#openapi_versionObject (readonly)

Returns the value of attribute openapi_version.



23
24
25
# File 'lib/openapi_first/builder.rb', line 23

def openapi_version
  @openapi_version
end

#resolvedObject (readonly)

Returns the value of attribute resolved.



23
24
25
# File 'lib/openapi_first/builder.rb', line 23

def resolved
  @resolved
end

Class Method Details

.build_router(resolved, config) ⇒ Object

Builds a router from a resolved OpenAPI document.

Parameters:



12
13
14
15
# File 'lib/openapi_first/builder.rb', line 12

def self.build_router(resolved, config)
  openapi_version = (resolved['openapi'] || resolved['swagger'])[0..2]
  new(resolved, config, openapi_version).router
end

Instance Method Details

#build_requests(path, request_method, operation_object, path_item_object) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/openapi_first/builder.rb', line 52

def build_requests(path, request_method, operation_object, path_item_object)
  hooks = config.hooks
  path_item_parameters = path_item_object['parameters']
  parameters = operation_object['parameters'].to_a.chain(path_item_parameters.to_a)
  required_body = operation_object.dig('requestBody', 'required') == true
  result = operation_object.dig('requestBody', 'content')&.map do |content_type, content|
    Request.new(path:, request_method:, operation_object:, parameters:, content_type:,
                content_schema: content['schema'], required_body:, hooks:, openapi_version:)
  end || []
  return result if required_body

  result << Request.new(
    path:, request_method:, operation_object:,
    parameters:, content_type: nil, content_schema: nil,
    required_body:, hooks:, openapi_version:
  )
end

#build_responses(operation_object) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/openapi_first/builder.rb', line 70

def build_responses(operation_object)
  Array(operation_object['responses']).flat_map do |status, response_object|
    headers = response_object['headers']
    response_object['content']&.map do |content_type, content_object|
      content_schema = content_object['schema']
      Response.new(status:, headers:, content_type:, content_schema:, openapi_version:)
    end || Response.new(status:, headers:, content_type: nil,
                        content_schema: nil, openapi_version:)
  end
end

#routerObject

rubocop:disable Metrics/MethodLength



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
# File 'lib/openapi_first/builder.rb', line 25

def router # rubocop:disable Metrics/MethodLength
  router = OpenapiFirst::Router.new
  resolved['paths'].each do |path, path_item_object|
    path_item_object.slice(*REQUEST_METHODS).keys.map do |request_method|
      operation_object = path_item_object[request_method]
      build_requests(path, request_method, operation_object, path_item_object).each do |request|
        router.add_request(
          request,
          request_method:,
          path:,
          content_type: request.content_type
        )
      end
      build_responses(operation_object).each do |response|
        router.add_response(
          response,
          request_method:,
          path:,
          status: response.status,
          response_content_type: response.content_type
        )
      end
    end
  end
  router
end