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
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
350
Spatial Rendering for Apple Vision Pro
warrenm
0
110
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
470
MCP with Cloudflare Workers
yusukebe
2
220
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
380
nekko cloudにおけるProxmox VE利用事例
irumaru
3
430
命名をリントする
chiroruxx
1
410
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
770
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
9k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
We Have a Design System, Now What?
morganepeng
51
7.3k
For a Future-Friendly Web
brad_frost
175
9.4k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
The Cost Of JavaScript in 2023
addyosmani
45
7k
YesSQL, Process and Tooling at Scale
rocio
169
14k
A better future with KSS
kneath
238
17k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
A Tale of Four Properties
chriscoyier
157
23k
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!