How can I most efficiently iterate over a hash of hashes in order based on key value in inner hash?

StackOverflow https://stackoverflow.com/questions/12002407

Question

I've got a JSON hash of hashes returned by a website API that I want to parse and display based on a specific key's value within the internal hashes.

I can think of solutions that would achieve this, but they would take a number of lines of code and don't seem efficient. Surely there must be a way to natively in Rails, given the focus on convention over configuration. I've googled around a bit, but found nothing that covers this issue.

Sample Response from API:

[{"banner":"01197271","birthday":"1991-01-11","committee_id":1,"created_at":"2012-08-08T01:56:02-05:00","email":"me@example.com","first_name":"Dan","graduation_date":"May 2013","hometown":"San Antonio","hours_enrolled":15,"id":2,"image":{"url":null,"thumb":{"url":null},"large":{"url":null}},"invitation_accepted_at":null,"invitation_limit":null,"invitation_sent_at":null,"invitation_token":null,"invited_by_id":null,"invited_by_type":null,"last_name":"Tester","local_apt":"","local_city":"San Antonio","local_state":"Texas","local_street":"One UTSA Circle","local_zip":"78249","major":"Computer Science","permanent_apt":"","permanent_city":"","permanent_state":"","permanent_street":"One UTSA Circle","permanent_zip":"","phone":"5558813284","same_address":true,"tour_trained":false,"updated_at":"2012-08-17T03:35:26-05:00","utsa_id":"uoi431"},
{"banner":"","birthday":"1990-10-25","committee_id":null,"created_at":"2012-08-03T16:19:23-05:00","email":"you@example.com","first_name":"Test","graduation_date":null,"hometown":null,"hours_enrolled":null,"id":1,"image":{"url":null,"thumb":{"url":null},"large":{"url":null}},"invitation_accepted_at":null,"invitation_limit":null,"invitation_sent_at":null,"invitation_token":null,"invited_by_id":null,"invited_by_type":null,"last_name":"User","local_apt":"","local_city":"","local_state":"","local_street":"","local_zip":"","major":null,"permanent_apt":"","permanent_city":"","permanent_state":"","permanent_street":"","permanent_zip":"","phone":"","same_address":false,"tour_trained":false,"updated_at":"2012-08-15T10:05:54-05:00","utsa_id":""}]

Potential solution would be to go through each internal hash, determining value of relevant key value, then store, based on where the key value places it compared to already tested hashes. When complete, return.

Was it helpful?

Solution

Ok so if you have objects that are set up to parse this information, those objects can build themselves based off the parameters of your hash. So you could do something like this

object = MyObject.create(your_hash_parameters)

Where your_hash_parameters are the parameters that you presented in your example.

I'm not sure what would happen if there were more paramaters than your object knew what to do with, if it would still build itself or not. If that is the case, you could use the delete_if method to exclude attributes that are unwanted.

One more note, if this isn't something that you want saved to your database, and its only to display temporary information. I would set up a model with attr_accessors that represent the attributes that you are displaying.

OTHER TIPS

As told in comment, I'd create an ActiveResource object and add relevant methods to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top