Class: Nexmo::OAS::Renderer::Presenters::ResponseTabs
- Inherits:
-
Object
- Object
- Nexmo::OAS::Renderer::Presenters::ResponseTabs
- Defined in:
- lib/nexmo/oas/renderer/presenters/response_tabs.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#switcher ⇒ Object
readonly
Returns the value of attribute switcher.
Instance Method Summary collapse
- #examples_for_schema(schema) ⇒ Object
- #handle_all_of(schema) ⇒ Object
-
#initialize(format, response, content, endpoint, theme_light: nil) ⇒ ResponseTabs
constructor
A new instance of ResponseTabs.
- #tab_links ⇒ Object
- #tab_panels ⇒ Object
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
#format ⇒ Object (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 |
#switcher ⇒ Object (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 |
#tab_links ⇒ Object
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_panels ⇒ Object
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 |