Sha256: fbbabdb25d18089f9ddde9b48528edf1a60d51d97eecca869b538d9117f852c0

Contents?: true

Size: 1.61 KB

Versions: 31

Compression:

Stored size: 1.61 KB

Contents

require 'rouge'
require 'neatjson'
require_relative '../services/oas_parser'

module Nexmo
  module OAS
    module Renderer
      class ResponseParserDecorator < ::OasParser::ResponseParser
        def formatted_json
          JSON.neat_generate(parse, {
            wrap: true,
            after_colon: 1,
          })
        end

        def formatted_xml(xml_options = {})
          xml_options[:root] = xml_options['name'] if xml_options
          xml_string = xml(xml_options)
          xml_string.gsub!(%r{^(\s+?)(<(?:\w|\=|\"|\_|\s)+?\>)(.+?)(</.+?>)}).each do |s|
            indentation = $1
            indentation_plus_one = "#{$1}  "
            opening_tag = $2
            content = $3
            closing_tag = $4

            next(s) if (indentation.size + opening_tag.size + content.size) < 60

            next "#{indentation}#{opening_tag}\n#{indentation_plus_one}#{content}\n#{indentation}#{closing_tag}"
          end

          xml_string
        end

        def html(format = 'application/json', xml_options: nil)
          formatter = Rouge::Formatters::HTML.new

          case format
          when 'application/json'
            lexer = Rouge::Lexer.find('json')
            highlighted_response = formatter.format(lexer.lex(formatted_json))
          when 'text/xml', 'application/xml'
            lexer = Rouge::Lexer.find('xml')
            highlighted_response = formatter.format(lexer.lex(formatted_xml(xml_options)))
          end

          output = <<~HEREDOC
      <pre class="highlight json"><code>#{highlighted_response}</code></pre>
          HEREDOC

          output
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
nexmo-oas-renderer-0.8.0 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.7.1 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.7.0 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.6.6 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.6.5 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.6.4 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.6.3 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.6.2 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.6.1 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.5.6 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.5.5 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.5.4 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.5.2 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.5.1 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.5.0 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.4.1 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.4.0 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.3.7 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.3.6 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb
nexmo-oas-renderer-0.3.5 lib/nexmo/oas/renderer/decorators/response_parser_decorator.rb