Customising your json output in Ruby on Rails 3.2

You know how it goes; you’ve found yourself developing a little application to store and display nursery rhyme lyrics because you’re a good parent and want to get the words right for your tot when you sing to them and really, who can remember all seven verses of Twinkle Twinkle Little Star. So, you scaffold something like:

rails g scaffold NurseryRhyme name:string rhyme:text

And you’re away. Typing (or in fact cut’n’pasting from Project Gutenberg). All is bliss. You nail the improvised high note right at the end of Frère Jacques and your kid smiles from ear to ear.

So now, our story breaks down. Suffice it to say you can grab your data from your auto-generated json api. And it looks something like this:

[
{"name":"Humpty dumpty","updated_at":"2012-04-11T20:07:46Z","rhyme":"Humpty dumpty sat on a wallrnHumpty dumpty had a great fallrnAll the kings horses and all the kings menrnCouldn't put humpty together againrn","id":1,"created_at":"2012-04-11T20:07:46Z"},
{"name":"Jack and Jill","updated_at":"2012-04-11T20:08:18Z","rhyme":"Jack and Jill went up the hillrnTo fetch a pail of waterrnJack fell down and broke his crownrnand Jill came tumbling after","id":2,"created_at":"2012-04-11T20:08:18Z"},
{"name":"Baa baa black sheep","updated_at":"2012-04-11T20:09:02Z","rhyme":"Baa baa black sheeprnHave you any woolrnYes sir Yes sirrnThree bags fullrnOne for the masterrnAnd one for the damernAnd one for the little boy rnWho lives down the lane","id":3,"created_at":"2012-04-11T20:09:02Z"}
]

But you want to import it into an application, that for some reason expects the json to be somewhat differently described. You need to trash the ids and dates and change “rhyme” to “lyric”. Plus, you need each rhyme nested in a “LovelyRhyme” element. Baaa (baa black sheep).

And here’s the tldr; How to customise your json output. Start by opening up your model. You should have something like this:

class NurseryRhyme < ActiveRecord::Base
  attr_accessible :name, :rhyme
end

Add in a little code, as follows:

  def as_json(options={})
     { :LovelyRhyme => { :name => name, :lyrics => rhyme } }
  end

And that will render you this:

[
{"LovelyRhyme":{"lyrics":"Humpty dumpty sat on a wallrnHumpty dumpty had a great fallrnAll the kings horses and all the kings menrnCouldn't put humpty together againrn","name":"Humpty dumpty"}},
{"LovelyRhyme":{"lyrics":"Jack and Jill went up the hillrnTo fetch a pail of waterrnJack fell down and broke his crownrnand Jill came tumbling after","name":"Jack and Jill"}},
{"LovelyRhyme":{"lyrics":"Baa baa black sheeprnHave you any woolrnYes sir Yes sirrnThree bags fullrnOne for the masterrnAnd one for the damernAnd one for the little boy rnWho lives down the lane","name":"Baa baa black sheep"}}]

Awesome. Thanks for reading.

Installing Valgrind on Shared Hosting

So you want to tinker with the goodness that is Valgrind, but you want to do it on a shared host? No problem.

# grab the latest valgrind
curl -O http://valgrind.org/downloads/valgrind-3.7.0.tar.bz2

#run an md5sum and check it matches the one listed on the downloads page
md5sum valgrind-3.7.0.tar.bz2

#unpack the puppy
tar -xjvf valgrind-3.7.0.tar.bs2

cd valgrind-3.7.0

#configure and pass in a directory you have permission to
./configure --prefix=$HOME/opt

#make valgrind
make

#install it - will end up in directory specified above
make install

Now you should have a bunch of files under your $HOME/opt directory. If you try to run it now, you’ll probably end up with an error something like

valgrind: failed to start tool 'memcheck' for platform 'blahblah-linux': No such file or directory

No problem – open up your .bash_profile (in your home directory) and add this in somewhere:

VALGRIND_LIB="$HOME/opt/lib/valgrind"
export VALGRIND_LIB

Finally, reload your .bash_profile – either log out and in again or simply

source .bash_profile

Dreamhost, Google App Engine and the naked domain forwarding fun

What are we trying to achieve:

We want a domain (both naked and with the www prefix) to point to our Google App Engine hosted project. For purposes of our demonstration, we will use the domain of my quick ‘n’ dirty project that I put together to play with GAE.

This doesn’t sound too difficult in theory. The gotcha is that google doesn’t support naked domains (i.e. the non prefixed version of the domain name) for some reason. The google documentation suggests that you add a CNAME record for your www prefixed domain to ghs.google.com and then forward or redirect your naked domain to your www prefixed domain.

I’m going to assume you have your project in GAE all set up and that you have followed google’s instructions on getting the domain name through to google apps, insane though it is that you have to do that instead of just having it all in the app engine admin side, but hey….I’ll jump through hoops for a free hosted nosql implementation.

Ok, on to the Dreamhost side – problem is, choosing the “fully hosted” option at Dreamhost, I couldn’t add a CNAME record for my domain because there is a non-editable CNAME already added by Dreamhost. Choose the “Google Hosted” option, and the www.example.com works, but I could not forward the naked domain to the www sub-domain because the domain was no longer hosted at Dreamhost and I couldn’t insert a redirect anywhere….. Arrrg!

This is what I did:

  • Enable the “DNS only” in Dreamhost.
  • Add a custom CNAME record for www.example.com.
  • Enable “Fully hosted” in Dreamhost with the “Remove WWW:” option.
  • Weyhey….now www.example.com goes to the google app engine hosted project, and example.com points to a directory at Dreamhost.
  • Last step – set up a .htaccess file in said directory to redirect the naked domain to www.example.com.

And it works!