Class: OpenapiFirst::Operation

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

Instance Method Summary collapse

Constructor Details

#initialize(parsed) ⇒ Operation

Returns a new instance of Operation.



17
18
19
# File 'lib/openapi_first/operation.rb', line 17

def initialize(parsed)
  @operation = parsed
end

Instance Method Details

#content_type_for(status) ⇒ Object



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

def content_type_for(status)
  content = response_for(status)['content']
  content.keys[0] if content
end

#nameObject



59
60
61
# File 'lib/openapi_first/operation.rb', line 59

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

#parameters_json_schemaObject



25
26
27
# File 'lib/openapi_first/operation.rb', line 25

def parameters_json_schema
  @parameters_json_schema ||= build_parameters_json_schema
end

#parameters_schemaObject



29
30
31
# File 'lib/openapi_first/operation.rb', line 29

def parameters_schema
  @parameters_schema ||= parameters_json_schema && JSONSchemer.schema(parameters_json_schema)
end

#pathObject



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

def path
  @operation.path.path
end

#response_for(status) ⇒ Object



52
53
54
55
56
57
# File 'lib/openapi_first/operation.rb', line 52

def response_for(status)
  @operation.response_by_code(status.to_s, use_default: true).raw
rescue OasParser::ResponseCodeNotFound
  message = "Response status code or default not found: #{status} for '#{name}'"
  raise OpenapiFirst::ResponseCodeNotFoundError, message
end

#response_schema_for(status, content_type) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/openapi_first/operation.rb', line 38

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 = content[content_type]
  unless media_type
    message = "Response content type not found '#{content_type}' for '#{name}'"
    raise ResponseContentTypeNotFoundError, message
  end
  media_type['schema']
end