class Brainstem::ApiDocs::Formatters::OpenApiSpecification::Version2::PresenterFormatter

Attributes

definition[RW]
output[RW]
presented_class[RW]
presenter[RW]

Public Class Methods

new(presenter, options = {}) click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 14
def initialize(presenter, options = {})
  self.presenter  = presenter
  self.definition = ActiveSupport::HashWithIndifferentAccess.new
  self.output     = ActiveSupport::HashWithIndifferentAccess.new

  super options
end

Public Instance Methods

call() click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 27
def call
  return {} if presenter.nodoc?

  format_title!
  format_description!
  format_type!
  format_fields!
  sort_properties!

  output.merge!(presenter.target_class => definition.reject {|_, v| v.blank?})
end

Private Instance Methods

association_description(key, name) click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 105
def association_description(key, name)
  ["`#{key}` will only be included in the response if `#{name}` is in the list of included associations.",
    "See <a href='#section/Includes'>include</a> section for usage."].join(' ')
end
association_key(association) click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 110
def association_key(association)
  if association.response_key
    association.response_key
  else
    key = association.name.singularize
    association.type == :has_many ? "#{key}_ids" : "#{key}_id"
  end
end
format_description!() click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 55
def format_description!
  definition.merge! description: format_sentence(presenter.description)
end
format_field(field) click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 119
def format_field(field)
  Brainstem::ApiDocs::FORMATTERS[:presenter_field][:oas_v2].call(presenter, field)
end
format_field_associations(properties) click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 78
def format_field_associations(properties)
  presenter.valid_associations.each_with_object(properties) do |(name, association), props|
    if association.polymorphic?
      key = association.name + "_ref"
      props[key] = {
        type: 'object',
        description: association_description(key, name),
        properties: {
          key: type_and_format(:string),
          id: type_and_format(:string)
        }
      }
      next
    end

    key = association_key(association)
    description = association_description(key, association.name)
    formatted_type = if association.type == :has_many
      type_and_format(:array, :string)
    else
      type_and_format(:string)
    end.merge(description: description)

    props[key] = formatted_type unless props[key]
  end
end
format_field_branch(branch) click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 72
def format_field_branch(branch)
  branch.each_with_object(ActiveSupport::HashWithIndifferentAccess.new) do |(name, field), buffer|
    buffer[name.to_s] = format_field(field)
  end
end
format_fields!() click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 63
def format_fields!
  return unless presenter.valid_fields.any? || presenter.valid_associations.any?

  properties = format_field_branch(presenter.valid_fields)
  with_associations = format_field_associations(properties)

  definition.merge! properties: with_associations
end
format_title!() click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 43
def format_title!
  definition.merge! title: presenter_title(presenter)
end
format_type!() click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 59
def format_type!
  definition.merge! type: 'object'
end
sort_properties!() click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/presenter_formatter.rb, line 47
def sort_properties!
  return if definition[:properties].blank?

  definition[:properties] = definition[:properties].sort.each_with_object({}) do |(key, val), obj|
    obj[key] = val
  end
end