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
900
7年目を迎えたRails アプリケーションの傾向と対策/Rails Developers Meetup 2019 Day1
taogawa
8
3.7k
おいしい時間を支えるAPI設計 / Food Service Engineers Meetup #3
taogawa
1
2.4k
Other Decks in Programming
See All in Programming
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
430
offers_20241022_imakiire.pdf
imakurusu
2
360
Dev ContainersとGitHub Codespacesの素敵な関係
ymd65536
1
130
現場で役立つモデリング 超入門
masuda220
PRO
13
2.9k
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
440
CSC509 Lecture 09
javiergs
PRO
0
110
Tuning GraphQL on Rails
pyama86
2
1k
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4.1k
qmuntal/stateless のススメ
sgash708
0
120
Macとオーディオ再生 2024/11/02
yusukeito
0
200
macOS でできる リアルタイム動画像処理
biacco42
7
1.9k
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Designing for humans not robots
tammielis
249
25k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
What's new in Ruby 2.0
geeforr
342
31k
A Philosophy of Restraint
colly
203
16k
Building Adaptive Systems
keathley
38
2.2k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Producing Creativity
orderedlist
PRO
341
39k
Done Done
chrislema
181
16k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
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!