3. github.com/ajsharp/bldr
What?
• 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 years
Bldr in a nutshell
4. github.com/ajsharp/bldr
Why?
• 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
8. Rails Sinatra
github.com/ajsharp/bldr
class PostsController < ApplicationController
# GET /posts
def index
@posts = Post.all.limit(10)
render :index
end
end
# app/views/posts/index.json.bldr
collection :posts => @posts do |post|
# ...
end
class Api::Posts < Sinatra::Application
register Sinatra::Bldr
get '/posts' do
@posts = Post.all.limit(10)
bldr :'posts/index'
end
end
# views/posts/index.json.bldr
collection :posts => @posts do |post|
# ...
end
Example: Rails / Sinatra