Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the general approach is:</p> <ol> <li>Create a hash for each table where the key is the employee</li> <li>Merge the results from both tables together</li> <li>Convert to JSON</li> </ol> <p><strong>Create a hash for each table where the key is the employee</strong></p> <p>This part you can do in Watir or Nokogiri. It only makes sense to use Nokogiri if Watir is giving poor performance due large tables.</p> <p>Watir:</p> <pre><code>#I assume you would have a better way to identify the tables than by index hours_table = browser.table(:index, 0) wage_table = browser.table(:index, 1) #Turn the tables into a hash employee_hours = {} hours_table.trs.drop(1).each do |tr| tds = tr.tds employee_hours[ tds[0].text ] = {"Reg Hours" =&gt; tds[1].text, "OT Hours" =&gt; tds[2].text} end #=&gt; {"Employee 1"=&gt;{"Reg Hours"=&gt;"10", "OT Hours"=&gt;"20"}, "Employee 2"=&gt;{"Reg Hours"=&gt;"5", "OT Hours"=&gt;"10"}} employee_wage = {} wage_table.trs.drop(1).each do |tr| tds = tr.tds employee_wage[ tds[0].text ] = {"Revenue" =&gt; tds[1].text} end #=&gt; {"Employee 2"=&gt;{"Revenue"=&gt;"$10"}, "Employee 1"=&gt;{"Revenue"=&gt;"$50"}} </code></pre> <p>Nokogiri:</p> <pre><code>page = Nokogiri::HTML.parse(browser.html) hours_table = page.search('table')[0] wage_table = page.search('table')[1] employee_hours = {} hours_table.search('tr').drop(1).each do |tr| tds = tr.search('td') employee_hours[ tds[0].text ] = {"Reg Hours" =&gt; tds[1].text, "OT Hours" =&gt; tds[2].text} end #=&gt; {"Employee 1"=&gt;{"Reg Hours"=&gt;"10", "OT Hours"=&gt;"20"}, "Employee 2"=&gt;{"Reg Hours"=&gt;"5", "OT Hours"=&gt;"10"}} employee_wage = {} wage_table.search('tr').drop(1).each do |tr| tds = tr.search('td') employee_wage[ tds[0].text ] = {"Revenue" =&gt; tds[1].text} end #=&gt; {"Employee 2"=&gt;{"Revenue"=&gt;"$10"}, "Employee 1"=&gt;{"Revenue"=&gt;"$50"}} </code></pre> <p><strong>Merge the results from both tables together</strong></p> <p>You want to merge the two hashes together so that for a specific employee, the hash will include their hours as well as their revenue.</p> <pre><code>employee = employee_hours.merge(employee_wage){ |key, old, new| new.merge(old) } #=&gt; {"Employee 1"=&gt;{"Revenue"=&gt;"$50", "Reg Hours"=&gt;"10", "OT Hours"=&gt;"20"}, "Employee 2"=&gt;{"Revenue"=&gt;"$10", "Reg Hours"=&gt;"5", "OT Hours"=&gt;"10"}} </code></pre> <p><strong>Convert to JSON</strong></p> <p>Based on this <a href="https://stackoverflow.com/questions/3183786/how-to-convert-a-ruby-hash-object-to-json">previous question</a>, you can then convert the hash to json.</p> <pre><code>require 'json' employee.to_json </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload