Build JSON and XML using RABL gem JUNAID ALAM PRESENTED BY
What is RABL? RABL (Ruby API Builder Language) is a Rails and Padrino ruby templating system for generating JSON, XML, MessagePack, PList and BSON.
Why Use RABL?  Create arbitrary nodes named based on combining data in an object  Pass arguments to methods and store the result as a child node  Render partial templates and inherit to reduce code duplication
Why Use RABL?  Rename or alias attributes to change the name from the model  Append attributes from a child into a parent node  Include nodes only if a certain condition has been met
Attributes # GET /articles/:id def show respond_with(@article) end # articles/show.rabl object @article attributes :id, :title, :content # JSON response { "article": { "id": 2, "title": "My article", "content": "Content of my article." } }
Aliased Attributes # GET /articles/:id def show respond_with(@article) end # articles/show.rabl object @article => :post attributes :id => :article_id, :title => :article_title, :content => :article_content # JSON response { "post": { "article_id": 2, "article_title": "My article", "article_content": "Content of my article." } }
Nested Models – Child Nodes # GET /articles/:id def show respond_with(@article) end # articles/show.rabl object @article attributes :id, :title, :content child :comments do attributes :id, :comment_text end
# JSON response { "article": { "id": 2, "title": "My Article", "content": "Content of my article", "comments": [ { "comment": { "id": 1, "comment_text": "This is a comment." } }, . . . { "comment": { "id": 2, "comment_text": "This is another comment." } } ] } }
Custom Attributes # GET /articles/:id def show respond_with(@article) end # articles/show.rabl object @article attributes :id, :title, :content child :comments do attributes :id, :comment_text end node(:total_comments) do |article| article.comments.count end
{ "article": { "id": 2, "title": "Police Brutality", "comments": [ { "comment": { "id": 2, "comment_text": "This is such a nasty thing" } }, { "comment": { "id": 3, "comment_text": "This is such a nasty thing" } } ], "total_comments": 3 } }
Collections # GET /articles def index @articles = Article.all respond_with(@articles) end # articles/index.rabl collection @articles attributes :id, :title, :content child :comments do attributes :id, :comment_text end node(:total_comments) do |article| article.comments.count end
{ “articles”: [ “article”: { “id”: 1, “comments”:[ “comment”: { “id”: 3 } ] }, . . . }
Inheritance # articles/show.rabl object @article attributes :id, :title, :content child :comments do attributes :id, :comment_text end # articles/index.rabl collection @articles attributes :id, :title, :content child :comments do attributes :id, :comment_text end node(:total_comments) do |article| article.comments.count end #comments/base.rabl attributes :id, :comment_text #articles/base.rabl attributes :id, :title, :content
# articles/show.rabl object @article extends 'articles/base' child :comments do extends 'comments/base' end # articles/index.rabl collection @articles extends 'articles/base' child :comments do extends 'comments/base' end node(:total_comments) do |article| article.comments.count end # articles/show.rabl object @article attributes :id, :title, :content child :comments do attributes :id, :comment_text end # articles/index.rabl collection @articles attributes :id, :title, :content child :comments do attributes :id, :comment_text end node(:total_comments) do |article| article.comments.count end
Complex JSON - Tree Structure # GET /articles def index @articles = Article.all respond_with(@articles) end #articles/index.rabl collection @articles => :articles extends 'articles/base' child :comments do extends 'comments/base' end #comments/base.rabl attributes :id, :comment_text node(:article_title, :if => lambda{ |cm| cm.article.present?}) do |cm| cm.article.title end node(:replies) do |comment| comment.replies.map do |reply| partial 'comments/base', object: reply, root: true end end
{ “articles”:[ “article”: { “comments”[ “comment”:{ “replies”: [ “reply”:{ } ] } ] }, . . . ] }
Links  RABL: https://github.com/nesquena/rabl  Similar Gems: - Jbuilder: https://github.com/rails/jbuilder - Active Model Serializers: https://github.com/rails-api/active_model_serializers
Thank you!

Build JSON and XML using RABL gem

  • 1.
    Build JSON andXML using RABL gem JUNAID ALAM PRESENTED BY
  • 2.
    What is RABL? RABL(Ruby API Builder Language) is a Rails and Padrino ruby templating system for generating JSON, XML, MessagePack, PList and BSON.
  • 3.
    Why Use RABL? Create arbitrary nodes named based on combining data in an object  Pass arguments to methods and store the result as a child node  Render partial templates and inherit to reduce code duplication
  • 4.
    Why Use RABL? Rename or alias attributes to change the name from the model  Append attributes from a child into a parent node  Include nodes only if a certain condition has been met
  • 5.
    Attributes # GET /articles/:id defshow respond_with(@article) end # articles/show.rabl object @article attributes :id, :title, :content # JSON response { "article": { "id": 2, "title": "My article", "content": "Content of my article." } }
  • 6.
    Aliased Attributes # GET/articles/:id def show respond_with(@article) end # articles/show.rabl object @article => :post attributes :id => :article_id, :title => :article_title, :content => :article_content # JSON response { "post": { "article_id": 2, "article_title": "My article", "article_content": "Content of my article." } }
  • 7.
    Nested Models –Child Nodes # GET /articles/:id def show respond_with(@article) end # articles/show.rabl object @article attributes :id, :title, :content child :comments do attributes :id, :comment_text end
  • 8.
    # JSON response { "article":{ "id": 2, "title": "My Article", "content": "Content of my article", "comments": [ { "comment": { "id": 1, "comment_text": "This is a comment." } }, . . . { "comment": { "id": 2, "comment_text": "This is another comment." } } ] } }
  • 9.
    Custom Attributes # GET/articles/:id def show respond_with(@article) end # articles/show.rabl object @article attributes :id, :title, :content child :comments do attributes :id, :comment_text end node(:total_comments) do |article| article.comments.count end
  • 10.
    { "article": { "id": 2, "title":"Police Brutality", "comments": [ { "comment": { "id": 2, "comment_text": "This is such a nasty thing" } }, { "comment": { "id": 3, "comment_text": "This is such a nasty thing" } } ], "total_comments": 3 } }
  • 11.
    Collections # GET /articles defindex @articles = Article.all respond_with(@articles) end # articles/index.rabl collection @articles attributes :id, :title, :content child :comments do attributes :id, :comment_text end node(:total_comments) do |article| article.comments.count end
  • 12.
    { “articles”: [ “article”: { “id”:1, “comments”:[ “comment”: { “id”: 3 } ] }, . . . }
  • 13.
    Inheritance # articles/show.rabl object @article attributes:id, :title, :content child :comments do attributes :id, :comment_text end # articles/index.rabl collection @articles attributes :id, :title, :content child :comments do attributes :id, :comment_text end node(:total_comments) do |article| article.comments.count end #comments/base.rabl attributes :id, :comment_text #articles/base.rabl attributes :id, :title, :content
  • 14.
    # articles/show.rabl object @article extends'articles/base' child :comments do extends 'comments/base' end # articles/index.rabl collection @articles extends 'articles/base' child :comments do extends 'comments/base' end node(:total_comments) do |article| article.comments.count end # articles/show.rabl object @article attributes :id, :title, :content child :comments do attributes :id, :comment_text end # articles/index.rabl collection @articles attributes :id, :title, :content child :comments do attributes :id, :comment_text end node(:total_comments) do |article| article.comments.count end
  • 15.
    Complex JSON -Tree Structure # GET /articles def index @articles = Article.all respond_with(@articles) end #articles/index.rabl collection @articles => :articles extends 'articles/base' child :comments do extends 'comments/base' end #comments/base.rabl attributes :id, :comment_text node(:article_title, :if => lambda{ |cm| cm.article.present?}) do |cm| cm.article.title end node(:replies) do |comment| comment.replies.map do |reply| partial 'comments/base', object: reply, root: true end end
  • 16.
  • 17.
    Links  RABL: https://github.com/nesquena/rabl Similar Gems: - Jbuilder: https://github.com/rails/jbuilder - Active Model Serializers: https://github.com/rails-api/active_model_serializers
  • 18.