Class: Docit::Builders::ResponseBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/docit/builders/response_builder.rb

Overview

Builds the schema for a single HTTP response, including properties, examples, headers, and schema references.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, description:) ⇒ ResponseBuilder

Returns a new instance of ResponseBuilder.



10
11
12
13
14
15
16
17
# File 'lib/docit/builders/response_builder.rb', line 10

def initialize(status:, description:)
  @status = status
  @description = description
  @properties = []
  @examples = []
  @headers = []
  @schema_ref = nil
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/docit/builders/response_builder.rb', line 8

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



8
9
10
# File 'lib/docit/builders/response_builder.rb', line 8

def examples
  @examples
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/docit/builders/response_builder.rb', line 8

def headers
  @headers
end

#propertiesObject (readonly)

Returns the value of attribute properties.



8
9
10
# File 'lib/docit/builders/response_builder.rb', line 8

def properties
  @properties
end

#schema_refObject (readonly)

Returns the value of attribute schema_ref.



8
9
10
# File 'lib/docit/builders/response_builder.rb', line 8

def schema_ref
  @schema_ref
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/docit/builders/response_builder.rb', line 8

def status
  @status
end

Instance Method Details

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



50
51
52
53
54
# File 'lib/docit/builders/response_builder.rb', line 50

def example(name, value, description: nil)
  ex = { name: name, value: value }
  ex[:description] = description if description
  @examples << ex
end

#header(name, type: :string, description: nil, example: nil) ⇒ Object

Declares a response header, e.g. a rate-limit or pagination header:

header "X-RateLimit-Remaining", type: :integer, description: "..."


25
26
27
28
29
30
# File 'lib/docit/builders/response_builder.rb', line 25

def header(name, type: :string, description: nil, example: nil)
  entry = { name: name.to_s, type: type.to_s }
  entry[:description] = description if description
  entry[:example] = example unless example.nil?
  @headers << entry
end

#property(name, type:, format: nil, example: nil, enum: nil, description: nil, items: nil, **opts, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/docit/builders/response_builder.rb', line 32

def property(name, type:, format: nil, example: nil, enum: nil, description: nil, items: nil, **opts, &block)
  prop = { name: name, type: type }
  prop[:format] = format if format
  prop[:example] = example if example
  prop[:enum] = enum if enum
  prop[:description] = description if description
  prop[:items] = items if items
  prop.merge!(opts)

  if block_given?
    nested = self.class.new(status: @status, description: @description)
    nested.instance_eval(&block)
    prop[:children] = nested.properties
  end

  @properties << prop
end

#schema(ref:) ⇒ Object



19
20
21
# File 'lib/docit/builders/response_builder.rb', line 19

def schema(ref:)
  @schema_ref = ref.to_sym
end