Class: Dox::Printers::BasePrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/dox/printers/base_printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ BasePrinter

Returns a new instance of BasePrinter.



8
9
10
# File 'lib/dox/printers/base_printer.rb', line 8

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

Instance Attribute Details

#specObject (readonly)

Returns the value of attribute spec.



6
7
8
# File 'lib/dox/printers/base_printer.rb', line 6

def spec
  @spec
end

Instance Method Details

#find_or_add(hash, key, default = {}) ⇒ Object



16
17
18
19
20
# File 'lib/dox/printers/base_printer.rb', line 16

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

  hash[key] = default
end

#format_desc(description) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dox/printers/base_printer.rb', line 48

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

  if desc.end_with?('.md')
    desc = read_file(desc)
    raise "#{description} file is missing!" if desc.nil?
  end

  desc
end

#formatted_body(body_str, content_type) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/dox/printers/base_printer.rb', line 28

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

#pretty_xml(xml_string) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/dox/printers/base_printer.rb', line 39

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

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/dox/printers/base_printer.rb', line 12

def print
  raise NotImplementedError
end

#read_file(path, config_root_path: Dox.config.descriptions_location) ⇒ Object



22
23
24
25
26
# File 'lib/dox/printers/base_printer.rb', line 22

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