Camel Casing Your API Response in rocket_pants

What is rocket_pants?

Rocket pants is a gem that gives you a set of tools for building an API in Ruby with Rails.

Motivation for camel casing

The developer team decided we wanted to return camel-cased json responses, e.g.,

1
2
 
3
{ myResponse: "My response" }
4
 
5

Steps for camel casing

Since our Rails model attributes use the traditional snake case naming and rocket_pants by default in its response returns the default attributes, I needed a way to camel case the attribute names that were
returned in the response.

I’m omitting the steps you need (perhaps I’ll cover it in a future post if there’s demand for it), to install rocket_pants, but that’s pretty straightforward for the most part via the project’s README file.

Step 1 – Override the attributes method in your model.rb file

01
02
class Trial < ActiveRecord::Base
03
 
04
  def attributes
05
    info = { 'id' => nil, 'firstName' => nil, 'lastName' => nil,
06
'fullName' => nil }
07
    super.replace info
08
  end
09
 
10
end
11

Instead of replace, you could use merge but I used replace because replace ensures the API response when you call the expose method from rocket_pants only contains the attributes in the info hash. If I had used replace, then I would have gotten all the rest of the Trial model attributes.

If you need different responses for specific API versions, then you could override the serializable hash as indicated in this stackoverflow post

Step 2 – camelize the model attributes using metaprogramming

1
2
  Trial.attribute_names.each do |attribute_name|
3
    unless attribute_name == attribute_name.camelize(:lower)
4
      define_method attribute_name.camelize(:lower) do
5
        self.send(attribute_name) 
6
      end
7
    end
8
  end
9

In step 2, unless the model attribute is already camel-cased, I create one using define_method along with Rails’ camelize method.

Step 3 – camel case any method names being passed in the API response

01
02
 
03
class Trial < ActiveRecord::Base
04
 
05
   def fullName
06
     first_name + last_name
07
   end
08
 
09
end
10
 
11

Notice how I have a camel-cased method called fullName which concatenates the first_name and _last_name attribute values for the Trial model.

This is how you can customize your API response via rocket_pants.

Testing Elasticsearch In Your Rails 4 Application

What is Elasticsearch?

Elasticsearch is an open-source real-time search and analytics engine that runs on top of Lucene, a Java-based indexing and search library.

If you haven’t setup elasticsearch with your Rails application, you can read about how to do it in these articles:

Steps to setup your RSpec tests

Step 1: Configure spec_helper.rb with a truncation strategy

Below is a copy of my spec_helper.rb setup. The important part is the
config.around :each, elasticsearch: true block

Step 2: Here’s a snapshot of my database_cleaner.rb

Below is a copy of my database_cleaner.rb setup.

Step 3: Use a tag to denote your elasticsearch tests

Below is a gist copy of a post_specs.rb file. Note that I’m calling the “posts” action of the API controller (Posts) which then runs a search against the Post model using ElasticSearch.

Note the use of the elasticsearch and commit tags. I’m also calling
sleep to allow a delay so the test index has time to get built. I
don’t like this solution, but for the moment it’s working and I’ll
update this post if I find a better way.

How to Implement jQuery Colorbox

How to Implement jQuery ColorboxWhat is jQuery Colorbox?jQuery Colorbox is a lightbox plugin that supports “photos, grouping, slideshow, ajax, inline, and iframed content.” In a nutshell, you can create nice looking popup boxes

Continue Reading …

How to Add jQuery UI Datepicker To Your Rails 4 Application With Formtastic and Cocoon

What is Formtastic, Cocoon, and jQuery UI Datepicker?Formtastic is a gem that lets you add forms more easily to your Rails application. Cocoon allows you to add dynamic nested forms to your Rails application using jQuery. jQuery-ui Datepicker

Continue Reading …

How To Move an Existing Github Repository to Bitbucket

Moving an Existing Github Repository to BitbucketHere is the link to the procedure on coderwall…

Continue Reading …

How To Save Nested Attribute Checkboxes With Formtastic and Cocoon and Avoid the TypeError

Avoiding the TypeError: can’t cast Array to stringRecently, I was inserting nested attribute checkboxes using Formtastic and Cocoon and was cruising along with all my other nested forms partials until I got a dreaded TypeError: can’t cast Array

Continue Reading …

How To Setup jQuery Autcomplete With Elasticsearch In Your Rails Application

What is jQuery autocomplete? The jQuery autocomplete plugin enables your users to receive suggestions while they type keywords in an input field. How do you use it with elasticsearch? If you haven’t setup elasticsearch with your Rails application,

Continue Reading …

How to Setup Elasticsearch in your Rails App in Production

Step 1 - Install elasticsearch in your Rails Application in ProductionIf you haven’t setup elasticsearch in your Rails application in development, this article tells you how to do so in LinuxStep 2 - Create the search indices on your Heroku

Continue Reading …

How To Setup Elasticsearch In Your Rails Application In Development

Step 1 - Install elasticsearchIf you haven’t installed elasticsearch on your local development machine, this article tells you how to do so in LinuxStep 2 - Add the right ruby gems for the Ruby elasticsearch client in your Gemfile and bundle

Continue Reading …

How To Install Elasticsearch On Linux for Rails Development

What is elasticsearch?Elasticsearch is an open-source real-time search and analytics engine that runs on top of Lucene, a Java-based indexing and search library.How to install it on Linux Mint 16 and Ubuntu 14.04Step 1 - Download

Continue Reading …