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



53
54
55
56
57
58
59
# File 'lib/dox/printers/base_printer.rb', line 53

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

  desc
end

#formatted_body(body_str, content_type) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/dox/printers/base_printer.rb', line 33

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



44
45
46
47
48
49
50
51
# File 'lib/dox/printers/base_printer.rb', line 44

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
27
28
29
30
31
# File 'lib/dox/printers/base_printer.rb', line 22

def read_file(path, config_root_path: Dox.config.descriptions_location)
  return '' unless config_root_path

  config_root_path.each do |root_path|
    file_path = File.join(root_path, path)
    next unless File.exist?(file_path)

    return File.read(file_path)
  end
end