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.4k
おいしい時間を支える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
110
キッチハイク社内勉強会 ドメイン駆動設計のはなし / 2021-09-01
taogawa
0
1.2k
キッチハイク社内勉強会 / 2021-03-03
taogawa
0
860
7年目を迎えたRails アプリケーションの傾向と対策/Rails Developers Meetup 2019 Day1
taogawa
8
3.6k
意図せぬレスポンスを防ぐAPI設計2つのコツ / Startup Rails #6
taogawa
0
2.4k
Other Decks in Programming
See All in Programming
Amebaチョイス立ち上げの裏側 ~依存システムとの闘い~
daichi_igarashi
0
220
Swift Concurrencyとレースコンディション
objectiveaudio
1
390
rbs-inlineを導入してYARDからRBSに移行する
euglena1215
1
230
これからの時代の新標準!SwiftTestingへの移行とトラブルシューティング
uetyo
0
480
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
1
1.1k
Swiftで高速フーリエ変換してオーディオビジュアライザーを作る / iOSDC Japan 2024 Day1 Track D
kyome22
2
490
ECMAScript仕様を読むのに必要な知識 - ダイジェスト版
syumai
4
2.6k
Desafios e Lições Aprendidas na Migração de Monólitos para Microsserviços em Java
jessilyneh
2
130
ESLint Rule により事業, 技術ドメインに沿った制約と誓約を敷衍させるアプローチのすゝめ
shinyaigeek
1
2.8k
2024 컴포즈 정원사
jisungbin
0
150
iOSの隠されたAPIを解明し、開発効率を向上させる方法/iOSDC24
noppefoxwolf
2
120
令和トラベルにおけるLLM活用事例:社内ツール開発から得た学びと実践
ippo012
0
120
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
167
14k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Bash Introduction
62gerente
608
210k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
22
3.9k
Building Your Own Lightsaber
phodgson
101
6k
A Modern Web Designer's Workflow
chriscoyier
690
190k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
32k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
502
140k
Fashionably flexible responsive web design (full day workshop)
malarkey
401
65k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.4k
Bootstrapping a Software Product
garrettdimon
PRO
304
110k
Designing with Data
zakiwarfel
98
5k
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!