Sha256: 094d64ddcd4f9c16a3efbaef28399d61b387e79aa631f725c6b477150a2e6a90

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

module OasRails
  module Builders
    class ResponsesBuilder
      def initialize(specification)
        @specification = specification
        @responses = Spec::Responses.new(specification)
      end

      def from_oas_route(oas_route)
        oas_route.docstring.tags(:response).each do |tag|
          @responses.add_response(ResponseBuilder.new(@specification).from_tag(tag).build)
        end

        self
      end

      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

      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

      def build
        @responses
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oas_rails-0.5.0 lib/oas_rails/builders/responses_builder.rb