Fun On Rails

Journal of a Web Developer #ruby #rails #JS

Grape API authentication using Devise Auth Token

| Comments

In previous post we saw how to buil RESTful API using Grape. In this post we will see how to add devise auth token to users and how to use it in Grape API authentication.

Lets see how this can be done assuming you already have devise setup ready.

Add token_authenticable to devise modules (works with devise versions <=3.2)

In user.rb add :token_authenticatable to the list of devise modules, it should look something like below:

class User < ActiveRecord::Base
# ..code..
  devise :database_authenticatable,

  attr_accessible :name, :email, :authentication_token

  before_save :ensure_authentication_token
# ..code..

Building RESTful API using Grape in Rails

| Comments

While developing a rich client side web application or mobile app, we need RESTful JSON API which interacts with the front-end javascript framework. Here you may use backbone.js, ember.js or angular.js on the front-end side of application.

Here we’ll be using Ruby on Rails on the back-end which will serve JSON API consumable by fron-end framework. If you look at the ruby toolbox you’ll see many API Builder gems available but it seems grape can be a good choice.

Grape is a RESTful API microframework built to easily and quickly produce APIs for Ruby-rooted web applications.

Let’s see how we can build RESTful JSON apis using Grape library:

Understanding RESTful vs SOAP web services

A Web service is a method of communications between two electronic devices over the World Wide Web. It is also defined as a software system designed to support interoperable machine-to-machine interaction over a network.

In simple words, “Web services are developed to use by other software applications”

Web services communicate via either SOAP or REST messages.


REST stands for REpresentational State Transfer. It is a term coined by Roy Fielding in his Ph.D. dissertation It is an architecture style for designing networked applications which typycally runs runs over HTTP.

REST is an architecture and not a “standard”. There is no w3c recommendation for REST. It is concept for better web and widely accepted.

RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.

It expects client to have knowlege of what to send and what to expect in response. It is focused on accessing named resources through a single consistent interface.


  1. It is simple
  2. It is lightweight and faster
  3. Platform-independent
  4. Language-independent
  5. runs over HTTP
  6. Response can be returned in “XML/JSON” format

regex to split string within quotes and spaces

I wanted to split a string based on white spaces except when if anything is enclosed inside quotes then i don’t want to split that piece of string from getting splitted.

It means string will be splitted based on consecutive double quotes from left to right and remaining string will be spitted on spaces.

Migrating from blogspot to octopress

| Comments

I was happily writing my blog at blogspot almost three plus years. From last 2-3 days, i was evaluating with octopress for its way of writing posts, customizations, themes, and plugins. As a result of evaluation, found octopress blog as very quick & easy to write hence decided to shift from blogger to octopress.

Below post explains steps involved in migration:

Export content from blogger

First, you need to export your posts from blogger. Goto blogger admin area and then go to settings >> other >> Export Blog and click on it. Please wait sometime and popup will comeup allowing you to download blog in xml format.

If you still finding some problem with exporting then see here

Getting started: Octopress powered and Github hosted blog

There are various blogging sites like blogspot, wordpress, posterous, and many more. Each has their own pros and cons and none of the engine gives us full power of ehhance our blogging experience. Anyone who has atleast some technical knowledge can start with octopress powered blog. This gives us the power of freedom of doing anything. So, Lets first understand about octopress framework and its features.


Octopress is a framework designed by Brandon Mathis for Jekyll, the blog aware static site generator. If you wanted to know more then it can be found here

what is new in rails 3

| Comments

Rails 3 has released with various notable features and said that it’s one of the best version rails community has released so far.

Highlights of major features:

1. Brand new router with an emphasis on RESTful declarations

2. New Action Mailer API modeled after Action Controller

3. New Active Record chainable query language built on top of relational algebra

4. Unobtrusive JavaScript helpers with drivers for Prototype, jQuery, and more coming (end of inline JS)

5. Explicit dependency management with Bundler

Dynamic conditions to rails associations

We all know that rails models associations gets defined while class definitions are loaded and once defined can’t be changed.

But still you can make use of block parameter to conditions to have dynamic query conditions inside associations.

Below line explains how to define dynamic associations :

has_one :code_sequence, :class_name => 'Sequence', :conditions => 'kind = "#{self.kind}"'

Please make a note that below code will not work :

has_one :code_sequence, :class_name => 'Sequence', :conditions => proc { |c| ['kind = ?', c.kind] }

Upgrading from Rails 2.1.x to Rails 2.3.11

If your application is currently on any version of Rails 2.1.x, Then following changes needs to be done for upgrading your application to Rails 2.3.11

1. First install Rails version 2.3.11

gem install rails -v2.3.11

2. Freeze app ruby gems

rake rails:freeze:gems

Hopefully it should work for you but it gave me following error

undefined method `manage_gems’ for Gem:Module

3. Create sample rails 2.3.11 app

rails _2.3.11_ testsapp