Class: Nexmo::OAS::Renderer::Presenters::ResponseTabs

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo/oas/renderer/presenters/response_tabs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, response, content, endpoint, theme_light: nil) ⇒ ResponseTabs

Returns a new instance of ResponseTabs.



13
14
15
16
17
18
19
20
# File 'lib/nexmo/oas/renderer/presenters/response_tabs.rb', line 13

def initialize(format, response, content, endpoint, theme_light: nil)
  @format   = format
  @response = response
  @content  = content
  @endpoint = endpoint
  @theme_light = theme_light
  @switcher ||= @response.schema(@format)['x-switcher']
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



11
12
13
# File 'lib/nexmo/oas/renderer/presenters/response_tabs.rb', line 11

def format
  @format
end

#switcherObject (readonly)

Returns the value of attribute switcher.



11
12
13
# File 'lib/nexmo/oas/renderer/presenters/response_tabs.rb', line 11

def switcher
  @switcher
end

Instance Method Details

#examples_for_schema(schema) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nexmo/oas/renderer/presenters/response_tabs.rb', line 47

def examples_for_schema(schema)
  # If there are any examples, show them
  examples = @response.raw.dig('content', @format, 'examples')
  return nil unless @content == :responses && @format == 'application/json' && examples

  example_switcher = Nexmo::OAS::Renderer::Presenters::ContentSwitcher.new(format: format, force_type: 'dropdown', theme_light: @theme_light)

  has_visible_panel = false
  examples.each_with_index do |v, _k|
    # Only if the example key is listed in x-examples in the schema
    next unless schema['x-examples']&.include?(v[0])

    response = JSON.neat_generate(v[1], {
        wrap: true,
        after_colon: 1,
    })

    content = <<~HEREDOC
      <pre class="pre-wrap language-json #{@theme_light ? 'Vlt-prism--dark' : ''} Vlt-prism--copy-disabled"><code>#{response}</code></pre>
    HEREDOC

    example_switcher.add_content(
      title: v[0].titleize,
      content: content,
      tab_id: v[0],
      active: !has_visible_panel
    )
    has_visible_panel = true
  end
  return example_switcher if example_switcher.panels.size.positive?
end

#handle_all_of(schema) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/nexmo/oas/renderer/presenters/response_tabs.rb', line 79

def handle_all_of(schema)
  if schema['allOf']
    schema['allOf'].each do |p|
      schema.deep_merge!(p)
    end
    schema.delete('allOf')
  end
  schema
end


22
23
24
25
26
27
28
29
30
# File 'lib/nexmo/oas/renderer/presenters/response_tabs.rb', line 22

def tab_links
  @tab_links ||= @response.split_schemas(@format).map.with_index do |schema, index|
    schema = handle_all_of(schema)
    ResponseTab::Link.new(
      index: index,
      schema: schema
    )
  end
end

#tab_panelsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nexmo/oas/renderer/presenters/response_tabs.rb', line 32

def tab_panels
  @tab_panels ||= @response.split_schemas(@format).map.with_index do |schema, index|
    schema = handle_all_of(schema)
    examples = examples_for_schema(schema)
    ResponseTab::Panel.new(
      schema: schema,
      index: index,
      format: @format,
      content: examples || @content,
      endpoint: @endpoint,
      theme_light: @theme_light
    )
  end
end