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 のデータを Rails らしく、しなやかに扱う / Handling next-...
Search
osyoyu
October 22, 2022
Programming
0
400
隣の API のデータを Rails らしく、しなやかに扱う / Handling next-door API data in Rails
osyoyu
October 22, 2022
Tweet
Share
More Decks by osyoyu
See All by osyoyu
Profile and benchmark every change - RubyKaigi 2025
osyoyu
0
50
Regional.rb and the Tokyo Metropolis
osyoyu
0
210
都市伝説バスターズ「WebアプリのボトルネックはDBだから言語の性能は関係ない」 - Kaigi on Rails 2024
osyoyu
29
17k
プロファイラ開発者と見る「推測するな、計測せよ」 - YAPC::Hakodate 2024
osyoyu
7
860
ZigでC拡張を作る 2024 Edition
osyoyu
1
110
Rubyのパフォーマンスプロファイリングの改善 / Enhancing performance profiling for Ruby
osyoyu
2
980
RubyKaigi Decks
osyoyu
0
180
The depths of profiling Ruby - RubyKaigi 2024
osyoyu
3
6.8k
今年のRubyKaigiはProfiler Year🤘
osyoyu
0
1.4k
Other Decks in Programming
See All in Programming
AIレビュアーをスケールさせるには / Scaling AI Reviewers
technuma
2
230
Scale out your Claude Code ~自社専用Agentで10xする開発プロセス~
yukukotani
9
2.6k
ワープロって実は計算機で
pepepper
2
1.4k
Laravel Boost 超入門
fire_arlo
1
120
20250808_AIAgent勉強会_ClaudeCodeデータ分析の実運用〜競馬を題材に回収率100%の先を目指すメソッドとは〜
kkakeru
0
200
技術的負債で信頼性が限界だったWordPress運用をShifterで完全復活させた話
rvirus0817
1
2.1k
なぜ今、Terraformの本を書いたのか? - 著者陣に聞く!『Terraformではじめる実践IaC』登壇資料
fufuhu
4
650
Understanding Ruby Grammar Through Conflicts
yui_knk
1
120
UbieのAIパートナーを支えるコンテキストエンジニアリング実践
syucream
2
700
ゲームの物理
fadis
5
1.5k
DockerからECSへ 〜 AWSの海に出る前に知っておきたいこと 〜
ota1022
5
1.8k
Introduction to Git & GitHub
latte72
0
120
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
95
14k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Being A Developer After 40
akosma
90
590k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
Side Projects
sachag
455
43k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
36
2.5k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
[RailsConf 2023] Rails as a piece of cake
palkan
56
5.8k
Docker and Python
trallard
45
3.5k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Transcript
Daisuke Aritomo (@osyoyu) / Kaigi on Rails 2022 ྡͷ API
ͷσʔλΛ Rails Β͘͠ɺ͠ͳ͔ʹѻ͏
@osyoyu Ͱ͢ • ΫοΫύουͰຖ Rails Λॻ͍ͯ·͢ • ී௨ͷ Ruby ͕͖
• ISUCON Ͱ Ruby Λ༏উ͍ͤͨ͞
ྡͷ API ͷσʔλΛ Rails Β͘͠ɺ͠ͳ͔ʹѻ͏
API ΞΫηεͳ͠ͰαʔϏεΛ࡞Εͣ ୭͕ API αʔόʔͱΫϥΠΞϯτͷ྆ํΛ࡞Δ େ API ࣌
API αʔόʔΛ࡞Δͱ͖ ϓϥΫςΟεཱ͕֬͞Ε͍ͯΔ
ҰํɺΫϥΠΞϯτΛ࡞Δͱ͖
ҰํɺΫϥΠΞϯτΛ࡞Δͱ͖
ҰํɺΫϥΠΞϯτΛ࡞Δͱ͖ API ΫϥΠΞϯτͷ࡞Γํ ඞͣ͠ཧ͞Ε͍ͯͳ͍
ྡͷ API ͷσʔλΛ Rails Β͘͠ɺ͠ͳ͔ʹѻ͏ ͍͍ײ͡ͷ API ΫϥΠΞϯτͷ࡞Γ͔ͨ
ʮ͍͍ײ͡ͷ API ΫϥΠΞϯτͷ࡞Γํʯ ΫοΫύουͰ͋Γ͕ͪͳࡐΛྫʹ έʔεελσΟͯ͠ΈΑ͏
ͲͷΑ͏ͳ Ruby ΠϯλʔϑΣʔεʹ͢Δ͔ʁ Ϩγϐίϯςετͷ microservice ͷྫ User (MySQL) Recipe
Contest (MySQL) Recipe (API) Recipe ContestEntry (MySQL) N 1 N 1 N 1 1 1
• ҟͳΔιʔεछผͷ en ti ty ҟͳΔऔಘํࣜΛఏڙ͢Δ • MySQL
༝དྷ AR::Base Λ ܧঝ͢ΔΫϥεͰදݱ • ֎෦༝དྷผͷΫϥεͰදݱ (i.e. AR::Base Λܧঝ͠ͳ͍) (1) ιʔεͷछผʹண͢Δ User (MySQL) Recipe Contest (MySQL) Recipe (API) Recipe ContestEntry (MySQL) N 1 N 1 N 1 1 1
None
has_one ૉʹදݱͰ͖ͦ͏
belongs_to Ͱ N+1 ʹͳΓ͕ͪ has_one ૉʹදݱͰ͖ͦ͏ has_manyͱ belongs_toͷෳ߹
(1) ιʔεͷछผʹண͢Δ ੑ࣭͕ҟͳΔ (DB/API) σʔλ͕֤ʑʹదͨ͠ߏΛͭ ϓϩάϥϜͷڍಈΛ૾͍͢͠ 👍 JOIN ؔ࿈
(associa ti on) Λදݱ͢Δͷʹແཧ͕ੜ͡Δ 🤔
• ಉҰͷυϝΠϯͷ en ti ty ಉҰͷऔಘํ๏Λఏڙ͢Δ • ΞϓϦͷυϝΠϯ্ͳΒ
ͯ͢ Ac ti veRecord-like ͳ औಘํࣜΛఏڙ͢Δ (2) υϝΠϯڥքʹண͢Δ Tweet (API) User (MySQL) Recipe Contest (MySQL) Recipe (API) Recipe ContestEntry (MySQL) 1 N 1 N 1 1
ਓྨͷྺ࢙: ActiveResource, Her, ... • Ac ti veResource • RESTful
API Λ͍͍ײ͡ʹ model ͬΆͯ͘͘͠ΕΔ • Rails 3.2 ·Ͱͷඪ४ػೳͩͬͨ • Her • ͜Εಉ͡Α͏ͳతͷ Gem • ΄͔ʹ Ac ti veRestClient, Spyke, Flexirest ͳͲ͕͋Δ
ਓྨͷྺ࢙: ActiveResource, Her, ... • ͜ΕΒ "Res tf ul API"
Λେલఏʹ͓͍͍ͯΔ • ಛఆͷ URL ߏΛٻΊΒΕΔ • ͬͺΓ JOIN ͦ͜·ͰಘҙͰͳ͍
(2) υϝΠϯڥքʹண͢Δ ؔ࿈͢ΔσʔλʹಉҰͷૢ࡞ମܥͰΞΫηεͰ͖Δ ʹ σʔλͷར༻͕ࣗવͳܗͰهड़Ͱ͖Δ 👍 ຊདྷҟͳΔૢ࡞ΛಉҰͷૢ࡞ʹݟ͔͚͍ͤͯΔͷͰ ࣮ʹແཧ͕ग़͖͕ͯͪ 🤔
(2) υϝΠϯڥքʹண͢Δ ؔ࿈͢ΔσʔλʹಉҰͷૢ࡞ମܥͰΞΫηεͰ͖Δ ʹ σʔλͷར༻͕ࣗવͳܗͰهड़Ͱ͖Δ 👍 ຊདྷҟͳΔૢ࡞ΛಉҰͷૢ࡞ʹݟ͔͚͍ͤͯΔͷͰ ࣮ʹແཧ͕ग़͖͕ͯͪ 🤔
👊👊 ࣮ͰΓӽ͑Εղܾ 👊👊
ͭͬͯ͘ΈΑ͏ʂ
AR ෩ API ΫϥΠΞϯτΛͭͬͯ͘ΈΑ͏ • ֎෦ API ͕ఏڙ͢Δ Recipe ʹ͍ͭͯ
• Recipe. fi nd(1) ͷΑ͏ͳܗͰΫΤϦͰ͖Δ • @recipe. ti tle ͷΑ͏ͳܗͰϑΟʔϧυΛಡΊΔ • Ϧιʔεαʔόʔʹಛఆͷߏ (RESTful) Λཁٻ͠ͳ͍ • JOIN (has_many, belongs_to, ...) ͏·͘͜ͳͤΔ • Α͏ͳʢجຊతͳʣͷΛͭͬͯ͘ΈΑ͏
ͨͱ͑ AR ෩ʹ͢Δ߹Ͱ͋ͬͯɺτϥϯεϙʔτ͖͢ ϦτϥΠॲཧͳͲ͍ͨ͠͠ɺgRPC ҠߦͳͲָʹͳΔ ·ͣૢ࡞తͳΫϥΠΞϯτΛ༻ҙ Level 1
ͦΕΛͬͯ Recipe.find ͰϦΫΤετ Level 1 ҰԠ has_one తͳϝιου ΫΤϦ༻ͷϝιου (
fi nd, where) recipe# ti tle ͳͲͰϑΟʔϧυΞΫηε
Controller Ͱผݸʹॲཧ͢ΕճආՄೳͰ͋Δ͕…… includes(:recipes) ͷΑ͏ʹهड़͍ͨ͠ ͕͢͞ʹ·࣮ͩ༻త͡Όͳ͍͔ User Recipe Contest Recipe
Recipe ContestEntry recipes (API) ΛԿϦΫΤετͯ͠͠·͏😔ɹɹɹɹ
• Recipe. fi nd ͷ෦ͰΩϡʔΠϯά͢Δ • Recipe. fi nd ͔Βͻͱ·ͣ
Promise తͳΦϒδΣΫτ͕ฦΔ • exAspArk/batch-loader Λར༻ • Recipe. fi nd ͷฦΓʹରͯ͠ # ti tle ͳͲΛݺͿͱɺอཹ͞Ε͍ͯͨϦΫΤετ͕·ͱΊͯඈͿ BatchLoader Ͱ eager loading ͢Δ Level 2
has_one :recipe Ͱهड़͍ͨ͠…… has_many :recipes Ͱهड़͍ͨ͠……
API ຊͷ ActiveRecord ʹͪ͠Ό͏ Level 3 • ຊͷ Ac ti
veRecord Ϋϥεʹͯ͠͠·͏ ͜ͱͰ has_many, belongs_to ͳͲ͕͑Δ • ͜ͷٕΛ͑Δ͔ෆม݅࣍ୈ • ͜͜Ͱ "recipe.user_id ͕ෆมͰ͋Δ" • (͔ͭ recipe.user_id ͕ FK Ͱ͋Δ͜ͱ) • ෆม݅ͷ෦͚ͩςʔϒϧʹೖΕΔ
• ݸਓతʹ AR ෩Ͱͳ͘ɺͬͱૢ࡞తͳܗ͕ྑ͍ͱࢥ͏ • APIClient.update(id: 1, {name: "nanika"}) •
τϥϯβΫγϣϯ͕ඞཁͳΒ1ͭͷDBͷ౷߹Λߟ͑Δ͖ • ΫϥΠΞϯτͷߏʹؔͳ͘ɺ͞ΜτϥϯβΫγϣϯࠔ ͱ͜ΖͰߋ৽ɾআʁ Others
མͪึͻΖ͍ɾࡉ͔͍ Others • AR ෩ͷΦϒδΣΫτΛ༻ҙ͢ΔʹͤΑɺescape hatch ༻ҙͨ͠΄͏͕͍͍ • Ac ti
veModel::Model ʹ͍ͭͯ • ͓ΈͰ include ͢ΔͱΑ͍Ͱ͠ΐ͏ • valida ti on callbacks, rou ti ng ͳͲʹؔ࿈͢Δαϙʔτ͕͍͖ͭͯ·͢ • cookpad/expeditor ͰߋͳΔߴԽΛૂ͏
·ͱΊ ಉҰυϝΠϯͷen ti tyΛಉҰͷૢ࡞ମܥͰѻ͑Δͱؾ͍͍࣋ͪ
We are hiring! cookpad.jobs