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
930
7年目を迎えたRails アプリケーションの傾向と対策/Rails Developers Meetup 2019 Day1
taogawa
8
3.7k
意図せぬレスポンスを防ぐAPI設計2つのコツ / Startup Rails #6
taogawa
0
2.5k
Other Decks in Programming
See All in Programming
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
760
testcontainers のススメ
sgash708
1
120
たのしいparse.y
ydah
3
120
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
910
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
From Translations to Multi Dimension Entities
alexanderschranz
2
130
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
130
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
770
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
5
820
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Agile that works and the tools we love
rasmusluckow
328
21k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
A designer walks into a library…
pauljervisheath
204
24k
We Have a Design System, Now What?
morganepeng
51
7.3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
BBQ
matthewcrist
85
9.4k
Music & Morning Musume
bryan
46
6.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!