John
John

Reputation: 3296

Array Logic in Rails (via JSON)

I have a JSON feed that I'm parsing. Here's the following code samples:

JSON

[
   {
      "global_event":{
         "ending_at":"2011-11-07T02:00:00Z",
         "short_url":"http://bit.ly/reAhRw",
         "created_at":"2011-10-04T14:25:41Z",
         "event_responses":[

         ],
         "addresses":{
            "location":{
               "city":"blah",
               "latitude":30.205288,
               "zipcode":"343434",
               "street":"blah",
               "longitude":-95.475289,
               "state":"TX"
            }
         },
         "body":"blahblahblah",
         "euid":"2f489d0c82d167f1c16aba5d3b4c29ade6f1d52a",
         "title":"Fusion",
         "updated_at":"2011-10-04T14:26:57Z",
         "event_roles":[

         ],
         "user":{
            "long_name":"Fusion Single",
            "nickname":""
         },
         "event_items":[

         ],
         "starting_at":"2011-11-07T00:00:00Z"
      }
   }
]

Controller

def events
    @json = ActiveSupport::JSON.decode(open('jsonfeed').read)
end

View

<ul class="events">
    <% @json.each do |event| %>
        <% event.each do |e, d| %>
            <li>
                <h4><%= d['starting_at'].to_date.strftime('%A') %></h4>
                    <small><%= d['starting_at'].to_date.strftime('%b %d') %></small>
                    <p>
                        <%= link_to d['title'], d['short_url'] %> <span style="font-size: 12px; padding: 0 10px; font-weight: bold;">|</span> 
                        <%= d['starting_at'].to_date.strftime('%I:%M %p') %>
                    </p>
            </li>
        <% end %>
    <% end %>
</ul>

Finally, with all of this in place, it is working perfectly. Here's what my output looks like: enter image description here

So, finally my question: As you can see in the screenshot, both of the items being printed onto the screen have the same date (January 14). What I would like to do is have both of those combined. My final output would therefore have 'Saturday' with the two events listed underneath it. Thanks for the help!

Upvotes: 1

Views: 155

Answers (1)

Benoit Garret
Benoit Garret

Reputation: 13675

The following code should help you to group your events by date:

Controller

def events
    @json = ActiveSupport::JSON.decode(open('jsonfeed').read).group_by {|e| e["global_event"]["starting_at"].to_date.strftime('%Y-%m-%d') }
end

The resulting data structure:

{"2011-11-07"=>
  [{"global_event"=>
     {
      "ending_at"=>"2011-11-07T02:00:00Z",
      "short_url"=>"http://bit.ly/reAhRw",
      "created_at"=>"2011-10-04T14:25:41Z",
      "event_responses"=>[],
      "addresses"=>
       {"location"=>
         {"city"=>"blah",
          "latitude"=>30.205288,
          "zipcode"=>"343434",
          "street"=>"blah",
          "longitude"=>-95.475289,
          "state"=>"TX"
         }
       },
      "body"=>"blahblahblah",
      "euid"=>"2f489d0c82d167f1c16aba5d3b4c29ade6f1d52a",
      "title"=>"Fusion",
      "updated_at"=>"2011-10-04T14:26:57Z",
      "event_roles"=>[],
      "user"=>{"long_name"=>"Fusion Single", "nickname"=>""},
      "event_items"=>[],
      "starting_at"=>"2011-11-07T00:00:00Z"
     }
  }]
}

View

<ul class="events">
    <% @json.each do |date, events| %>
        <li>
            <h4><%= date.to_date.strftime('%A') %></h4>
            <small><%= date.to_date.strftime('%b %d') %></small>
                <% events.each do |e| %>
                    <p>
                        <%= link_to e['global_event']['title'], e['global_event']['short_url'] %> <span style="font-size: 12px; padding: 0 10px; font-weight: bold;">|</span> 
                        <%= e['global_event']['starting_at'].to_date.strftime('%I:%M %p') %>
                    </p>
                <% end %>
        </li>
    <% end %>
</ul>

Upvotes: 1

Related Questions