Class: OpenapiRuby::DSL::ResponseContext

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_ruby/dsl/response_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code, description) ⇒ ResponseContext

Returns a new instance of ResponseContext.



8
9
10
11
12
13
14
15
16
# File 'lib/openapi_ruby/dsl/response_context.rb', line 8

def initialize(status_code, description)
  @status_code = status_code.to_s
  @description = description
  @schema_definition = nil
  @headers = {}
  @examples = {}
  @links = {}
  @content_types = nil
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/openapi_ruby/dsl/response_context.rb', line 6

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



6
7
8
# File 'lib/openapi_ruby/dsl/response_context.rb', line 6

def examples
  @examples
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/openapi_ruby/dsl/response_context.rb', line 6

def headers
  @headers
end

Returns the value of attribute links.



6
7
8
# File 'lib/openapi_ruby/dsl/response_context.rb', line 6

def links
  @links
end

#schema_definitionObject (readonly)

Returns the value of attribute schema_definition.



6
7
8
# File 'lib/openapi_ruby/dsl/response_context.rb', line 6

def schema_definition
  @schema_definition
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



6
7
8
# File 'lib/openapi_ruby/dsl/response_context.rb', line 6

def status_code
  @status_code
end

Instance Method Details

#example(content_type, value:, name: "example", summary: nil, description: nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/openapi_ruby/dsl/response_context.rb', line 26

def example(content_type, value:, name: "example", summary: nil, description: nil)
  @examples[content_type] ||= {}
  entry = {"value" => value}
  entry["summary"] = summary if summary
  entry["description"] = description if description
  @examples[content_type][name.to_s] = entry
end

#header(name, attributes = {}) ⇒ Object



22
23
24
# File 'lib/openapi_ruby/dsl/response_context.rb', line 22

def header(name, attributes = {})
  @headers[name.to_s] = normalize(attributes)
end

#produces(*content_types) ⇒ Object



34
35
36
# File 'lib/openapi_ruby/dsl/response_context.rb', line 34

def produces(*content_types)
  @content_types = content_types.flatten
end

#schema(definition) ⇒ Object



18
19
20
# File 'lib/openapi_ruby/dsl/response_context.rb', line 18

def schema(definition)
  @schema_definition = normalize(definition)
end

#to_openapiObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/openapi_ruby/dsl/response_context.rb', line 38

def to_openapi
  result = {"description" => @description}

  if @schema_definition
    types = @content_types || ["application/json"]
    result["content"] = {}
    types.each do |ct|
      media = {"schema" => @schema_definition}
      media["examples"] = @examples[ct] if @examples.key?(ct)
      result["content"][ct] = media
    end
  end

  result["headers"] = build_headers if @headers.any?
  result["links"] = @links unless @links.empty?
  result
end