Class: OasRails::Builders::ResponsesBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(specification) ⇒ ResponsesBuilder

Returns a new instance of ResponsesBuilder.



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

def initialize(specification)
  @specification = specification
  @responses = Spec::Responses.new(specification)
end

Instance Method Details

#add_autodiscovered_responses(oas_route) ⇒ Object



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

def add_autodiscovered_responses(oas_route)
  return self if !OasRails.config.autodiscover_responses || oas_route.docstring.tags(:response).any?

  new_responses = Extractors::RenderResponseExtractor.extract_responses_from_source(@specification, source: oas_route.source_string)

  new_responses.each do |new_response|
    @responses.add_response(new_response) if @responses.responses[new_response.code].blank?
  end

  self
end

#add_default_responses(oas_route, security) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/oas_rails/builders/responses_builder.rb', line 32

def add_default_responses(oas_route, security)
  return self unless OasRails.config.set_default_responses

  content = ContentBuilder.new(@specification, :outgoing).with_schema(JsonSchemaGenerator.process_string(OasRails.config.response_body_of_default)[:json_schema]).build
  common_errors = []
  common_errors.push(:unauthorized, :forbidden) if security

  case oas_route.method
  when "show", "update", "destroy"
    common_errors.push(:not_found)
  when "create", "index"
    # possible errors for this methods?
  end

  (OasRails.config.possible_default_responses & common_errors).each do |e|
    code = Utils.status_to_integer(e)
    response = ResponseBuilder.new(@specification).with_code(code).with_description(Utils.get_definition(code)).with_content(content).build

    @responses.add_response(response) if @responses.responses[response.code].blank?
  end

  self
end

#buildObject



56
57
58
# File 'lib/oas_rails/builders/responses_builder.rb', line 56

def build
  @responses
end

#from_oas_route(oas_route) ⇒ Object



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

def from_oas_route(oas_route)
  oas_route.docstring.tags(:response).each do |tag|
    content = ContentBuilder.new(@specification, :outgoing).with_schema(tag.schema).with_examples_from_tags(oas_route.docstring.tags(:response_example).filter { |re| re.code == tag.name }).build
    response = ResponseBuilder.new(@specification).with_code(tag.name.to_i).with_description(tag.text).with_content(content).build

    @responses.add_response(response)
  end

  self
end