Bldr - Rubyconf 2011 Lightning Talk

22
BLDR Minimalist JSON templating DSL Alex Sharp @ajsharp Friday, September 30, 2011

description

Lightning talk I gave at Rubyconf 2011 on Bldr (https://github.com/ajsharp/bldr)

Transcript of Bldr - Rubyconf 2011 Lightning Talk

Page 1: Bldr - Rubyconf 2011 Lightning Talk

BLDRMinimalist JSON templating DSL

Alex Sharp@ajsharp

Friday, September 30, 2011

Page 2: Bldr - Rubyconf 2011 Lightning Talk

WHO AM I?

Alex Sharp (@ajsharp)

Ruby Engineer at Zaarly (zaarly.com)

Friday, September 30, 2011

Page 3: Bldr - Rubyconf 2011 Lightning Talk

WHAT IS ZAARLY?

Buyer-centric local commerce platform

Heavily api driven (iOS, Android, Web, HTML5 mobile)

API only speaks json

Friday, September 30, 2011

Page 4: Bldr - Rubyconf 2011 Lightning Talk

WHY BLDR?

Friday, September 30, 2011

Page 5: Bldr - Rubyconf 2011 Lightning Talk

LET ME COUNT THE REASONS

#as_json quickly gets...unwieldy

We need tight control over our json responses

Friday, September 30, 2011

Page 6: Bldr - Rubyconf 2011 Lightning Talk

SECURITY EXPLOIT

We were leaking information in our json documents we weren’t aware of

We didn’t understand #as_json was recursing through relationships and serializing them

Not good...

Friday, September 30, 2011

Page 7: Bldr - Rubyconf 2011 Lightning Talk

TECHCRUNCH STORY

Friday, September 30, 2011

Page 8: Bldr - Rubyconf 2011 Lightning Talk

BLDR

We wanted a simple, declarative DSL for defining JSON responses

Friday, September 30, 2011

Page 9: Bldr - Rubyconf 2011 Lightning Talk

BLDR

Simple, declarative DSL

Works with Sinatra

Rails 3 support nearly complete

Four DSL methods (object, collection, attribute, attributes)

Friday, September 30, 2011

Page 10: Bldr - Rubyconf 2011 Lightning Talk

SINATRA

get '/foo' do bar = 'baz' bldr :foo, :locals => {:bar => bar}end

Friday, September 30, 2011

Page 11: Bldr - Rubyconf 2011 Lightning Talk

SINATRA

# foo.bldrobject do attribute :foo, barend

# output{"foo": "baz"}

Friday, September 30, 2011

Page 12: Bldr - Rubyconf 2011 Lightning Talk

ATTRIBUTE LISTS

object :post => post do attributes :title, :bodyend

{ "post": { "title": "my title", "body": "..." }}

Friday, September 30, 2011

Page 13: Bldr - Rubyconf 2011 Lightning Talk

IMPLIED OBJECTS

object :post do attributes :title, :bodyend

{ "post": { "title": "my title", "body": "..." }}

Friday, September 30, 2011

Page 14: Bldr - Rubyconf 2011 Lightning Talk

DYNAMIC ATTRIBUTES

object :post do attribute :comment_count do |post| post.comments.count endend

{ "post": {"comment_count":1} }

Friday, September 30, 2011

Page 15: Bldr - Rubyconf 2011 Lightning Talk

OBJECT NESTINGobject :post => post do attributes :title, :body

object :author => post.author do attribute :last_name endend

{ "post": { "title": "my title", "body": "...", "author": {"last_name": "Doe"} }}

Friday, September 30, 2011

Page 16: Bldr - Rubyconf 2011 Lightning Talk

ATTRIBUTE ALIASESobject :post => post do attributes :title, :body

object :author => post.author do attribute :surname => :last_name endend

{ "post": { "title": "my title", "body": "...", "author": {"surname": "Doe"} }}

Friday, September 30, 2011

Page 17: Bldr - Rubyconf 2011 Lightning Talk

TOP-LEVEL COLLECTIONS

collection :posts => posts do attributes :title, :body attribute :comment_count { |post| post.comments.count }end

{ "posts": [ { "title": "my title", "comment_count": 2, }]}

Friday, September 30, 2011

Page 18: Bldr - Rubyconf 2011 Lightning Talk

NESTED COLLECTIONScollection :posts => posts do collection :comments => current_object.comments do attributes :body, :author, :email endend

{ "posts": [ { "comments": [ { "body": "...", "author_name": "Comment Troll", "email": "[email protected]" } ]}]}

Friday, September 30, 2011

Page 19: Bldr - Rubyconf 2011 Lightning Talk

OTHER FEATURES

Uses multi_json gem -- pick your encoding library

Bldr.handler

Friday, September 30, 2011

Page 20: Bldr - Rubyconf 2011 Lightning Talk

Bldr.handler BSON::ObjectId do |value| val.to_s # => "4e77a682364141ecf5000002"end

Friday, September 30, 2011

Page 21: Bldr - Rubyconf 2011 Lightning Talk

MORE INFO

Friday, September 30, 2011

Page 22: Bldr - Rubyconf 2011 Lightning Talk

MORE INFO

github.com/ajsharp/bldr

@ajsharp

[email protected]

zaarly.com / alexjsharp.com

Friday, September 30, 2011