Last active
January 10, 2017 22:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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