class GrapeSwagger::Entity::AttributeParser

Attributes

endpoint[R]

Public Class Methods

new(endpoint) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 8
def initialize(endpoint)
  @endpoint = endpoint
end

Public Instance Methods

call(entity_options) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 12
def call(entity_options)
  param = if (entity_model = model_from(entity_options))
            name = GrapeSwagger::Entity::Helper.model_name(entity_model, endpoint)
            entity_model_type(name, entity_options)
          else
            data_type_from(entity_options)
          end

  documentation = entity_options[:documentation]
  return param if documentation.nil?

  add_array_documentation(param, documentation) if documentation[:is_array]

  add_attribute_sample(param, documentation, :default)
  add_attribute_sample(param, documentation, :example)

  add_attribute_documentation(param, documentation)

  add_extension_documentation(param, documentation)
  add_discriminator_extension(param, documentation)
  param
end

Private Instance Methods

add_array_documentation(param, documentation) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 132
def add_array_documentation(param, documentation)
  param[:minItems] = documentation[:min_items] if documentation.key?(:min_items)
  param[:maxItems] = documentation[:max_items] if documentation.key?(:max_items)
  param[:uniqueItems] = documentation[:unique_items] if documentation.key?(:unique_items)
end
add_attribute_documentation(param, documentation) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 118
def add_attribute_documentation(param, documentation)
  param[:minimum] = documentation[:minimum] if documentation.key?(:minimum)
  param[:maximum] = documentation[:maximum] if documentation.key?(:maximum)

  values = documentation[:values]
  if values&.is_a?(Range)
    param[:minimum] = values.begin if values.begin.is_a?(Numeric)
    param[:maximum] = values.end if values.end.is_a?(Numeric)
  end

  param[:minLength] = documentation[:min_length] if documentation.key?(:min_length)
  param[:maxLength] = documentation[:max_length] if documentation.key?(:max_length)
end
add_attribute_sample(attribute, hash, key) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 111
def add_attribute_sample(attribute, hash, key)
  value = hash[key]
  return if value.nil?

  attribute[key] = value.is_a?(Proc) ? value.call : value
end
add_discriminator_extension(param, documentation) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 142
def add_discriminator_extension(param, documentation)
  param[:documentation] = { is_discriminator: true } if documentation.key?(:is_discriminator)
end
add_extension_documentation(param, documentation) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 138
def add_extension_documentation(param, documentation)
  GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(documentation, param)
end
ambiguous_model_type?(type) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 55
def ambiguous_model_type?(type)
  type&.is_a?(Class) &&
    !GrapeSwagger::DocMethods::DataType.primitive?(type.name.downcase) &&
    !type == Array
end
could_it_be_a_model?(value) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 45
def could_it_be_a_model?(value)
  return false if value.nil?

  direct_model_type?(value[:type]) || ambiguous_model_type?(value[:type])
end
data_type_from(entity_options) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 61
def data_type_from(entity_options)
  documentation = entity_options[:documentation] || {}
  documented_type = entity_options[:type] || documentation[:type]

  data_type = GrapeSwagger::DocMethods::DataType.call(documented_type)

  documented_data_type = document_data_type(documentation, data_type)

  if documentation[:is_array]
    {
      type: :array,
      items: documented_data_type
    }
  else
    documented_data_type
  end
end
direct_model_type?(type) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 51
def direct_model_type?(type)
  type.to_s.include?('Entity') || type.to_s.include?('Entities')
end
document_data_type(documentation, data_type) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 79
def document_data_type(documentation, data_type)
  type = if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
           data = GrapeSwagger::DocMethods::DataType.mapping(data_type)
           { type: data.first, format: data.last }
         else
           { type: data_type }
         end

  type[:format] = documentation[:format] if documentation.key?(:format)

  if (values = documentation[:values]) && values.is_a?(Array)
    type[:enum] = values
  end

  type
end
entity_model_type(name, entity_options) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 96
def entity_model_type(name, entity_options)
  if entity_options[:documentation] && entity_options[:documentation][:is_array]
    {
      'type' => 'array',
      'items' => {
        '$ref' => "#/definitions/#{name}"
      }
    }
  else
    {
      '$ref' => "#/definitions/#{name}"
    }
  end
end
model_from(entity_options) click to toggle source
# File lib/grape-swagger/entity/attribute_parser.rb, line 37
def model_from(entity_options)
  model = entity_options[:using] if entity_options[:using].present?

  model ||= entity_options[:documentation][:type] if could_it_be_a_model?(entity_options[:documentation])

  model
end