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
110
キッチハイク社内勉強会 ドメイン駆動設計のはなし / 2021-09-01
taogawa
0
1.2k
キッチハイク社内勉強会 / 2021-03-03
taogawa
0
920
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
RubyLSPのマルチバイト文字対応
notfounds
0
120
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Outline View in SwiftUI
1024jp
1
330
Realtime API 入門
riofujimon
0
150
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Adopting Sorbet at Scale
ufuk
73
9.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
We Have a Design System, Now What?
morganepeng
50
7.2k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
4 Signs Your Business is Dying
shpigford
180
21k
Ruby is Unlike a Banana
tanoku
97
11k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
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!