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
イベント駆動で成長して委員会
happymana
1
340
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.3k
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
240
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
900
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
C++でシェーダを書く
fadis
6
4.1k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
CSC509 Lecture 09
javiergs
PRO
0
140
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Functional Event Sourcing using Sekiban
tomohisa
0
100
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
427
64k
Designing Experiences People Love
moore
138
23k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
It's Worth the Effort
3n
183
27k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
110
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
The Cost Of JavaScript in 2023
addyosmani
45
6.8k
A Philosophy of Restraint
colly
203
16k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Code Review Best Practice
trishagee
64
17k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Happy Clients
brianwarren
98
6.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!