Class: OpenapiFirst::Definition

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

Overview

Represents an OpenAPI API Description document

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolved, filepath) ⇒ Definition

Returns a new instance of Definition.



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

def initialize(resolved, filepath)
  @filepath = filepath
  @paths = resolved['paths']
  @openapi_version = detect_version(resolved)
end

Instance Attribute Details

#filepathObject (readonly)

Returns the value of attribute filepath.



9
10
11
# File 'lib/openapi_first/definition.rb', line 9

def filepath
  @filepath
end

#openapi_versionObject (readonly)

Returns the value of attribute openapi_version.



9
10
11
# File 'lib/openapi_first/definition.rb', line 9

def openapi_version
  @openapi_version
end

#pathsObject (readonly)

Returns the value of attribute paths.



9
10
11
# File 'lib/openapi_first/definition.rb', line 9

def paths
  @paths
end

Instance Method Details

#find_path_item_and_params(request_path) ⇒ Object

Parameters:

  • request_path

    String



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/openapi_first/definition.rb', line 18

def find_path_item_and_params(request_path)
  matches = paths.each_with_object([]) do |kv, result|
    path, path_item_object = kv
    template = Mustermann::Template.new(path)
    path_params = template.params(request_path)
    next unless path_params

    path_item = PathItem.new(path, path_item_object, openapi_version:)
    result << [path_item, path_params]
  end
  # Thanks to open ota42y/openapi_parser for this part
  matches.min_by { |match| match[1].size }
end

#operationsObject



32
33
34
35
36
37
38
# File 'lib/openapi_first/definition.rb', line 32

def operations
  methods = %w[get head post put patch delete trace options]
  @operations ||= paths.flat_map do |path, path_item_object|
    path_item = PathItem.new(path, path_item_object, openapi_version:)
    path_item_object.slice(*methods).keys.map { |method| path_item.find_operation(method) }
  end
end