Sha256: 09e8f06e5f73846f80939105ccdf48df826c812e97dc511b7c73ddb96bcd0ad2

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 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(&JSON.method(:generate))
          .to_s
      end

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

      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} }

      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

2 entries across 2 versions & 1 rubygems

Version Path
fakeit-0.8.1 lib/fakeit/openapi/operation.rb
fakeit-0.8.0 lib/fakeit/openapi/operation.rb