Class: SwaggerPrinter

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

Overview

This class walks through the payload object and builds a prettified representation in rswag format.

Class Method Summary collapse

Class Method Details

.end_wrapObject



28
29
30
31
32
33
34
35
# File 'lib/printer.rb', line 28

def end_wrap
  line = ''
  while @indent > 2
    @indent -= 2
    line += ' ' * @indent + "}\n"
  end
  line + '}'
end

.escape_key(key) ⇒ Object



90
91
92
93
94
# File 'lib/printer.rb', line 90

def escape_key(key)
  return key unless key.to_s.include?('-')

  "'#{key}'"
end

.prettify_value(type, key, val) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/printer.rb', line 96

def prettify_value(type, key, val)
  return ':' + val.to_s if key == :type
  return val if key != :example
  return 'nil' if val.nil?

  type[:type] == :string ? "'#{val}'" : val
end


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/printer.rb', line 67

def print_array(key, val)
  line = ' ' * @indent + "#{key}: {\n"
  @indent += 2
  line += wrap_array
  line += print_values(val[:items].first)
  @indent -= 2
  line += ' ' * @indent + "}\n"
  @indent -= 2
  line += ' ' * @indent + '}'
  line
end


56
57
58
59
60
61
62
63
64
65
# File 'lib/printer.rb', line 56

def print_hash(key, val)
  line = ' ' * @indent + "#{key}: {\n"
  @indent += 2
  line += print_values(val[:properties])
  @indent -= 2
  line += ' ' * @indent + "}\n"
  @indent -= 2
  line += ' ' * @indent + '}'
  line
end


79
80
81
82
83
84
85
86
87
88
# File 'lib/printer.rb', line 79

def print_line(key, val)
  line = ' ' * @indent + "#{key}: { "
  val.each_with_index do |(val_key, val_val), j|
    next if val_key == :type && val.any? { |k, v| k == :example && v.nil? }

    val_comma = j == val.keys.size - 1 ? '' : ','
    line += "#{escape_key(val_key)}: #{prettify_value(val, val_key, val_val)}#{val_comma} "
  end
  line + '}'
end


7
8
9
10
11
12
# File 'lib/printer.rb', line 7

def print_swagger(object, test_title)
  @indent ||= 2
  line = "#{test_title}: {\n"
  line += print_values(object)
  line + end_wrap
end


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/printer.rb', line 37

def print_values(object)
  return wrap_array + print_values(object.first) if object.is_a?(Array)

  output = wrap_hash
  object.each_with_index do |(key, val), i|
    line =  if val[:type] == :object
              print_hash(key, val)
            elsif val[:type] == :array
              print_array(key, val)
            else
              print_line(key, val)
            end
    comma = i == object.keys.size - 1 ? '' : ','
    line += "#{comma}\n"
    output += line
  end
  output
end

.wrap_arrayObject



21
22
23
24
25
26
# File 'lib/printer.rb', line 21

def wrap_array
  line  = ' ' * @indent + "type: :array,\n"
  line += ' ' * @indent + "items: {\n"
  @indent += 2
  line
end

.wrap_hashObject



14
15
16
17
18
19
# File 'lib/printer.rb', line 14

def wrap_hash
  line  = ' ' * @indent + "type: :object,\n"
  line += ' ' * @indent + "properties: {\n"
  @indent += 2
  line
end