Bldr: A Minimalist JSON Templating DSL

1,347 views

Published on

Lightning talk from Railsconf 2013.

https://github.com/ajsharp/bldr

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,347
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Bldr: A Minimalist JSON Templating DSL

  1. 1. BldrA minimalist JSON templating DSLgithub.com/ajsharp/bldr
  2. 2. github.com/ajsharp/bldrgithub.com/ajsharp/bldr
  3. 3. github.com/ajsharp/bldrWhat?• Produce JSON from ruby objects• 4 core API methods• Treat json API responses as a view layer concern• Support for rack/sinatra and rails 3.2• In production use at Zaarly for ~2 yearsBldr in a nutshell
  4. 4. github.com/ajsharp/bldrWhy?• Rails is a great framework for building API services• But #as_json is not for serving prod-ready json• APIs are not intuitive for other json templating engines• See: rabl, json_builder, et al
  5. 5. object :post => @post doattributes :title, :bodyend{"post":{"title":"my title","body": "..."}}bldr JSONgithub.com/ajsharp/bldrExample: objects
  6. 6. bldr JSONcollection :posts => @posts do |post|attributes :title, :bodyattribute(:created_at) {post.created_at.iso8601}end{"posts": [{"title": "Railsconf 2013 wrap-up","body": "...","created_at": "2013-05-01T14:18:16-07:00"},{"title": "OMG Railsconf is coming up!","body": "...","created_at": "2013-04-20T08:01:12-07:00"}]}github.com/ajsharp/bldrExample: collections
  7. 7. bldr JSONcollection :posts => @posts do |post|attributes :title, :bodyattribute(:created_at) { post.created_at.iso8601 }collection :comments => post.comments do |comment|attributes(:author_name)attribute(:body) {HTML::Whitelister.clean(comment.body)}endend{"posts": [{"title": "Railsconf 2013 wrap-up","body": "...","created_at": "2013-05-01T14:18:16-07:00","comments":[{"author_name": "Bob Bobberson","body": "ohai!"}]}]}github.com/ajsharp/bldrExample: nested collections
  8. 8. Rails Sinatragithub.com/ajsharp/bldrclass PostsController < ApplicationController# GET /postsdef index@posts = Post.all.limit(10)render :indexendend# app/views/posts/index.json.bldrcollection :posts => @posts do |post|# ...endclass Api::Posts < Sinatra::Applicationregister Sinatra::Bldrget /posts do@posts = Post.all.limit(10)bldr :posts/indexendend# views/posts/index.json.bldrcollection :posts => @posts do |post|# ...endExample: Rails / Sinatra
  9. 9. twitter @ajsharpweb alexjsharp.comgithub.com/ajsharp/bldr

×