Sha256: f523a9b4c1d4dde0370bf9ddf57da4a7e5089b5e8bf146bfead51d0256d391fc

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require "mountapi/route"

module Mountapi
  module OpenApi
    class Operation
      METHODS = %w[get post put patch delete options trace head].freeze

      attr_reader :path, :method, :version, :spec
      def initialize(method, spec, path, version)
        @path = path
        @method = method
        @version = version
        @spec = spec
      end

      def to_route
        operation_id = spec.node.operation_id

        Route.build(
          path: path,
          method: method,
          handler: Mountapi.handlers[operation_id],
          parameters: parameters,
          responses: responses,
          version: version,
          operation_id: operation_id
        )
      end

      # @return [Array<String, Hash>] the parameter name and it's options
      def parameters
        http_params.concat(body_params)
      end

      # @return [Array<String, Hash>] the response status code and it's schema
      def responses
        spec.node.responses.inject([]) do |responses, (code, response)|
          responses << [code, response.content["application/json"]&.schema]
        end
      end

      private

      def http_params
        spec.node.parameters.map { |param| [param.name, param.in, param] }
      end

      def body_params
        return [] unless spec.node.request_body&.content&.any?
        content = spec.node.request_body.content["application/json"] || spec.node.request_body.content["multipart/form-data"]

        if content
          content.schema.properties.map do |name, schema|
            [name, "body", OpenStruct.new(schema: schema)]
          end
        else
          []
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mountapi-0.11.1 lib/mountapi/open_api/operation.rb