More Related Content
Similar to rablについてざっくりだけ調べてみた #shinjukurb 新宿rb 41th
Similar to rablについてざっくりだけ調べてみた #shinjukurb 新宿rb 41th (20)
More from Koichiro Sumi (20)
rablについてざっくりだけ調べてみた #shinjukurb 新宿rb 41th
- 2. © 2015 Actcat, Inc.
@sumyapp
Koichiro Sumi
コードレビューのためのCIサービス、
SideCI を運営
最近はマーケの人と化しています
コードは他のメンバーが書いています
2
- 3. © 2015 Actcat, Inc.
SideCIとは
GitHub連携の自動コードレビューSaaS
RuboCopなどLintツールを約20ほど提供
独自解析エンジンもこそっと提供開始中
3
https://github.com/integrations
- 4. © 2015 Actcat, Inc.
アジェンダ
rabl って何?
使ったことある、使っている人いる?
僕はこういうときにrablを使いたい
4
- 6. © 2015 Actcat, Inc.
What is rabl?
RABL (Ruby API Builder Language) is a Rails
and Padrino ruby templating system for
generating JSON, XML, MessagePack, PList
and BSON.When using theActiveRecord
'to_json' method, I find myself wanting a
more expressive and powerful solution for
generatingAPIs.This is especially true when
the JSON representation is complex or
doesn't match the exact schema defined
within the database.
6
オブジェクトをレスポンスする、
API専用のテンプレートエンジン
- 7. © 2015 Actcat, Inc.
お題
Q.「rablを使ったことが
ある人はいますか?」
A.
A. 使っている
B. 使ったことがある
C. 名前を聞いたことがある
D. 初めて聞いた
7
- 9. © 2015 Actcat, Inc.
詳細はREADMEで
普通のviewで出来ることはたいてい出来る
パーシャル
条件分岐によるJSONへのkeyの有無
キャッシュもRails機構で
action_controller.perform_caching
https://github.com/nesquena/rabl/blob/maste
r/README.md
9
- 10. © 2015 Actcat, Inc.
Controller直レンダリングからrablへ
Pure Rails controller
Controllerクラスで
JSONを組み立て、
render :json でレンダ
リング
APIの返り値は
controllerが管理
View dirを見ても空
rabl
Controllerではインス
タンスの作成のみ、
rablでレンダリング
APIの返り値はrabl
ファイルで管理
View dirにAPIの値も普
通のviewもある
10
- 11. © 2015 Actcat, Inc.
僕はこういうときにrablを使いたい
オブジェクトを複数組み合わせた
オブジェクトをAPIの返り値とするとき
1つの to_json だけでは解決しないとき
APIをViewとして管理したいとき
`render :json` と書くより、users.html.slim
users.json.rablをviewのdirに置きたい
to_jsonより非常に柔軟でシンプルなので、と
りあえず使っておきたい
11