Class: AutoRswagHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_rswag_helper.rb

Overview

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

Class Method Summary collapse

Class Method Details

.convert(key) ⇒ Object



55
56
57
# File 'lib/auto_rswag_helper.rb', line 55

def convert(key)
  key.to_sym
end

.convert_response(response) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



59
60
61
62
63
64
65
66
# 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