Tropical Software Observations

01 June 2011

Posted by Anonymous

at 10:19 AM

30 comments

Labels:

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.

Problems

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": "matteo@wilkinsonhuel.name",
#         "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": "rubie@hayes.name",
#         "display_name": "Mrs. Gaylord Hoeger"
#     }
# }, {
#     "user": {
#         "id": 2,
#         "username": "jarrell.robel",
#         "email": "jarod@eichmann.com",
#         "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'
end

## 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": "rubie@hayes.name",
#             "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])
end
end

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"
end

## JSON output:
# [{
#     "event_guest": {
#         "rsvp": "PENDING",
#         "id": 3,
#         "username": "myrna_harvey",
#         "email": "shad.armstrong@littelpouros.name",
#         "display_name": "Savion Balistreri"
#     }
# }, {
#     "event_guest": {
#         "rsvp": "PENDING",
#         "id": 4,
#         "username": "adelle.nader",
#         "email": "brendon.howe@cormiergrady.info",
#         "display_name": "Edgardo Dickens"
#     }
# }]

The complete Rails example code is available at github.com/teohm/tryrabl.

30 comments:

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.

https://globalonlinepills.com/product/soma-350-mg/


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

Unknown said...

Best AWS certification training courses. Build your AWS cloud skills with expert instructor- led classes. Live projects, Hands-on training,24/7 support
https://onlineidealab.com/aws-certification/

Chris Hemsworth said...

I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
magento training course in chennai
magento training institute in chennai
magento 2 training in chennai
magento development training
magento 2 course
magento developer training

Naveen S said...

This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted. Thank you so much for this article.

Learn Best Digital Marketing Course in Chennai

Digital Marketing Course Training with Placement in Chennai

Learn Digital Marketing Course Training in Chennai

Digital Marketing Training with Placement Institute in Chennai

zuan said...

I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
Web Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement | Web Designing and Development Course in Chennai | Web Designer Training Course in Chennai
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai

Unknown said...

If you are interested in then we can provide you thedatabase providers in mumbai and company directory
We also provide b2b company in india and data provider.

We also provide best business directory

mahi said...

Please refer below if you are looking for best project center in coimbatore

Java Training in Coimbatore | Digital Marketing Training in Coimbatore | SEO Training in Coimbatore | Tally Training in Coimbatore | Python Training In Coimbatore | Final Year Java Projects In Coimbatore | FINAL YEAR DOT NET PROJECTS IN COIMBATORE | Final Year Big Data Projects In Coimbatore | Final Year Python Projects In Coimbatore

Thank you for excellent article.Please refer below if you are looking for best project center

mahi said...

Please refer below if you are looking for best project center in coimbatore

Java Training in Coimbatore | Digital Marketing Training in Coimbatore | SEO Training in Coimbatore | Tally Training in Coimbatore | Python Training In Coimbatore | Final Year Java Projects In Coimbatore | FINAL YEAR DOT NET PROJECTS IN COIMBATORE | Final Year Big Data Projects In Coimbatore | Final Year Python Projects In Coimbatore

Thank you for excellent article.Please refer below if you are looking for best project center

Reshma said...


Amazing Post. keep update more information.
Aws Training in Hyderabad
Aws Course in Hyderabad
AWS Training in Chennai
Aws training in bangalore
Aws training in coimbatore
AWS Course in Chennai
AWS Certification in Chennai
Best AWS Training in Chennai
Amazon web services Training in Chennai

vijay said...

Awesome..I read this post so nice and very imformative information...thanks for sharing

aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore