Do the Monster Mash!
Working on several API wrappers lately, one of my favorite gems is Mash from Michael Bleigh. Much like OpenStruct, Mash is basically a Hash with dot notation.
But unlike OpenStruct, you can pass a Hash to Mash and it will recursively do a deep dive and Mashify it. Now you get pretty accessor methods for all those parsed hashes from your JSON API calls:
# Return tweet referencing a URL
results = Twitterland::BackTweets.search('http://squeejee.com', 'OU812')
results.tweets.size
=> 25
results.tweets.first.from_user
=> "euromarianne"
results.items_per_page
=> 25
results.total_results
=> 3301
i_am_ron_burgandy?
Mash includes a couple of helpful methods for traversing those deep hashes when you’re not certain if a key has been set:
mash = Mash.new
mash.first_name?
=> false
mash.first_name = 'Wynn'
mash.first_name?
=> true
The ! method provides multi-level assignment, kind of like mkdir -p for hashes:
mash = Mash.new
mash.these_droids!.will_do_nicely = true
Install the gem and mash it up
sudo gem install mash
— posted by Wynn Netherland // @pengwynn