Sha256: 79aaeda81afc8fda531f81b3e6d5de229744d1700c467536c3018502016433a3

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

module Fakeit
  module Openapi
    class Operation
      def initialize(request_operation, app_options)
        @request_operation = request_operation
        @validator = Fakeit::Validation::Validator.new(request_operation)
        @app_options = app_options
      end

      def status
        response.first.to_i
      end

      def headers
        response_headers
          &.map { |k, v| [k, v.schema.to_example(example_options)] }
          .to_h
          .tap { |headers| headers['Content-Type'] = response_content_type if response_content_type }
      end

      def body
        response_schema
          &.schema
          &.to_example(example_options)
          &.then(&JSON.method(:generate))
          .to_s
      end

      def validate(**request_parts)
        @validator.validate(request_parts)
      end

      private

      def example_options
        { use_example: @app_options.use_example, use_static: @app_options.method(:use_static?), depth: 0 }
      end

      def response_content
        response.last.content&.find { |k, _| k =~ %r{^application/.*json} }
      end

      def response_schema
        response_content&.last
      end

      def response_content_type
        response_content&.first
      end

      def response_headers
        response.last.headers
      end

      def response
        @request_operation.operation_object.responses.response.min
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fakeit-0.4.2 lib/fakeit/openapi/operation.rb
fakeit-0.4.1 lib/fakeit/openapi/operation.rb
fakeit-0.4.0 lib/fakeit/openapi/operation.rb
fakeit-0.3.1 lib/fakeit/openapi/operation.rb
fakeit-0.3.0 lib/fakeit/openapi/operation.rb