Sha256: f75561b16cf73c23a07a6aa79a6a0e7bc0849840785b48feedc47339e1802cfc

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 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

      def headers
        response_headers
          .to_h
          .transform_values { _1.schema.to_example(example_options) }
          .tap { _1['Content-Type'] = response_content_type if response_content_type }
      end

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

      def validate(...) = @validator.validate(...)

      private

      def serialise(body) = body.is_a?(String) ? body : JSON.generate(body)

      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} || k == 'application/pdf' }
      end

      def response_schema = response_content&.last

      def response_content_type = response_content&.first

      def response_headers = response.last.headers

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fakeit-0.11.0 lib/fakeit/openapi/operation.rb
fakeit-0.10.0 lib/fakeit/openapi/operation.rb
fakeit-0.9.2 lib/fakeit/openapi/operation.rb
fakeit-0.9.1 lib/fakeit/openapi/operation.rb
fakeit-0.9.0 lib/fakeit/openapi/operation.rb