SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
Bldr
A minimalist JSON templating DSL
github.com/ajsharp/bldr
github.com/ajsharp/bldr
github.com/ajsharp/bldr
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
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
object :post => @post do
attributes :title, :body
end
{
"post":{
"title":"my title",
"body": "..."
}
}
bldr JSON
github.com/ajsharp/bldr
Example: objects
bldr JSON
collection :posts => @posts do |post|
attributes :title, :body
attribute(: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/bldr
Example: collections
bldr JSON
collection :posts => @posts do |post|
attributes :title, :body
attribute(:created_at) { post.created_at.iso8601 }
collection :comments => post.comments do |comment|
attributes(:author_name)
attribute(:body) {
HTML::Whitelister.clean(comment.body)
}
end
end
{"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/bldr
Example: nested collections
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
twitter @ajsharp
web alexjsharp.com
github.com/ajsharp/bldr

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

I18nize Scala programs à la gettext
I18nize Scala programs à la gettextI18nize Scala programs à la gettext
I18nize Scala programs à la gettext
 
Rubyon Rails
Rubyon RailsRubyon Rails
Rubyon Rails
 
The Ruby Racer: under the hood
The Ruby Racer: under the hoodThe Ruby Racer: under the hood
The Ruby Racer: under the hood
 
Consuming REST services with ActiveResource
Consuming REST services with ActiveResourceConsuming REST services with ActiveResource
Consuming REST services with ActiveResource
 
Railsチュートリアルの歩き方 (第3版)
Railsチュートリアルの歩き方 (第3版)Railsチュートリアルの歩き方 (第3版)
Railsチュートリアルの歩き方 (第3版)
 
Christmas present for you
Christmas present for youChristmas present for you
Christmas present for you
 
PHP7
PHP7PHP7
PHP7
 
Hands on Gradle
Hands on GradleHands on Gradle
Hands on Gradle
 
Declarative JavaScript concepts and implemetation
Declarative JavaScript concepts and implemetationDeclarative JavaScript concepts and implemetation
Declarative JavaScript concepts and implemetation
 
GraphQL over REST
GraphQL over RESTGraphQL over REST
GraphQL over REST
 
Webservices: The RESTful Approach
Webservices: The RESTful ApproachWebservices: The RESTful Approach
Webservices: The RESTful Approach
 
Intro to Clojure lightningtalk
Intro to Clojure lightningtalkIntro to Clojure lightningtalk
Intro to Clojure lightningtalk
 
Realm
RealmRealm
Realm
 
Letswift18 키노트
Letswift18 키노트Letswift18 키노트
Letswift18 키노트
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
 
How to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHow to Begin to Develop Ruby Core
How to Begin to Develop Ruby Core
 
Intro to Clojure 4 Developers
Intro to Clojure 4 DevelopersIntro to Clojure 4 Developers
Intro to Clojure 4 Developers
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3
 
Bhavesh ro r
Bhavesh ro rBhavesh ro r
Bhavesh ro r
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 

Andere mochten auch

DSL in test automation
DSL in test automationDSL in test automation
DSL in test automation
test test
 
Лучшие практики Continuous Delivery с Docker / Дмитрий Столяров (Флант)
Лучшие практики Continuous Delivery с Docker / Дмитрий Столяров (Флант)Лучшие практики Continuous Delivery с Docker / Дмитрий Столяров (Флант)
Лучшие практики Continuous Delivery с Docker / Дмитрий Столяров (Флант)
Ontico
 

Andere mochten auch (9)

Validation of Derived Features and Well-Formedness Constraints in DSLs
Validation of Derived Features and Well-Formedness Constraints in DSLsValidation of Derived Features and Well-Formedness Constraints in DSLs
Validation of Derived Features and Well-Formedness Constraints in DSLs
 
Internal DSLs For Automated Functional Testing
Internal DSLs For Automated Functional TestingInternal DSLs For Automated Functional Testing
Internal DSLs For Automated Functional Testing
 
Инструментарий для создания дистрибутивов продуктов | Владимир Селин
Инструментарий для создания дистрибутивов продуктов | Владимир СелинИнструментарий для создания дистрибутивов продуктов | Владимир Селин
Инструментарий для создания дистрибутивов продуктов | Владимир Селин
 
DSL in test automation
DSL in test automationDSL in test automation
DSL in test automation
 
20 examples on Domain-Specific Modeling Languages
20 examples on Domain-Specific Modeling Languages20 examples on Domain-Specific Modeling Languages
20 examples on Domain-Specific Modeling Languages
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
Domain-Specific Languages
Domain-Specific LanguagesDomain-Specific Languages
Domain-Specific Languages
 
Лучшие практики Continuous Delivery с Docker / Дмитрий Столяров (Флант)
Лучшие практики Continuous Delivery с Docker / Дмитрий Столяров (Флант)Лучшие практики Continuous Delivery с Docker / Дмитрий Столяров (Флант)
Лучшие практики Continuous Delivery с Docker / Дмитрий Столяров (Флант)
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
 

Ähnlich wie Bldr: A Minimalist JSON Templating DSL

Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]
Karel Minarik
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
Henry S
 
Rubyonrails 120409061835-phpapp02
Rubyonrails 120409061835-phpapp02Rubyonrails 120409061835-phpapp02
Rubyonrails 120409061835-phpapp02
sagaroceanic11
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
Manoj Kumar
 
Gwt and rpc use 2007 1
Gwt and rpc use 2007 1Gwt and rpc use 2007 1
Gwt and rpc use 2007 1
Sam Muhanguzi
 

Ähnlich wie Bldr: A Minimalist JSON Templating DSL (20)

Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]
 
