Class: OpenapiFirst::Operation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/openapi_first/operation.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, request_method, path_item_object) ⇒ Operation

Returns a new instance of Operation.



21
22
23
24
25
# File 'lib/openapi_first/operation.rb', line 21

def initialize(path, request_method, path_item_object)
  @path = path
  @method = request_method
  @path_item_object = path_item_object
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



19
20
21
# File 'lib/openapi_first/operation.rb', line 19

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/openapi_first/operation.rb', line 19

def path
  @path
end

Instance Method Details

#content_types_for(status) ⇒ Object



50
51
52
# File 'lib/openapi_first/operation.rb', line 50

def content_types_for(status)
  response_for(status)['content']&.keys
end

#nameObject



87
88
89
# File 'lib/openapi_first/operation.rb', line 87

def name
  "#{method.upcase} #{path} (#{operation_id})"
end

#operation_idObject



27
28
29
# File 'lib/openapi_first/operation.rb', line 27

def operation_id
  operation_object['operationId']
end

#parameters_schemaObject



43
44
45
46
47
48
# File 'lib/openapi_first/operation.rb', line 43

def parameters_schema
  @parameters_schema ||= begin
    parameters_json_schema = build_parameters_json_schema
    parameters_json_schema && SchemaValidation.new(parameters_json_schema)
  end
end

#read?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/openapi_first/operation.rb', line 31

def read?
  !write?
end

#request_bodyObject



39
40
41
# File 'lib/openapi_first/operation.rb', line 39

def request_body
  operation_object['requestBody']
end

#request_body_schema(request_content_type) ⇒ Object



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

def request_body_schema(request_content_type)
  content = operation_object.dig('requestBody', 'content')
  media_type = find_content_for_content_type(content, request_content_type)
  schema = media_type&.fetch('schema', nil)
  return unless schema

  SchemaValidation.new(schema, write: write?)
end

#response_for(status) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/openapi_first/operation.rb', line 79

def response_for(status)
  response_content = response_by_code(status)
  return response_content if response_content

  message = "Response status code or default not found: #{status} for '#{name}'"
  raise OpenapiFirst::ResponseCodeNotFoundError, message
end

#response_schema_for(status, content_type) ⇒ Object

Raises:



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

def response_schema_for(status, content_type)
  content = response_for(status)['content']
  return if content.nil? || content.empty?

  raise ResponseInvalid, "Response has no content-type for '#{name}'" unless content_type

  media_type = find_content_for_content_type(content, content_type)

  unless media_type
    message = "Response content type not found '#{content_type}' for '#{name}'"
    raise ResponseContentTypeNotFoundError, message
  end
  schema = media_type['schema']
  SchemaValidation.new(schema, write: false) if schema
end

#write?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/openapi_first/operation.rb', line 35

def write?
  WRITE_METHODS.include?(method)
end