My Year of Software – A Review of 2014

Ten software lessons in 2014

Lesson 1 – Building a Rails API

In 2014, I built a backedn Rails 4.2 API using the rocket_pants gem and used another Rails application to view the API data. I learned to watch how many objects I was creating and how important it was to use performance monitoring tools like New Relic to ensure I was hitting ~200 millisecond response times to ensure a great user experience.

Lesson 2 – Vimgolf is great for teaching yourself vim one day at a time

I’ve been playing Vimgolf and learning a few vim tricks such as using gg to jump to the top of the file and Shift+G to jump to the bottom of the file.

Lesson 3 – Blogging is good for self-documenting

Blogging has been quite useful for documenting all the little tricks I’ve been using in my Rails applications, from testing elasticsearch with RSpec to saving nested attribute checkboxes with the cocoon gem.

Lesson 4 – You really learn by shipping applications that get used in production

Once you start getting customers telling you what’s broken and what can be improved, you get a bird’s eye view of not only how well you and the customer both understood the requirements and their translation into working software.

If you’ve been writing unmaintainable code, you’ll also experience the pain of trying to add new features quickly.

Lesson 5 – Portfolio applications (non-production) are great for testing new concepts

Portfolio applications are great for testing new ideas, but if they’re not being stress tested by actual users, you don’t get to the joy of writing performant code.

Lesson 6 – Sometimes big companies won’t take the risk of trying “new” technology

It could be because of tight deadlines or other developers aren’t comfortable with the latest stuff coming out. In that case, if you want to do it, hopefully you can find a good business case for it.

Lesson 7 – The only way to learn is to try

I really jumped into my first Rails application without knowing things like metaprogramming, SQL, and Vim. When I look back at how little I knew, I am sometimes amazed that anything got shipped! :)

Lesson 8 – The more I keep learning the more I realize how ignorant I am

When I look at what I consider are great programmers (Yehuda Katz, David Heinemeier Hansson, John Resig, and the list goes on…), I realize how little I know.

Lesson 9 – Logging is your best friend to ensure important data is not lost

Due to a tight shipping schedule, some controller parameter values were not being saved but I never realized it until the end user noticed it. Fortunately, we had been logging things with Papertrail, and so I was able to backfill the data.

Lesson 10 – Test Driven Development is your friend

Maybe experienced programmers like DHH (David H. Hansson) can get away without testing, but as I experienced in Lesson 9, a little testing would have ensured I didn’t have to manually backfill logged data into my Rails application database.

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

Continue Reading …

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 …