require 'kosi'

module Slacken
  class TableElement
    attr_reader :header, :columns
    def initialize(children)
      thead, tbody = children.slice(0, 2)
      @header = thead.child # tr tag
      @columns = tbody.children # tr tags
    end

    def render
      head = header.children.map(&:to_s)
      body = columns.map { |cl| cl.children.map(&:to_s) }
      table = Kosi::Table.new(header: head).render(body)
      table.to_s.chomp
    end

    def to_s
      render
    end
  end
end

require 'kosi'

module Slacken
  class TableElement
    attr_reader :header, :columns
    def initialize(children)
      if children.first.type.name == :thead
        thead, tbody = children.slice(0, 2)
        @header = thead.child # tr tag
        @columns = tbody.children # tr tags
      else
        @header = nil
        @columns = children
      end
    end

    def render
      Kosi::Table.new(table_head).render(table_body).to_s.chomp
    end

    def to_s
      render
    end

    private

    def table_head
      header ? { header: header.children.map(&:to_s) } : {}
    end

    def table_body
      columns.map { |cl| cl.children.map(&:to_s) }
    end
  end
end

Fix bug that TableElement throws an error while processing table without thead
Fix bug that TableElement throws an error while processing table without thead
