robertgauld/snmp_table_viewer

View on GitHub
lib/snmp_table_viewer/formatter/json.rb

Summary

Maintainability
A
0 mins
Test Coverage
module SNMPTableViewer

  # Formatting class for JSON output
  class Formatter::JSON < Formatter
    # Output the data. If no headings are provided you'll get an Array or Arrays. If headings are provided you'll get an Array of Hashes.
    # @return [String] the JSON data
    def output()
      data = @data
      if @headings.size > 0
        # Convert inner arrays to hashes
        data = data.map{ |row| Hash[row.map.with_index{ |value, index| [@headings[index] || '', value] }] }
      end
      ::JSON.generate(data)
    end
  end # JSON Formatter

end # module SNMPTableViewer