Tropical Software Observations

01 June 2011

Posted by Anonymous

at 10:19 AM



Using RABL in Rails JSON Web API

Let's use an event management app as the example.

The app has a simple feature: a user can add some events, then invite other users to attend the event. Its data are represented in 3 models: User, Event, and Event Guest.

Let's say we are going to add a read-only JSON web API to allow clients to browse data records.


Model is not view

When working on a non-trivial web API, you will soon realize that models often cannot be serialized directly in a web API.

Within the same app, one API may need to render a summary view of the model, while another needs a detail view of the same model. You want to serialize a view or view object, not a model.

RABL (Ruby API Builder Language) gem is designed for this purpose.

Define once, reuse everywhere

Let's say we need to render these user attributes: id, username, email, display_name, but not password.

In RABL, we can define the attribute whitelist in a RABL template.

# tryrabl/app/views/users/base.rabl
attributes :id, :username, :email, :display_name

To show an individual user, we can now reuse the template through RABL extends.
# tryrabl/app/views/users/show.rabl
extends "users/base"
object @user

## JSON output:
# {
#     "user": {
#         "id": 8,
#         "username": "blaise",
#         "email": "",
#         "display_name": "Ms. Noe Lowe"
#     }
# }

Here's another example to show a list of users.
# tryrabl/app/views/users/index.rabl
extends "users/base"
collection @users

## JSON output:
# [{
#     "user": {
#         "id": 1,
#         "username": "alanna",
#         "email": "",
#         "display_name": "Mrs. Gaylord Hoeger"
#     }
# }, {
#     "user": {
#         "id": 2,
#         "username": "jarrell.robel",
#         "email": "",
#         "display_name": "Oran Lebsack"
#     }
# }]

The template can be reused in nested children as well, through RABL child.
attributes :id, :title, :description, :start, :end, :location
child :creator => :creator do
extends 'users/base'

## JSON output:
# {
#     "event": {
#         "id": 7,
#         "title": "Et earum sed fuga.",
#         "description": "Quis sed ..e ad.",
#         "start": "2011-05-31T08:31:45Z",
#         "end": "2011-06-01T08:31:45Z",
#         "location": "Saul Tunnel",
#         "creator": {
#             "id": 1,
#             "username": "alanna",
#             "email": "",
#             "display_name": "Mrs. Gaylord Hoeger"
#         }
#     }
# }

Join table rendered as subclass

I notice a recurring pattern in two recent projects. For instance, in this example, from the client's point of view, Event Guest is basically a User with an additional attribute: RSVP status.

When querying the database, usually we need to query the join table: event_guests.
class GuestsController < ApplicationController
def index
@guests = EventGuest.where(:event_id => params[:event_id])

But when rendering, the result set needs to be rendered as a list of Users. RABL allows you to do that easily, using its glue feature (a weird name though :).
# tryrabl/app/views/guests/index.rabl
collection @event_guests

# include the additional attribute
attributes :rsvp

# add child attributes to parent model
glue :user do
extends "users/base"

## JSON output:
# [{
#     "event_guest": {
#         "rsvp": "PENDING",
#         "id": 3,
#         "username": "myrna_harvey",
#         "email": "",
#         "display_name": "Savion Balistreri"
#     }
# }, {
#     "event_guest": {
#         "rsvp": "PENDING",
#         "id": 4,
#         "username": "adelle.nader",
#         "email": "",
#         "display_name": "Edgardo Dickens"
#     }
# }]

The complete Rails example code is available at


Nathan said...

Thanks for posting about RABL! Great explanation of common patterns and how to use it.

Naviya Nair said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

priya said...

This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 
Data Science training in rajaji nagar
Data Science with Python training in chennai
Data Science training in electronic city
Data Science training in USA
Data science training in bangalore

jorick228 said...

registering in an online casino my life has become better great top casino good luck walks here

service care said...

very good post!!! Thanks for sharing with us... It is more useful for us..I am amazed by the way you have explained things in this post. This post is quite interesting and i am looking forward to read more of your posts.
motorola mobile service centre in chennai
moto g service center in chennai
motorola service center in velachery

Madhu Balan said...

Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
Java Training in Chennai
Java Training in Coimbatore
Java Training in Bangalore

shwethapriya said...

Excellent idea!!! I am really enjoy to read your post and I like it.
Informatica Training in Chennai
Best Informatica Training center In Chennai
pearson vue test center in chennai
IoT Training in Chennai
Xamarin Training in Chennai
Node JS Training in Chennai
Informatica Training in Tambaram
Informatica Training in OMR

Riya Raj said...

Wonderful blog!!! Thanks for sharing this great information with us...
SEO Training in Chennai
SEO Course in Chennai
SEO Training Institute in Chennai
Best seo training in chennai
SEO training in Guindy
SEO training in Tambaram
Python Training in Chennai
Big data training in chennai
Digital marketing Course in chennai
JAVA Training in Chennai

