Class: Egi::Fedcloud::Cloudhound::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/egi/fedcloud/cloudhound/formatter.rb

Constant Summary collapse

FORMATS =
%w(table json plain).freeze

Class Method Summary collapse

Class Method Details

.as_json(data = []) ⇒ Object



27
28
29
30
31
32
# File 'lib/egi/fedcloud/cloudhound/formatter.rb', line 27

def as_json(data = [])
  Egi::Fedcloud::Cloudhound::Log.debug "[#{self}] Transforming #{data.inspect} into a JSON document"
  return '{}' if data.blank?

  JSON.generate(data)
end

.as_plain(data = []) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/egi/fedcloud/cloudhound/formatter.rb', line 35

def as_plain(data = [])
  Egi::Fedcloud::Cloudhound::Log.debug "[#{self}] Transforming #{data.inspect} into plain text"
  return '' if data.blank?

  plain = []
  data.each do |site|
    Egi::Fedcloud::Cloudhound::Log.warn "[#{self}] #{site[:name]} doesn't " \
                                        "have a CSIRT e-mail!" if site[:csirt].blank?
    next if site[:csirt].blank?
    plain << "#{site[:name]} <#{site[:csirt]}>"
  end

  plain.join ', '
end

.as_table(data = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/egi/fedcloud/cloudhound/formatter.rb', line 10

def as_table(data = [])
  Egi::Fedcloud::Cloudhound::Log.debug "[#{self}] Transforming #{data.inspect} into a table"

  data ||= []
  table = Terminal::Table.new

  table.add_row [' >>> Site Name <<< ', ' >>> NGI Name <<< ', ' >>> CSIRT Contact(s) <<< ']
  table.add_separator
  data.each do |site|
    table.add_separator
    table.add_row [site[:name], site[:ngi], site[:csirt]]
  end

  table
end