Skip to content

Instantly share code, notes, and snippets.

@dillonhafer
Last active January 10, 2017 22:37
Show Gist options
  • Save dillonhafer/c0c0c86f533a323f9556463de0e2a2ab to your computer and use it in GitHub Desktop.
Save dillonhafer/c0c0c86f533a323f9556463de0e2a2ab to your computer and use it in GitHub Desktop.
Great way to parse tables with capybara - h/t https://github.com/briandunn
module Tabular
module TableHelper
def columns(*column_names)
map {|row| row.slice(*column_names) }
end
alias column columns
end
def hashes_from_table(selector='table')
actual = find(selector)
headers = actual.all("th").map(&:text)
actual.all("tbody tr").map do |row|
Hash[headers.zip row.all("td").map(&:text).compact.map(&:strip)]
end.extend TableHelper
end
def deedle_hash
all('dl').map do |dl|
dl.all('dt,dd').each_slice(2).map {|dt, dd| {dt.text => dd.text} }
end.flatten.reduce :merge
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment