class OpenApiDocumentation::Path

Constants

VERBS_WITHOUT_BODY

Public Class Methods

new(path, prefix, app) click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 59
def initialize(path, prefix, app)
  @path, @prefix, @app = path, prefix, app
end

Public Instance Methods

build() click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 63
def build
  request_body = build_request_body unless VERBS_WITHOUT_BODY.include?(@path.http_verb)
  {
    name =>
    {
      @path.http_verb.to_sym => {
        summary: @path.description,
        description: @path.description,
        tags: [ @app.to_s ],
        parameters: build_parameters,
        requestBody: request_body,
        responses: build_responses,
        operationId: operation_id
      }.delete_if { |_k, v| v.nil? || v.empty? }
    }
  }
end
name() click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 81
def name
  full_path = @path.path.to_s
  @path.route_parameters.each do |parameter|
    # This is a bit confusing but naming is a little different between
    # apiculture and openapi
    full_path.gsub!(":#{parameter.name}", "\{#{parameter.name}\}")
  end
  Util.clean_path("#{@prefix}#{full_path}")
end

Private Instance Methods

build_parameters() click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 98
def build_parameters
  if VERBS_WITHOUT_BODY.include?(@path.http_verb)
    build_route_parameters + build_query_parameters
  else
    build_route_parameters
  end
end
build_query_parameters() click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 122
def build_query_parameters
  params = @path.parameters.map do |parameter|
    {
      name: parameter.name,
      description: parameter.description,
      required: true,
      in: :query,
      schema: {
        type: Util.map_type(parameter.matchable),
        example: parameter.matchable
      }
    }
  end
  params
end
build_request_body() click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 138
def build_request_body
  return nil if VERBS_WITHOUT_BODY.include?(@path.http_verb)

  body_params = Hash[ @path.parameters.collect do |parameter|
    [parameter.name, {
      type: Util.map_type(parameter.matchable),
      description: parameter.description
    }]
  end ]

  return nil if body_params.count == 0

  schema = {
    type: :object,
    properties: body_params
  }

  schema[:required] = @path.parameters.select(&:required).map(&:name) if @path.parameters.select(&:required).map(&:name).count > 0
  {
    content: {
      "application/json": {
        schema: schema
      }
    }
  }
end
build_responses() click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 165
def build_responses
  responses = Hash[@path.responses.collect do |response|
    _response = {
      description: response.description
    }

    unless response.jsonable_object_example.nil? || response.jsonable_object_example.empty?
      _response[:content] = {
        'application/json': {
            schema:
              { type: 'object',
              properties: Util.response_to_schema(response.jsonable_object_example) }
        }
      }
    end

    [response.http_status_code.to_s, _response]
  end ]
  responses
end
build_route_parameters() click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 106
def build_route_parameters
  route_params = @path.route_parameters.map do |parameter|
    {
      name: parameter.name,
      description: parameter.description,
      required: true,
      in: :path,
      schema: {
        type: Util.map_type(parameter.matchable),
        example: Util.map_example(parameter.matchable)
      }
    }
  end
  route_params
end
operation_id() click to toggle source
# File lib/apiculture/openapi_documentation.rb, line 93
def operation_id
  # base64 encoding to make sure these ids are safe to use in an url
  Base64.urlsafe_encode64("#{@path.http_verb}#{@prefix}#{@path.path}")
end