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設計 / Food Service Engineers Meetup #3
Search
taogawa
June 14, 2017
Programming
1
2.5k
おいしい時間を支えるAPI設計 / Food Service Engineers Meetup #3
2017/6/14 「Food Service Engineers Meetup #3 〜メキシカンナイト〜」での発表スライドです
taogawa
June 14, 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
950
7年目を迎えたRails アプリケーションの傾向と対策/Rails Developers Meetup 2019 Day1
taogawa
8
3.8k
意図せぬレスポンスを防ぐAPI設計2つのコツ / Startup Rails #6
taogawa
0
2.5k
Other Decks in Programming
See All in Programming
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
410
asdf-ecspresso作って 友達が増えた話 / Fujiwara Tech Conference 2025
koluku
0
1.4k
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
6
1.4k
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
210
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
200
선언형 UI에서의 상태관리
l2hyunwoo
0
270
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
300
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
1
450
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
190
GitHub CopilotでTypeScriptの コード生成するワザップ
starfish719
26
6k
DMMオンラインサロンアプリのSwift化
hayatan
0
190
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Gamification - CAS2011
davidbonilla
80
5.1k
The Invisible Side of Design
smashingmag
299
50k
Thoughts on Productivity
jonyablonski
68
4.4k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
For a Future-Friendly Web
brad_frost
176
9.5k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.6k
Rails Girls Zürich Keynote
gr2m
94
13k
Site-Speed That Sticks
csswizardry
3
270
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
3
180
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Transcript
͓͍͍࣌ؒ͠Λࢧ͑Δ APIઃܭ 2017/6/14 taogawa
ࣗݾհ • גࣜձࣾΩονϋΠΫ ΤϯδχΞ • খ ߶ (taogawa) • αʔόʔαΠυΛओʹ୲͍ͯ͠·͢ɻ
ྉཧΛͭ͘Δਓͱ৯ΔਓͷަྲྀίϛϡχςΟαΠτ KitchHike https://kitchhike.com/
ΦϑΟεKitchHike ͡Ί·ͨ͠ http://www.office-kitchhike.com/
ຊ͓͢Δ͜ͱ • ࣗࣾͷϞόΠϧΞϓϦ༻APIΛ։ൃ͢Δʹ͋ͨ ͍ͬͯΖ͍Ζߟ͑ͨઃܭϙΠϯτʹ͍ͭͯ
ϞόΠϧΞϓϦϦϦʔε༧ఆ • 8݄ʹϦϦʔε༧ఆ • ReactNativeͰ։ൃ
API։ൃͰղܾ͔ͨͬͨ͠՝ • ௨৴ίετͷ • ηΩϡϦςΟͷ
՝ᶃ ௨৴ίετͷ
௨৴ͷίετ͕ʹͳΔͱ͖ • ௨৴ճ͕ଟ͍ • ௨৴ྔ͕ଟ͍
௨৴ճ͕ଟ͍ • ͨΒͱAPIίʔϧ͕ଟ͍ • ୯७ͳϞσϧͷϨεϙϯεͩͱى͜Γ͕ͪ • ϦϨʔγϣϯͷࢀর͕ id ͚ͩ •
ؔ࿈͢ΔϞσϧͷσʔλͷऔಘʹAPIίʔϧ
• Pop−UpΠϕϯτ • 2ͭͷϦϨʔγϣϯ • ྉཧΛ;Δ·͏Ϣʔ βʔ(COOK) • Πϕϯτग़੮ऀ
௨৴ྔ͕ଟ͍ • Ϩεϙϯεʹෆඞཁͳใ͕ଟ͍ • ͪ͜Β୯७ͳϞσϧͷϨεϙϯεͩͱى͜Γ͕ͪ • ը໘Ͱඞཁͳ߲ͦͷ2ɺ3߲ͳͷʹɺे߲ ͕ฦͬͯ͘Δ • ެ։APIͳΒ͍͍͔͠Εͳ͍͕ɺࣗࣾΞϓϦ͚
APIͰ໌Β͔ʹ
ෆඞཁͳ߲͕ଟ͍ render json: @user.as_json # => response # { #
"id": 1, # "first_name": "hike", # "last_name": "kitch", # ...ͨ͘͞Μͷ߲͕ଓ͘ # } • ཉ͍͠ͷidͱ໊લ͚ͩͳͷʹɾɾɾ
՝ᶄ ηΩϡϦςΟͷ
ηΩϡϦςΟ্ͷݒ೦ • ࣗࣾΞϓϦͱ͍͑ɺJSONܗࣜͰΫϥΠΞϯ τʹσʔλ͕Δ • HTMLϏϡʔͷϨϯμϦϯάͱҟͳΓɺ͏͔ͭ ͳϨεϙϯεฦͤͳ͍ • User ϞσϧΛͦͷ··ϨεϙϯεͰฦ͢
-> ύεϫʔυμΠδΣετؚ͕·Ε͍ͯͨ
͜͜·Ͱͷ·ͱΊ • Ϩεϙϯε߲ͷա / ෆ௨৴ίετͷ ͷݪҼʹͳΓ͏Δ • APIͰϦιʔεΛͦͷ··ϨεϙϯεͰฦ͢ ͱҙਤ͠ͳ͍ͷ·ͰΫϥΠΞϯτʹͬͯ ͠·͏
ରࡦฤ
Ͳ͏ͯ͜͠ͷ͕ى͖Δͷ͔ • ӬଓԽͷσʔλߏͱɺAPIͰฦ͍ͨ͠σʔ λߏҰக͠ͳ͍͔Β
௨৴ίετରࡦ: ઃܭͰ௨৴ίετΛݮΒ͢
Ͳ͏ແବͳ௨৴Λආ͚Δ͔ • Ҋ1: ը໘ͱ1ର1ʹͳΔΑ͏ͳઃܭ • ը໘ʹدͤΔ • Ҋ2: Ϧιʔε͝ͱͷΤϯυϙΠϯτ &
ΫΤϦύϥ ϝʔλͰϦϨʔγϣϯΛऔಘͰ͖ΔΑ͏ͳઃܭɻ • ϦιʔεʹدͤΔ
Altech/rails-restful-api https://github.com/Altech/rails-restful-api/ blob/master/README.md Rails 5 Ͱ࡞Δ RESTful API शձͷࢿྉ
݁ہͲ͏͔ͨ͠ • 1.ͱ2.ͷதؒɻ • ը໘߲Λચ͍ग़ͯ͠ɺ࠷େެʹͳΔΑ ͏ͳΤϯυϙΠϯτΛΔ • ։ൃϦιʔεཁ݅Λ͍Ζ͍Ζߟ͑ͯɺฐࣾ ʹ͍ͪΜίεύ͕ྑ͔ͬͨ
֤Ҋͷൺֱ ॊೈ͞ γϯϓϧ͞ Ҋ ˚ ˕ Ҋ ˕ ˚ Ҋ,)
̋ ̋
ηΩϡϦςΟରࡦ: ͏͔ͬΓηΩϡϦςΟϗʔϧ Λແ͘͢ઃܭ
۩ମతͳ࣮ํ • Ϩεϙϯε߲ϗϫΠτϦετํࣜ • Ϣʔβʔࣗ/ͦΕҎ֎ͷϦιʔεΤϯυϙ ΠϯτΛ͚Δ
ηΩϡϦςΟରࡦ ͦͷ1: Ϩεϙϯε߲ ϗϫΠτϦετํࣜ
ѱ͍ྫ • ͍͚ͬͯͳ͍ as_json render json: @user.as_json # => response
# { # "id": 1, # "first_name": "hike", # "last_name": "kitch", # ... # "password_digest": "xxxxxxxxx" # !!!!! # }
as_jsonͷ • σϑΥϧτͰશͯͷ߲͕ग़ͯ͠·͏ɻ • جຊϒϥοΫϦετํࣜͳͷͰɺ আ֎ઃఆͷ ϛεͰϨεϙϯεʹؚ·Εͯ͠·͏
ςϯϓϨʔτΤϯδϯ or SerializerΛ͓͏ • ςϯϓϨʔτΤϯδϯ • Rabl • jbuilder •
Serializer • ActiveModelSerializer -> ฐࣾͰ͜ΕΛ͍· ͨ͠
ActiveModelSerializer class UserSerializer < ActiveModel::Serializer attributes :id, :full_name def full_name
"#{object.first_name} #{object.last_name}" end end
༨ஊɿ ActiveModelSerializerͷ Α͍ͱ͜Ζ/Α͘ͳ͍ͱ͜Ζ
Α͍ͱ͜Ζ • ςϯϓϨʔτΤϯδϯͱҧ͍RubyͷΫϥεͳͷͰ͍ Ζ͍ΖͰ͖Δ • நԽͷςΫχοΫ͕৭ʑ͑Δ(ϝιουԽɺϞ δϡʔϧԽ) • ϩδοΫ͕৭ʑॻ͚Δ •
ςετॻ͖͍͢
Α͘ͳ͍ͱ͜Ζ • جຊ 1Ϟσϧ-1γϦΞϥΠβΛఆ͍ͯ͠Δͬ Ά͍ • ෳϞσϧʹ·͕ͨΔΑ͏ͳγϦΞϥΠβΛ ॻ͜͏ͱ͢Δͱɺ్ʹॻ͖ͮΒ͘ͳΔ
ηΩϡϦςΟରࡦ ͦͷ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ʹ݁͠ͳ͍ͷͰɺࠓճڐ༰͍ͯ͠ Δ • γϏΞͳύϑΥʔϚϯε͕ٻΊΒΕΔ߹ ɺ͜ͷݪଇΛ่͢ͷඞཁ͔
·ͱΊ • ແବͳ௨৴Λආ͚Δʹ • ԣணͤͣʹը໘ଆͷ߲ΛͪΌΜͱߟ͑ͯϨεϙϯε Λઃܭɾ࣮͠Α͏ • ෆ༻ҙͳϨεϙϯεΛฦ͞ͳ͍ͨΊʹ • ϨεϙϯεϗϫΠτϦετํࣜ
• ΤϯυϙΠϯτΛ͚Δͱ͖ͬ͢Γ͢ΔͷͰΦεεϝ
We’re Hiring!