vinudevan said...

Thanks for sharing informative article with us...
Hibernate Training in Chennai
Hibernate course in Chennai
Hibernate Training institute in Chennai
hibernate training in T nagar
hibernate training in Guindy
Spring Training in Chennai
clinical sas training in chennai
DOT NET Training in Chennai
QTP Training in Chennai
LoadRunner Training in Chennai

gowsika said...

Great job. Keep updating this article by posting new informations.
Air Hostess Academy Bangalore
Aviation Institute in Bangalore
Airport Management in Bangalore
Ground Staff Training in Bangalore
Air Hostess Training in Chennai
Air Hostess Training in Chennai
Air Hostess Training Institute in chennai
Air Hostess Training in Bangalore
Aviation Courses in Chennai
Aviation Institute in Bangalore

shwethapriya said...

Nice blog and valuable for all people. Thank you for posting this.
Informatica MDM Training in Chennai
Informatica mdm training
german classes
Best IELTS Coaching in Chennai
learn Japanese in Chennai
IoT Training in Chennai
Informatica MDM Training in Porur
Informatica MDM Training in Adyar

Sivanandhana Girish said...

An extremely informative and interesting blog among all you have shared. Update it frequently.
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Coaching in Anna Nagar
Spoken English Class in Anna Nagar

Sadhana Rathore said...

Fabulous post admin, hats off to your creativity. I got lots of information here.
Web Designing Course in chennai
web designing training in chennai
Web Designing Institute in Chennai
Ethical Hacking Training in Chennai
PHP Training in Chennai
Angular 6 Training in Chennai
Web Designing Course in Porur
Web Designing Course in Tambaram
Web Designing Course in Adyar

silakarim said...

Thanks for sharing this post. It is very helpful for me to develop my skills in a right way.
Ethical Hacking Course in Velachery
Ethical Hacking Course in T Nagar
Ethical Hacking Course in Tambaram
Ethical Hacking Course in Anna Nagar
Ethical Hacking Course in Porur
Ethical Hacking Course in Vadapalani
Ethical Hacking Course in Thiruvanmiyur
Ethical Hacking Course in Adyar
Ethical Hacking Course in OMR

Aruna Ram said...

Surpassing Article, This post contains great information about this topic. Can you please do a more extra post like from this blog and Thank you much more for sharing this useful info.

Primavera Training in Chennai
Primavera Software Training in Chennai
Advanced Excel Training in Chennai
Oracle DBA Training in Chennai
Embedded System Course Chennai
Unix Training in Chennai
Linux Training in Chennai
Pega Training in Chennai
Tableau Training in Chennai
Primavera Training in Thiruvanmiyur
Primavera Training in Vadapalani

manjuragul said...

Very nice post with lots of information. Thanks for sharing this updates.
Software testing training in Anna Nagar
Software Testing Training in Chennai
Software testing training in OMR
ccna Training in OMR
java training in T nagar
Software testing training in T Nagar
ccna Training in T Nagar
DevOps Training in OMR

Patell Priya said...

Nice post...! I am eagerly waiting for your new updates...
Embedded System Courses in Chennai
Embedded Training in Chennai
Advanced Excel Training in Chennai
Soft Skills Training in Chennai
JMeter Training in Chennai
Appium Training in Chennai
job Openings in chennai
Embedded Training in OMR
Embedded Training in Adyar

gokul said...

Thank you for this informative blog
Top 5 Data science training in chennai
Data science training in chennai
Data science training in velachery
Data science training in OMR
Best Data science training in chennai
Data science training course content
Data science certification in chennai
Data science courses in chennai
Data science training institute in chennai
Data science online course
Data science with python training in chennai
Data science with R training in chennai

Pravin said...

If you are looking for the best b2b data companies then we are providing you the best database or business directory. If you want, we can help each other in the process process or searching for sales automation then we can also help you here also to provide free marketing automation tools.

raj khan said...

Soma pill is very effective as a painkiller that helps us to get effective relief from pain. This cannot cure pain. Yet when it is taken with proper rest, it can offer you effective relief from pain.
This painkiller can offer you relief from any kind of pain. But Soma 350 mg is best in treating acute pain. Acute pain is a type of short-term pain which is sharp in nature. Buy Soma 350 mg online to get relief from your acute pain.

Buy Soma 350 mg
Soma Pill
Buy Soma 350 mg online

Buy Soma 350 mg online
Soma Pill
Buy Soma 350 mg

Benish said...

Really nice post. Thank you for sharing amazing information.
Python training in Chennai/Python training in OMR/Python training in Velachery/Python certification training in Chennai/Python training fees in Chennai/Python training with placement in Chennai/Python training in Chennai with Placement/Python course in Chennai/Python Certification course in Chennai/Python online training in Chennai/Python training in Chennai Quora/Best Python Training in Chennai/Best Python training in OMR/Best Python training in Velachery/Best Python course in Chennai