Class: Fleetctl::TableParser

Inherits:
Object
  • Object
show all
Defined in:
lib/fleetctl/table_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ TableParser

Returns a new instance of TableParser.



11
12
13
# File 'lib/fleetctl/table_parser.rb', line 11

def initialize(raw)
  @raw = raw
end

Instance Attribute Details

#rawObject

Returns the value of attribute raw.



3
4
5
# File 'lib/fleetctl/table_parser.rb', line 3

def raw
  @raw
end

Class Method Details

.parse(raw) ⇒ Object



6
7
8
# File 'lib/fleetctl/table_parser.rb', line 6

def parse(raw)
  self.new(raw).parse
end

Instance Method Details

#parseObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fleetctl/table_parser.rb', line 15

def parse
  rows = raw.split("\n").map { |row| row.split(/\t+/) }
  header = rows.shift
  if header
    keys = header.map { |key| key.downcase.to_sym }
    [].tap do |output|
      rows.each do |row|
        scrubbed_row = row.map { |val| val == '-' ? nil : val }
        output << Hash[keys.zip(scrubbed_row)]
      end
    end
  else
    Fleetctl.logger.error('ERROR in Fleetctl::TableParser.parse - no header row found')
    []
  end
end