2013 in review

The WordPress.com stats helper monkeys prepared a 2013 annual report for this blog.

Here’s an excerpt:

A New York City subway train holds 1,200 people. This blog was viewed about 5,900 times in 2013. If it were a NYC subway train, it would take about 5 trips to carry that many people.

Click here to see the complete report.

List of open files in your Rails Environment

Sometimes when you are processing a huge number of files, you might need end up having the error “Too many open files”. I recently bumped into a similar issue, and the following code helped me solve the issue

ObjectSpace.each_object(File) do |f|
if f.path.include?("rails_app_directory/tmp/uploads") && !f.closed?

The above code looks for open file descriptors from the desired directory and closes them. You can definitely change the file path according to your needs. This basically helps you keep the number of open files in your application in control, and in turn also improves the performance of your servers.

Do not depend on rubygems.org when in production

We bumped into an issue lately, while deploying one of our major app in the production environment. Just when we tried to run the command

bundle install

we realized that the rubygems repository was facing a downtime. Hence we were not able to install gems on our production server, which in turn caused a downtime on our side.

In order to overcome this issue we took the decision of caching our gems into our application’s repository(i.e. vendor/cache directory). Below are the steps that we took:

Step 1: On your local machine run the command below

$ bundle package

this command will copy all the ‘.gem’ files and their respective dependencies in your bundle into the ‘vendor/cache’ directory of your rails application.

Step 2: Now after committing your code and running the bundle install command on your production servers, run the command below:

$ bundle install --local

The `–local` parameter will fetch the gems only inside the ‘vendor/cache’ directory of your rails application. Hence not hitting the rubygems servers.

This proves to be very helpful in cases where you want quick deployments on your servers.

Changing SMTP Settings at Runtime in Your Rails App

Posted another blog post for raw engineering blog, which helps you to manage your smtp settings dynamically


JSON responses for 404 and 500 errors in rails

Rails proves to be very much helpful in creating REST APIs. Hence, while you are creating a REST API you would never want to render HTML pages for your 404(page not found) and 500(internal server) errors. This post will help you to return JSON responses instead of the default HTML templates for the same.

Prior to rails 3 you could do this easily and write the code below inside your application controller.

class ApplicationController < ActionController::Base
  rescue_from(ActionController::RoutingError) {
    render :json => {:error_message => "The resource you were looking for does not exist"}
  rescue_from(Exception) {
    render :json => {:error_message => "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."}

But with the advent of ActionDispatch in Rails 3.0 all the routing has been moved to rack DSL. Hence, now we cannot catch the ActionController::RoutingError inside of our ApplicationController, as the rack DSL will automatically give the response to the client even before the request reaches your code.

So, in order to achieve the functionality we need, we will have to change the way the routing exceptions are handled inside ActionDispatch::ShowExceptions

Step 1: Create a initializer file show_exceptions.rb inside config/initializers directory, and use the code blow:

# A Custom Exception Handler
# This will call the ErrorsController when a 404 or 500 response is to be sent

require 'action_dispatch/middleware/show_exceptions'

module ActionDispatch
  class ShowExceptions
      def render_exception_with_template(env, exception)
        body = ExceptionsController.action(rescue_responses[exception.class.name]).call(env)
        body[1]['Content-Type']= "application/json; charset=utf-8"
          render_exception_without_template(env, exception)

      alias_method_chain :render_exception, :template

This will give a call to the appropriate exception handler action inside of ExceptionsController.

Step: 2: Create ErrorsController, and use the code below

class ErrorsController < ApplicationController
  ERRORS = [

  ERRORS.each do |e|
    define_method e do
      message = e == :not_found ? "The request resource was not found" : "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."

      respond_to do |format|
        format.any {render :json => {:error_message => message}, :status => e}

That’s all that is required to give a custom json response for 404 and 500 errors for your restful rails applications.

Integrate Google Search Appliance with Rails Application

If you ever want to integrate a Google Search Appliance(GSA) into your rails application, try using the gem rails-gsa.

You can simply download it with the command

gem install rails-gsa

Then after including it in you Gemfile you can start communicating with your GSA. This gem uses the API’s provided by the GSA and provides you search results.

Check out the complete documentation of the gem at https://github.com/rohit9889/rails-gsa