Sha256: c77dc860608f0b9ebf07c5674b38bfb368f2f1ee8a81de059fb3083054e153a3

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'rexml/document'

module Dox
  module Printers
    class BasePrinter
      attr_reader :spec

      def initialize(spec)
        @spec = spec || {}
      end

      def print
        raise NotImplementedError
      end

      def find_or_add(hash, key, default = {})
        return hash[key] if hash.key?(key)

        hash[key] = default
      end

      def read_file(path, config_root_path: Dox.config.descriptions_location)
        full_path = Util::File.file_path(path, config_root_path: config_root_path)

        full_path.nil? ? nil : File.read(full_path)
      end

      def formatted_body(body_str, content_type)
        case content_type
        when %r{application\/.*json}
          JSON.parse(body_str)
        when /xml/
          pretty_xml(body_str)
        else
          body_str
        end
      end

      def pretty_xml(xml_string)
        doc = REXML::Document.new(xml_string)
        formatter = REXML::Formatters::Pretty.new
        formatter.compact = true
        result = ''
        formatter.write(doc, result)
        result
      end

      def format_desc(description)
        desc = description
        desc = '' if desc.nil?
        desc = read_file(desc) if desc.end_with?('.md')

        desc
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dox-2.4.0 lib/dox/printers/base_printer.rb