Class: Transducer::Formatter

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

Instance Method Summary collapse

Instance Method Details

#format_endpoint(path, method, details) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/transducer/formatter.rb', line 5

def format_endpoint(path, method, details)
  output = []
  output << "### #{method.upcase} #{path}"
  output << ''
  output << details['description'] if details['description']
  output << ''
  output << format_parameters(details['parameters']) if details['parameters']
  output << format_request_body(details['requestBody']) if details['requestBody']
  output << format_responses(details['responses']) if details['responses']
  output.join("\n")
end

#format_parameters(parameters) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/transducer/formatter.rb', line 17

def format_parameters(parameters)
  return '' if parameters.nil? || parameters.empty?

  output = []
  output << '**Parameters**:'
  output << ''
  output << '| Name | Location | Type | Required | Description |'
  output << '|------|----------|------|----------|-------------|'

  parameters.each do |param|
    name = param['name'] || 'N/A'
    location = param['in'] || 'N/A'
    type = extract_type(param['schema'])
    required = param['required'] ? 'Yes' : 'No'
    description = param['description'] || ''
    output << "| #{name} | #{location} | #{type} | #{required} | #{description} |"
  end

  output << ''
  output.join("\n")
end

#format_request_body(request_body) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/transducer/formatter.rb', line 39

def format_request_body(request_body)
  return '' unless request_body

  output = []
  output << '**Request Body**:'
  output << ''

  if request_body['description']
    output << request_body['description']
    output << ''
  end

  request_body['content']&.each do |content_type, details|
    output << "**Content-Type**: `#{content_type}`"
    output << ''
    output << format_schema(details['schema']) if details['schema']
    next unless details['example']

    output << '**Example**:'
    output << ''
    output << '```json'
    output << JSON.pretty_generate(details['example'])
    output << '```'
    output << ''
  end

  output.join("\n")
end

#format_responses(responses) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/transducer/formatter.rb', line 68

def format_responses(responses)
  return '' unless responses

  output = []
  output << '**Responses**:'
  output << ''

  responses.each do |status_code, response|
    output << "#### #{status_code} #{status_name(status_code)}"
    output << ''
    output << response['description'] if response['description']
    output << ''

    next unless response['content']

    response['content'].each do |content_type, details|
      output << "**Content-Type**: `#{content_type}`"
      output << ''
      if details['schema']
        output << '**Schema**:'
        output << ''
        output << '```json'
        output << JSON.pretty_generate(details['schema'])
        output << '```'
        output << ''
      end
      next unless details['example']

      output << '**Example**:'
      output << ''
      output << '```json'
      output << JSON.pretty_generate(details['example'])
      output << '```'
      output << ''
    end
  end

  output.join("\n")
end

#format_schema(schema) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/transducer/formatter.rb', line 108

def format_schema(schema)
  return '' unless schema

  output = []

  if schema['$ref']
    ref_name = schema['$ref'].split('/').last
    output << "**Schema**: `#{ref_name}`"
    output << ''
    return output.join("\n")
  end

  output << "**Type**: #{schema['type']}" if schema['type']
  output << ''

  if schema['properties']
    output << '**Properties**:'
    output << ''
    output << '| Name | Type | Required | Description |'
    output << '|------|------|----------|-------------|'

    required_fields = schema['required'] || []
    schema['properties'].each do |prop_name, prop_details|
      type = extract_type(prop_details)
      is_required = required_fields.include?(prop_name) ? 'Yes' : 'No'
      description = prop_details['description'] || ''
      output << "| #{prop_name} | #{type} | #{is_required} | #{description} |"
    end
    output << ''
  end

  output.join("\n")
end