Class: ConsoleUtils::JSONOutput::BaseFormatter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/console_utils/json_output/base_formatter.rb

Overview

The abstract singleton class for a prettified JSON formatting.

Direct Known Subclasses

DefaultFormatter, JqFormatter

Instance Method Summary collapse

Instance Method Details

#call(body) ⇒ Object

Prints formatted JSON to stdout.


8
9
10
11
12
13
14
15
# File 'lib/console_utils/json_output/base_formatter.rb', line 8

def call(body) # :yields:
  formatted = format_with_fallback(body)
  if block_given?
    yield(formatted)
  else
    puts formatted
  end
end

#format(body) ⇒ Object

Formats a given JSON string

Raises:

  • (NotImplementedError)

18
19
20
# File 'lib/console_utils/json_output/base_formatter.rb', line 18

def format(body)
  raise NotImplementedError
end

#format_with_fallback(body) ⇒ Object


22
23
24
25
26
27
# File 'lib/console_utils/json_output/base_formatter.rb', line 22

def format_with_fallback(body)
  format(body)
rescue ParseError => error
  warn error
  return body.to_s
end