Question

Thanks for reading. Super frustrated (and maybe missing something fundamental, I've never been great with Nokogiri)

In short - I have a source:

<div class="schedule-page">
  <h3>Sunday, September 1, 2013</h3>
  <table class="views-table cols-1 schedule-table">
  <div class="game">Game 1</div>
  <div class="game">Game 2</div>
  </table>

  <h3>Sunday, September 7, 2013</h3>
  <table class="views-table cols-1 schedule-table">
  <div class="game">Game 1</div>
  <div class="game">Game 2</div>
  <div class="game">Game 3</div>
  </table>
  <!--and so forth.... -->
</div>

I am able to iterate through the source, grab each day and X number of games, and create the container for each day and fields for each game.

<% @schedule_array.each do |a_game| %> #

<div class="game-info">

    <div class="date"><%= @the_date %></div>

    <div class="single-game"> # this pulls info for each game, works fine.
        <div class="game-home"><%= a_game.css('.field-home-team').text %></div>
        <div class="game-score"><%= a_game.css('.field-score').text %></div>
        <div class="game-away"><%= a_game.css('.field-away-team').text %></div>
        <div class="game-time"><%= a_game.css('.field-game-date-start-time').text %</div>
    </div>

</div>

<%end%>

But I really don't know how to retrieve the original date (h3) from the source and parse it in such a way so that I can use it as shown above.

I've tried a dozen variations of the example shown under Moving Nodes, here:

http://nokogiri.org/tutorials/modifying_an_html_xml_document.html

But nothing's working. Can anyone tell me the correct way to handle this? My method is gibberish and I'm not sure it's helpful to put it up.

Was it helpful?

Solution

I assume @schedule_array is coming from something like @schedule_array = Nokogiri::HTML( html_contents).css('.schedule-table').

In which case, you need to traverse back to the parent and go back to it's previous sibling and get the value:

game_date = a_game.parent.previous_element.text

So in your particular case, that line can be:

<div class="date"><%= a_game.parent.previous_element.text %></div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top