Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Building API For The REST of Us
Search
Terry
September 26, 2016
Programming
8
710
Building API For The REST of Us
Slides in RubyConf China 2016 Chengdu
Terry
September 26, 2016
Tweet
Share
More Decks by Terry
See All by Terry
RubyConf China Welcome Slides
poshboytl
1
78
Rails Girls Chengdu 2014
poshboytl
2
320
Refactoring Re-education
poshboytl
5
960
How we test our projects
poshboytl
29
1.9k
Other Decks in Programming
See All in Programming
iOSでSVG画像を扱う
kishikawakatsumi
0
150
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
560
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
880
AIと人間の共創開発!OSSで試行錯誤した開発スタイル
mae616
2
780
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
1.1k
contribution to astral-sh/uv
shunsock
0
450
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
350
AI駆動で0→1をやって見えた光と伸びしろ
passion0102
1
770
CSC305 Lecture 10
javiergs
PRO
0
220
Introduce Hono CLI
yusukebe
6
3k
What's new in Spring Modulith?
olivergierke
1
160
SwiftDataを使って10万件のデータを読み書きする
akidon0000
0
240
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
55
11k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
The World Runs on Bad Software
bkeepers
PRO
72
11k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
Rails Girls Zürich Keynote
gr2m
95
14k
Statistics for Hackers
jakevdp
799
220k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
How GitHub (no longer) Works
holman
315
140k
Build your cross-platform service in a week with App Engine
jlugia
232
18k
The Invisible Side of Design
smashingmag
302
51k
Transcript
Building API For The REST of Us — Terry Tai
About Me
Host of Teahour.FM
Creator of Railscasts-China
I’m working for My Color Way 619'4 䨅⃚
Co-founder of fengche.co
Co-team of Peatio Project
Let’s Building API
Conditions • Based on Ruby on Rails • Not a
pure API service, web is still the first-class citizen • Client builder is resource owner or ourselves
1. Stack Choice
VS
Performance
Flexibility
Level of abstraction
Community
I like Grape a little bit more!
DSL Higer Abstraction
Isolation
Michael Bleigh Grape is created by my coworker in Intridea.
He is also the creator of oauth2, omniauth, hashie, multi_json …
2. Routes & Version
In Rails
In Grape
Rails with version in header
Rails with version in header
Rails with version in header
Grape with version in header
3. Request & Params
Params Encoding application/x-www-form-urlencoded multipart/form-data application/json
Sending Params
Rails server side
Grape server side
Grape params real world example
4.Authentication
Basic Auth
None
With HTTPS?
None
Token Based Auth
Token Based Auth • Simple • A little bit more
secure than Basic Auth • Muti-tokens for authorisation
Digest Based Auth
What we use?
Open Source Exchange
Ancun
Zhiren
Signature based Authentication?
Amazon use the similar strategy
None
One request includes: • access_key • tonce • payload •
signature
Signature
payload
None
Everything Work Well until..
payload
WTF is that?
Why not use JSON?
One request includes: • access_key • tonce • payload •
signature
Strategy • Put business related data in payload as JSON
• Put authentication related data out of payload
Simpler than Amazon
5.Resource Presenter
If you use Grape
If you use Rails
The most quick way
Extract a Presenter layer
Extract a Presenter layer
Treat it as View!
Jbuilder
–DHH “Every time you use a loop in your view,
but you don’t use partial, you might be wrong.”
Jbuilder
RABL
None
It’s not View
Active Model Serializers
View A new layer VS
Two Stories ̽Terry ݏݏᦖඳԪ̾
Story 1
After user signup send him/her a welcome email
Where to put this line?
User model callback
after_create ?
after_commit on create
Send in Callback
Send in Controller
Which one is the BEST way?
I don’t know
The second solution is from Rails Guides
Story 2
Jan Xie Ex-Intridea programmer Ex-Peatio co-member Founder of Cryptape ruby-pinyin
author ruby-ethereum author My darling….
His last project in Ruby?
Python, Go
Sinatra Node
When A record created, B,C,D must be created at the
same time
A B C D
None
None
Communicating emotions
It’s become better but not BEST!
What’s the BEST way?
Jan’s solution — Service
My solution — PubSub
My solution — PubSub
My solution — PubSub
My solution — PubSub You can use Wisper gem to
do similar thing
Two hours PK…
We decide change Nothing
Maybe, there is no BEST way We need know why
people think different We need know compromise
Welcome to Chengdu ́౮᮷҅ Ӟଷԧ੪ӧమᐶጱउ૱̶͂
Spicy Food
Girls
Spicy Food
Girls
RubyConf China in Chengdu
We are hiring!
None
BTW҅I’m ́ፗካ͂
Enjoy your “one night in Chengdu”