JSON API Specificiation
JSON API SpecificiationJSON API Specificiation
JSON API Specificiation
 
Build JSON and XML using RABL gem
Build JSON and XML using RABL gemBuild JSON and XML using RABL gem
Build JSON and XML using RABL gem
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
How DSL works on Ruby
How DSL works on RubyHow DSL works on Ruby
How DSL works on Ruby
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
 
Rubyonrails 120409061835-phpapp02
Rubyonrails 120409061835-phpapp02Rubyonrails 120409061835-phpapp02
Rubyonrails 120409061835-phpapp02
 
JSON-LD update DC 2017
JSON-LD update DC 2017JSON-LD update DC 2017
JSON-LD update DC 2017
 
Creating applications with Grails, Angular JS and Spring Security
Creating applications with Grails, Angular JS and Spring SecurityCreating applications with Grails, Angular JS and Spring Security
Creating applications with Grails, Angular JS and Spring Security
 
GraphQL in an Age of REST
GraphQL in an Age of RESTGraphQL in an Age of REST
GraphQL in an Age of REST
 
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
 
What’s New in Rails 5.0?
What’s New in Rails 5.0?What’s New in Rails 5.0?
What’s New in Rails 5.0?
 
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
 
Drupal, JSON:API, and Cheap Canned Beer - Drupalcamp Atlanta 2019
Drupal, JSON:API, and Cheap Canned Beer - Drupalcamp Atlanta 2019Drupal, JSON:API, and Cheap Canned Beer - Drupalcamp Atlanta 2019
Drupal, JSON:API, and Cheap Canned Beer - Drupalcamp Atlanta 2019
 
Apache Calcite (a tutorial given at BOSS '21)
Apache Calcite (a tutorial given at BOSS '21)Apache Calcite (a tutorial given at BOSS '21)
Apache Calcite (a tutorial given at BOSS '21)
 
Change RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDBChange RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDB
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Wider than rails
Wider than railsWider than rails
Wider than rails
 
Gwt and rpc use 2007 1
Gwt and rpc use 2007 1Gwt and rpc use 2007 1
Gwt and rpc use 2007 1
 
Json and Jsonpath in Postgres 12
Json and Jsonpath in Postgres 12Json and Jsonpath in Postgres 12
Json and Jsonpath in Postgres 12
 

Mehr von Alex Sharp (11)

Bldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning TalkBldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning Talk
 
Mysql to mongo
Mysql to mongoMysql to mongo
Mysql to mongo
 
Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010
 
Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
 
Practical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby MidwestPractical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby Midwest
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSFPractical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
 
Intro To MongoDB
Intro To MongoDBIntro To MongoDB
Intro To MongoDB
 
Getting Comfortable with BDD
Getting Comfortable with BDDGetting Comfortable with BDD
Getting Comfortable with BDD
 
Testing Has Many Purposes
Testing Has Many PurposesTesting Has Many Purposes
Testing Has Many Purposes
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Bldr: A Minimalist JSON Templating DSL

  • 1. Bldr A minimalist JSON templating DSL github.com/ajsharp/bldr
  • 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
  • 5. object :post => @post do attributes :title, :body end { "post":{ "title":"my title", "body": "..." } } bldr JSON github.com/ajsharp/bldr Example: objects
  • 6. bldr JSON collection :posts => @posts do |post| attributes :title, :body attribute(: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/bldr Example: collections
  • 7. bldr JSON collection :posts => @posts do |post| attributes :title, :body attribute(:created_at) { post.created_at.iso8601 } collection :comments => post.comments do |comment| attributes(:author_name) attribute(:body) { HTML::Whitelister.clean(comment.body) } end end {"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/bldr Example: nested collections
  • 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