Class: Docit::Builders::RequestBodyBuilder

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

Overview

Builds the schema for a request body, including properties, required fields, and schema references.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(required: false, content_type: "application/json") ⇒ RequestBodyBuilder

Returns a new instance of RequestBodyBuilder.



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

def initialize(required: false, content_type: "application/json")
  @required = required
  @content_type = content_type
  @properties = []
  @schema_ref = nil
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



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

def content_type
  @content_type
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#requiredObject (readonly)

Returns the value of attribute required.



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

def required
  @required
end

#schema_refObject (readonly)

Returns the value of attribute schema_ref.



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

def schema_ref
  @schema_ref
end

Instance Method Details

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/docit/builders/request_body_builder.rb', line 21

def property(name, type:, required: false, format: nil, example: nil, enum: nil, description: nil, items: nil,
             **opts, &block)
  prop = { name: name, type: type, required: required }
  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(required: @required, content_type: @content_type)
    nested.instance_eval(&block)
    prop[:children] = nested.properties
  end

  @properties << prop
end

#required_propertiesObject



40
41
42
# File 'lib/docit/builders/request_body_builder.rb', line 40

def required_properties
  @properties.select { |prop| prop[:required] }.map { |prop| prop[:name].to_s }
end

#schema(ref:) ⇒ Object



17
18
19
# File 'lib/docit/builders/request_body_builder.rb', line 17

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