Class: SoftLayer::CLIFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/softlayer/cli/formatter.rb

Class Method Summary collapse

Class Method Details

.csv(content, headers, output_file = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/softlayer/cli/formatter.rb', line 13

def self.csv(content, headers, output_file = nil)
  require 'csv'
  csv_str = CSV.generate do |csv|
    csv << headers.map { |c| c.upcase }
    content.each do |row|
      csv << row
    end
  end

  CLISerializer.serialize(csv_str, output_file)
end

.format(content, format = 'plain', headers = CLI::COLUMNS, output_file = nil) ⇒ Object



3
4
5
# File 'lib/softlayer/cli/formatter.rb', line 3

def self.format(content, format = 'plain', headers = CLI::COLUMNS, output_file = nil)
  send(format.to_sym, content, headers, output_file)
end

.json(content, headers, output_file = nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/softlayer/cli/formatter.rb', line 25

def self.json(content, headers, output_file = nil)
  json_obj = []
  content.each do |row|
    json_obj << Hash[*headers.zip(row).flatten.compact]
  end

  CLISerializer.serialize(JSON.generate(json_obj), output_file)
end

.plain(content, headers, output_file = nil) ⇒ Object



7
8
9
10
11
# File 'lib/softlayer/cli/formatter.rb', line 7

def self.plain(content, headers, output_file = nil)
  require 'terminal-table'
  table = Terminal::Table.new(headings: headers.map { |c| c.upcase }, rows: content)
  CLISerializer.serialize(table, output_file)
end