1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/doc/open_api/api_doc.rb
2023-03-22 10:58:22 +01:00

61 lines
1.5 KiB
Ruby

# frozen_string_literal: true
# app/concerns/controllers/api_doc.rb
#
# Controller extension with common API documentation shortcuts
#
module OpenAPI::APIDoc
# Apipie doesn't allow to append anything to esisting
# description. It raises an error on double definition.
#
def append_desc(desc = '')
_apipie_dsl_data[:description] << desc << "\n"
end
# Converts passed +code+ to the markdown
# by prepending 4 spaces to each line
#
# @param code [String]
# @return [String]
#
def to_markdown_code(code)
code.split("\n").map do |line|
(' ' * 4) + line
end.join("\n")
end
# Includes passed list of json schemas
# to method description
#
# @example
# include_response_schema 'users.json', '_user.json'
#
# @param schemas [Array<String>]
#
def include_response_schema(*schemas)
_apipie_dsl_data[:description] = _apipie_dsl_data[:description].strip_heredoc
append_desc('## Response schema')
schemas.each do |relative_path|
append_desc MarkdownJsonSchema.read(relative_path)
end
end
# Exports all documentation from provided class
#
# @example
# class ProfilesController < ApplicationController
# extend Controllers::ApiDoc
# expose_doc
# # exports all docs from ProfilesDoc class
# # that must be inherired from ApplicationDoc
# end
#
# @see ApplicationDoc
#
def expose_doc(doc_name = "#{controller_path}_doc")
doc_klass = doc_name.classify.constantize
doc_klass.apply(self)
end
end