Sha256: aa31d93c6033b1d4a57a083e9f7f3fed58e7733107c13d8b80dfd0ec8a1a5f0d

Contents?: true

Size: 1.87 KB

Versions: 15

Compression:

Stored size: 1.87 KB

Contents

module SoberSwag
  class Compiler
    ##
    # Compile a singular path, and that's it.
    # Only handles the actual body.
    class Path
      ##
      # @param route [SoberSwag::Controller::Route] a route to use
      # @param compiler [SoberSwag::Compiler] the compiler to use for type compilation
      def initialize(route, compiler)
        @route = route
        @compiler = compiler
      end

      attr_reader :route, :compiler

      def schema
        base = {}
        base[:summary] = route.summary if route.summary
        base[:description] = route.description if route.description
        base[:parameters] = params if params.any?
        base[:responses] = responses
        base[:requestBody] = request_body if request_body
        base
      end

      def responses # rubocop:disable Metrics/MethodLength
        route.response_serializers.map { |status, serializer|
          [
            status.to_s,
            {
              description: route.response_descriptions[status],
              content: {
                'application/json': {
                  schema: compiler.response_for(serializer.type)
                }
              }
            }
          ]
        }.to_h
      end

      def params
        query_params + path_params
      end

      def query_params
        if route.query_params_class
          compiler.query_params_for(route.query_params_class)
        else
          []
        end
      end

      def path_params
        if route.path_params_class
          compiler.path_params_for(route.path_params_class)
        else
          []
        end
      end

      def request_body
        return nil unless route.request_body_class

        {
          required: true,
          content: {
            'application/json': {
              schema: compiler.body_for(route.request_body_class)
            }
          }
        }
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sober_swag-0.16.0 lib/sober_swag/compiler/path.rb
sober_swag-0.15.0 lib/sober_swag/compiler/path.rb
sober_swag-0.14.0 lib/sober_swag/compiler/path.rb
sober_swag-0.13.0 lib/sober_swag/compiler/path.rb
sober_swag-0.12.0 lib/sober_swag/compiler/path.rb
sober_swag-0.11.0 lib/sober_swag/compiler/path.rb
sober_swag-0.10.0 lib/sober_swag/compiler/path.rb
sober_swag-0.9.0 lib/sober_swag/compiler/path.rb
sober_swag-0.8.0 lib/sober_swag/compiler/path.rb
sober_swag-0.7.0 lib/sober_swag/compiler/path.rb
sober_swag-0.6.0 lib/sober_swag/compiler/path.rb
sober_swag-0.5.0 lib/sober_swag/compiler/path.rb
sober_swag-0.4.0 lib/sober_swag/compiler/path.rb
sober_swag-0.3.0 lib/sober_swag/compiler/path.rb
sober_swag-0.2.0 lib/sober_swag/compiler/path.rb