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
440
0
Share
隣の API のデータを Rails らしく、しなやかに扱う / Handling next-door API data in Rails
osyoyu
October 22, 2022
More Decks by osyoyu
See All by osyoyu
もうちょっといいRubyプロファイラを作りたい (2025)
osyoyu
1
780
Profile and benchmark every change - RubyKaigi 2025
osyoyu
0
140
Regional.rb and the Tokyo Metropolis
osyoyu
0
270
都市伝説バスターズ「WebアプリのボトルネックはDBだから言語の性能は関係ない」 - Kaigi on Rails 2024
osyoyu
29
19k
プロファイラ開発者と見る「推測するな、計測せよ」 - YAPC::Hakodate 2024
osyoyu
7
970
ZigでC拡張を作る 2024 Edition
osyoyu
1
130
Rubyのパフォーマンスプロファイリングの改善 / Enhancing performance profiling for Ruby
osyoyu
2
1.1k
RubyKaigi Decks
osyoyu
0
220
The depths of profiling Ruby - RubyKaigi 2024
osyoyu
3
7.9k
Other Decks in Programming
See All in Programming
AIエージェントで業務改善してみた
taku271
0
520
SREに優しいTerraform構成 modulesとstateの組み方
hiyanger
1
110
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
920
PDI: Como Alavancar Sua Carreira e Seu Negócio
marcelgsantos
0
120
Mastering Event Sourcing: Your Parents Holidayed in Yugoslavia
super_marek
0
150
実用!Hono RPC2026
yodaka
2
180
10 Tips of AWS ~Gen AI on AWS~
licux
5
380
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
3
290
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
4.9k
AI-DLC Deep Dive
yuukiyo
8
3.6k
Server-Side Kotlin LT大会 vol.18 [Kotlin-lspの最新情報と Neovimのlsp設定例]
yasunori0418
1
140
VueエンジニアがReactを触って感じた_設計の違い
koukimiura
0
170
Featured
See All Featured
Facilitating Awesome Meetings
lara
57
6.8k
The Cult of Friendly URLs
andyhume
79
6.8k
Building the Perfect Custom Keyboard
takai
2
730
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
680
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
310
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
230
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
420
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
53k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.4k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.6k
Making Projects Easy
brettharned
120
6.6k
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