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
意図せぬレスポンスを防ぐAPI設計2つのコツ / Startup Rails #6
Search
taogawa
July 18, 2017
Programming
0
2.5k
意図せぬレスポンスを防ぐAPI設計2つのコツ / Startup Rails #6
2017/7/18「第6回スタートアップRails勉強会」での発表スライドです
taogawa
July 18, 2017
Tweet
Share
More Decks by taogawa
See All by taogawa
「一人でも多く、一円でも多く」 価値を届ける決済の仕組みと工夫 / 2022-11-30_10x_campfire_kanmu
taogawa
0
120
キッチハイク社内勉強会 ドメイン駆動設計のはなし / 2021-09-01
taogawa
0
1.3k
キッチハイク社内勉強会 / 2021-03-03
taogawa
0
930
7年目を迎えたRails アプリケーションの傾向と対策/Rails Developers Meetup 2019 Day1
taogawa
8
3.7k
おいしい時間を支えるAPI設計 / Food Service Engineers Meetup #3
taogawa
1
2.5k
Other Decks in Programming
See All in Programming
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
140
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
480
Go の GC の不得意な部分を克服したい
taiyow
3
800
Recoilを剥がしている話
kirik
5
6.8k
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
100
テストコード書いてみませんか?
onopon
2
140
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
340
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
850
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
140
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
280
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
The Cult of Friendly URLs
andyhume
78
6.1k
Scaling GitHub
holman
458
140k
4 Signs Your Business is Dying
shpigford
181
21k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
RailsConf 2023
tenderlove
29
940
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Code Reviewing Like a Champion
maltzj
520
39k
Transcript
ҙਤͤ͵ϨεϙϯεΛ ͙APIઃܭ2ͭͷίπ 2017/7/18 taogawa
ࣗݾհ • גࣜձࣾΩονϋΠΫ ΤϯδχΞ • খ ߶ (taogawa) • αʔόʔαΠυΛओʹ୲͍ͯ͠·͢ɻ
ྉཧΛͭ͘Δਓͱ৯ΔਓͷަྲྀίϛϡχςΟαΠτ KitchHike https://kitchhike.com/
TechBlogͬͯ·͢ http://tech.kitchhike.com/ ʮDHHྲྀͷϧʔςΟϯάͰಘΒΕΔϝϦοτͱɺऔΓೖΕΔ্ͰͷϙΠϯτʯ ͱ͍͏هࣄΛॻ͖·ͨ͠
API։ൃʹ ͖ͭ·ͱ͏
ʮҙਤͤ͵Ϩεϙϯεʯ Λ͙ϙΠϯτ
render json: @user.as_json # => response # { # "id":
1, # "first_name": "hike", # "last_name": "kitch", # ... # "password_digest": "xxxxxxxxx" # !!!!! # }
ҙਤͤ͵Ϩεϙϯε • ϢʔβʔຊਓͷΈݟΕΔ͖ඇެ։ใΛϨ εϙϯεͰฦ͍ͯ͠Δ • Ծʹඇެ։APIͰ͋ͬͯΫϥΠΞϯτʹσʔ λ͕ΔͨΊɺආ͚ͳ͍͚ͯ͘ͳ͍ • ҰํͰͪΐͬͱͨ͠ϛεʹΑΔόά͕ೖΓࠐ Έ͍͢
ҙਤͤ͵ϨεϙϯεΛ ઃܭϨϕϧͰ͙
1. Ϩεϙϯε߲ ϗϫΠτϦετํࣜ
ϒϥοΫϦετํࣜΛࢭΊΑ͏ • ϒϥοΫϦετํࣜϛεΛ͍͢͠ • ͍ͭͷ·ʹ͔ΧϥϜ͕ՃΘͬͨ • ϒϥοΫϦετͷ͚Ճ͑࿙Ε • ۩ମతʹݴ͑ as_json
ࢭΊͨ΄͏͕͍͍ Ͱ͢
before render json: @user.as_json # => response # { #
"id": 1, # "first_name": "hike", # "last_name": "kitch", # ... # } • as_jsonσϑΥϧτͰશͯͷ߲Λฦ͢ͷͰ ਏ͍ɾɾɾ
ϗϫΠτϦετํࣜʹ͠Α͏ • ϨεϙϯεͰฦ͖߲͢Λ໌จԽ͢Δ • ॻ͔ͳ͍߲Ϩεϙϯεʹؚ·Εͳ͍ • ActiveModelSerializer, Jbuilder • as_json
͕ආ͚ΒΕͳͯ͘ɺͤΊͯonlyࢦఆ Λ͠Α͏
after class UserSerializer < ActiveModel::Serializer attributes :id, :full_name def full_name
"#{object.first_name} #{object.last_name}" end end # => response # { # "id": 1, # "full_name": "hike kitch" # }
2. Ϣʔβʔࣗ/ͦΕҎ֎ͷ ϦιʔεΤϯυϙΠϯτΛ ͚Δ
Α͋͘Δύλʔϯ • ΫΤϦύϥϝʔλͰϨεϙϯεΛग़͚͠Δ • Ϩεϙϯεͷग़͚͠ͷ݅ఆϛε • ެ։Ϧιʔε͚ͩͷͭΓ͕ϢʔβʔͷϓϥΠ ϕʔτϦιʔεฦ͍ͯͨ͠
before # /kitchens/:id?scope=self class KitchensController < ApplicationController def show if
params[:scope] == "self" # ... else # ... end end end
͍ͬͦΤϯυϙΠϯτΛ ͚Α͏ • ΫΤϦύϥϝʔλͰͷ݅ذͰͳ͘ɺί ϯτϩʔϥࣗମΛ͚Δ • ϓϥΠϕʔτϦιʔεselfͷωʔϜεϖʔε ԼʹҰݩԽ
after # /kitchens/:id class KitchensController < ApplicationController def show end
end # /self/kitchens/:id class Self::KitchensController < ApplicationController def show end end
ΤϯυϙΠϯτׂͷϝϦοτ • ݅ذͷϛεͷ৺Λͳͤͨ͘ • ҆৺ײ͕ͱͯେ͖͍ • ͜ͷΤϯυϙΠϯτɺ͜ͷϨεϙϯεɺͷ Γ͚͕͖ͬΓͨ͠ • ։ൃऀͷೝࣝҧ͍Λͳͤͨ͘
ΤϯυϙΠϯτׂͷσϝϦο τ • વAPIίʔϧ૿͑Δ • N+1ʹͳΒͳ͍ͷͰڐ༰͍ͯ͠Δ • ׂͨ͠ίϯτϩʔϥͰͷίʔυͷॏෳ͕ى ͜Γ͍͢ •
ϞσϧʹίʔυΛدͤͯରԠ
·ͱΊ • ҙਤͤ͵ϨεϙϯεΛฦ͞ͳ͍ઃܭ2ͭͷίπ • ϨεϙϯεϗϫΠτϦετํࣜʹ͢Δ • ݅ذΑΓΤϯυϙΠϯτͷׂ
We’re Hiring!