class AutoRswagHelper

This class performs conversions of the response data based on test metadata.

Public Class Methods

convert(key) click to toggle source
# File lib/auto_rswag_helper.rb, line 55
def convert(key)
  key.to_sym
end
convert_response(response) click to toggle source
# File lib/auto_rswag_helper.rb, line 7
def convert_response(response)
  return response if [Array, Hash].include?(response.class)

  body = response.body
  body = body.respond_to?(:read) ? body.read : body

  JSON.parse(body)
rescue StandardError
  body
end
map_fields(object) click to toggle source
# File lib/auto_rswag_helper.rb, line 18
def map_fields(object)
  if object.is_a? Array
    object = {
      type: :array,
      items: map_fields(object.first)
    }
  elsif object.is_a?(Hash)
    object = {
      type: :object,
      properties: map_object_keys(object)
    }
  end
  object
end
map_object_keys(object) click to toggle source
# File lib/auto_rswag_helper.rb, line 33
def map_object_keys(object)
  object.keys.each do |key|
    value = object.delete(key)
    converted_key = convert(key)
    if value.is_a? Hash
      object[converted_key] = {
        type: :object,
        properties: value
      }
      map_fields(value)
    elsif value.is_a? Array
      object[converted_key] = {
        type: :array,
        items: [value.first]
      }
      map_fields(value)
    else
      object[converted_key] = parse_field(value)
    end
  end
end
parse_field(value) click to toggle source
# File lib/auto_rswag_helper.rb, line 59
def parse_field(value)
  type = value.nil? ? :string : value.class.to_s.downcase.to_sym
  {
    type: type,
    example: value,
    'x-nullable' => true
  }
end