Class: Mortymer::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/mortymer/endpoint.rb

Overview

Represents an endpoint in a given system

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Endpoint

Returns a new instance of Endpoint.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mortymer/endpoint.rb', line 10

def initialize(opts = {})
  @http_method = opts[:http_method]
  @input_class = opts[:input_class]
  @output_class = opts[:output_class]
  @name = opts[:name]
  @path = opts[:path] || infer_path_from_class
  @controller_class = opts[:controller_class]
  @action = opts[:action]
  @security = opts[:security]
  @tags = opts[:tags]
  @exception_handlers = opts[:exception_handlers]
end

Instance Attribute Details

#actionObject (readonly)

rubocop:disable Metrics/ClassLength



8
9
10
# File 'lib/mortymer/endpoint.rb', line 8

def action
  @action
end

#controller_classObject (readonly)

rubocop:disable Metrics/ClassLength



8
9
10
# File 'lib/mortymer/endpoint.rb', line 8

def controller_class
  @controller_class
end

#http_methodObject (readonly)

rubocop:disable Metrics/ClassLength



8
9
10
# File 'lib/mortymer/endpoint.rb', line 8

def http_method
  @http_method
end

#input_classObject (readonly)

rubocop:disable Metrics/ClassLength



8
9
10
# File 'lib/mortymer/endpoint.rb', line 8

def input_class
  @input_class
end

#nameObject (readonly)

rubocop:disable Metrics/ClassLength



8
9
10
# File 'lib/mortymer/endpoint.rb', line 8

def name
  @name
end

#output_classObject (readonly)

rubocop:disable Metrics/ClassLength



8
9
10
# File 'lib/mortymer/endpoint.rb', line 8

def output_class
  @output_class
end

#pathObject (readonly)

rubocop:disable Metrics/ClassLength



8
9
10
# File 'lib/mortymer/endpoint.rb', line 8

def path
  @path
end

Instance Method Details

#api_nameObject



27
28
29
# File 'lib/mortymer/endpoint.rb', line 27

def api_name
  Utils::StringTransformations.underscore(name.gsub("::", "/")).gsub(/_endpoint$/, "").split("#").first
end

#controller_nameObject



31
32
33
# File 'lib/mortymer/endpoint.rb', line 31

def controller_name
  Utils::StringTransformations.underscore(@name.split("#").first.split("::").join("/"))
end

#generate_openapi_schemaObject

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mortymer/endpoint.rb', line 42

def generate_openapi_schema # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  return unless defined?(@input_class) && defined?(@output_class)

  input_schema = @input_class.respond_to?(:json_schema) ? @input_class.json_schema : Generator.new.from_struct(@input_class)
  responses = generate_responses

  # Add 422 response if there are required properties or non-string types that need coercion
  if validations?(input_schema)
    responses["422"] = {
      description: "Validation Failed - Invalid parameters or coercion error",
      content: {
        "application/json": {
          schema: error422_ref
        }
      }
    }
  end

  operation = {
    operation_id: operation_id,
    parameters: generate_parameters,
    requestBody: generate_request_body,
    responses: responses,
    tags: @tags
  }

  operation[:security] = security if @security
  {
    path.to_s => {
      http_method.to_s => operation
    }
  }
end

#infer_path_from_classObject



35
36
37
38
39
40
# File 'lib/mortymer/endpoint.rb', line 35

def infer_path_from_class
  # Remove 'Endpoint' suffix if present and convert to path
  "/" + @name.split("#").first.split("::").map do |s|
    Utils::StringTransformations.underscore(s).gsub(/_endpoint$/, "").gsub(/_controller$/, "")
  end.join("/")
end

#routeable?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/mortymer/endpoint.rb', line 23

def routeable?
  [@input_class, @http_method, @output_class, @path].none?(&:nil?)
end