class ApiSchema::ResourceDefinition

Constants

HeaderParam
PathParam
QueryParam

Attributes

api_version[R]
base_path[R]
body_param[R]
desc_file_name[R]
desc_file_path[RW]
description[R]
errors[R]
extra_path[R]
full_path[R]
header_params[R]
method[R]
path_params[R]
query_params[R]
resp[R]
summary[R]

Public Class Methods

new(method, api_version, base_path, extra_path = nil) click to toggle source
# File lib/api_schema/resource_definition.rb, line 5
def initialize(method, api_version, base_path, extra_path = nil)
  @base_path = base_path
  @extra_path = extra_path
  @method = method
  @api_version = api_version
  @header_params = []
  @path_params = []
  @query_params = []
  @errors = []
end

Public Instance Methods

body(body_param) click to toggle source
# File lib/api_schema/resource_definition.rb, line 42
def body(body_param)
  @body_param = body_param
end
build(neighbors) click to toggle source
# File lib/api_schema/resource_definition.rb, line 92
def build(neighbors)
  error_model = :error_model
  error_desc = {
    '401' => "Unauthorized",
    '403' => "Forbidden",
    '404' => "Not found",
    '422' => "Unprocessable Entity"
 }

  build_description if desc_file_name
  resource = self
  swagger_path resource.full_path do
    neighbors[resource.full_path].each do |r|
      operation(r.method) do
        key :summary, r.summary
        key :description, r.description
        key :operationId, "#{r.method}_#{r.full_path}"
        key :tags, r.base_path
        security do
          key :authorization, []
        end
        body_param(r.body_param) if r.with_body?

        r.header_params.each do |p|
          header_param(p.name, p.type, p.required)
        end
        r.path_params.each do |p|
          path_param(p.name, p.type, p.required)
        end
        r.query_params.each do |p|
          query_param(p.name, p.type, p.required)
        end

        success_response(r.resp.code, r.resp.model, r.resp.fields)
        error_responses(error_model, error_desc, *r.errors) if r.with_errors?
      end
    end
  end
end
build_description() click to toggle source
# File lib/api_schema/resource_definition.rb, line 82
def build_description
  @description = IO.read(desc_file_path, encoding: 'utf-8')
end
build_neighbors(neighbors) click to toggle source
# File lib/api_schema/resource_definition.rb, line 86
def build_neighbors(neighbors)
  generate_full_path
  neighbors[full_path] ||= []
  neighbors[full_path] << self
end
desc(desc) click to toggle source
# File lib/api_schema/resource_definition.rb, line 30
def desc(desc)
  @description = desc
end
desc_file(desc_file_name) click to toggle source
# File lib/api_schema/resource_definition.rb, line 34
def desc_file(desc_file_name)
  @desc_file_name =  desc_file_name
end
error!(*codes) click to toggle source
# File lib/api_schema/resource_definition.rb, line 61
def error!(*codes)
  @errors = *codes
end
generate_full_path() click to toggle source
# File lib/api_schema/resource_definition.rb, line 77
def generate_full_path
  @full_path = with_path_param? ? "/#{base_path}/{id}" : "/#{base_path}"
  @full_path << "/#{extra_path}" if extra_path
end
header(name, type, required: true) click to toggle source
# File lib/api_schema/resource_definition.rb, line 38
def header(name, type, required: true)
  @header_params << HeaderParam.new(name, type, required)
end
name(name) click to toggle source
# File lib/api_schema/resource_definition.rb, line 26
def name(name)
  @summary = name
end
path_param(name, type, required: true) click to toggle source
# File lib/api_schema/resource_definition.rb, line 46
def path_param(name, type, required: true)
  @path_params << PathParam.new(name, type, required)
end
query_param(name, type, required: true) click to toggle source
# File lib/api_schema/resource_definition.rb, line 50
def query_param(name, type, required: true)
  @query_params << QueryParam.new(name, type, required)
end
response(code, model_name = nil, &block) click to toggle source
# File lib/api_schema/resource_definition.rb, line 54
def response(code, model_name = nil, &block)
  @resp = Response.new(code, model_name)
  if block && model_name.nil?
    block.call(@resp)
  end
end
with_body?() click to toggle source
# File lib/api_schema/resource_definition.rb, line 69
def with_body?
  !!body_param
end
with_errors?() click to toggle source
# File lib/api_schema/resource_definition.rb, line 73
def with_errors?
  !errors.empty?
end
with_path_param?() click to toggle source
# File lib/api_schema/resource_definition.rb, line 65
def with_path_param?
  !path_params.empty?
end