Class: OasRails::Builders::RequestBodyBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(specification) ⇒ RequestBodyBuilder

Returns a new instance of RequestBodyBuilder.



4
5
6
7
# File 'lib/oas_rails/builders/request_body_builder.rb', line 4

def initialize(specification)
  @specification = specification
  @request_body = Spec::RequestBody.new(specification)
end

Instance Method Details

#buildObject



40
41
42
43
44
# File 'lib/oas_rails/builders/request_body_builder.rb', line 40

def build
  return {} if @request_body.content == {}

  @request_body
end

#from_model_class(klass:, **kwargs) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/oas_rails/builders/request_body_builder.rb', line 32

def from_model_class(klass:, **kwargs)
  @request_body.description = kwargs[:description] || klass.to_s
  @request_body.content = ContentBuilder.new(@specification, :incoming).from_model_class(klass).with_examples_from_tags(kwargs[:examples_tags] || {}).build
  @request_body.required = kwargs[:required]

  self
end

#from_oas_route(oas_route) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/oas_rails/builders/request_body_builder.rb', line 9

def from_oas_route(oas_route)
  tag_request_body = oas_route.docstring.tags(:request_body).first
  if tag_request_body.nil? && OasRails.config.autodiscover_request_body
    detect_request_body(oas_route) if %w[create update].include? oas_route.method
  elsif !tag_request_body.nil?
    from_tags(tag: tag_request_body, examples_tags: oas_route.docstring.tags(:request_body_example))
  end

  self
end

#from_tags(tag:, examples_tags: []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/oas_rails/builders/request_body_builder.rb', line 20

def from_tags(tag:, examples_tags: [])
  if tag.klass.ancestors.include? ActiveRecord::Base
    from_model_class(klass: tag.klass, description: tag.text, required: tag.required, examples_tags:)
  else
    @request_body.description = tag.text
    @request_body.content = ContentBuilder.new(@specification, :incoming).with_schema(tag.schema).with_examples_from_tags(examples_tags).build
    @request_body.required = tag.required
  end

  self
end

#referenceObject



46
47
48
49
50
# File 'lib/oas_rails/builders/request_body_builder.rb', line 46

def reference
  return {} if @request_body.content == {}

  @specification.components.add_request_body(@request_body)
end