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
RxSwiftでTwitterの ライブラリを作ってる話 / RxTwift
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Mihyaeru
March 12, 2016
Technology
770
4
Share
RxSwiftでTwitterの ライブラリを作ってる話 / RxTwift
RxSwiftでTwitterの ライブラリを作ってる話
Mihyaeru
March 12, 2016
More Decks by Mihyaeru
See All by Mihyaeru
会計 freee バックエンドの今後 / freee backend api
mihyaeru21
8
26k
Railsで実装されている機能のマイクロサービスへの切り離し / split to microservices from rails
mihyaeru21
1
860
WebエンジニアのためのMySQLデータ構造基礎(再) / mysql data structure for web developer 2
mihyaeru21
0
320
WebエンジニアのためのMySQLデータ構造基礎 / mysql data structure for web developer
mihyaeru21
1
720
WebエンジニアのためのMySQL入門 / learning mysql for web developer
mihyaeru21
7
2.7k
入門ReactiveX / about ReactiveX
mihyaeru21
3
500
RxSwiftと愉快な仲間たち / RxSwift with Units
mihyaeru21
4
2.8k
CrystalでTwitter botを作ってみた / Create a Twitter bot in Crystal
mihyaeru21
0
1.3k
Gotanda.pm #1
mihyaeru21
1
190
Other Decks in Technology
See All in Technology
インターネットの技術 / Internet technology
ks91
PRO
0
190
AIエージェントの権限管理 2: データ基盤の Fine grained access control 編
ren8k
0
120
ARIA Notifyについて
ryokatsuse
1
120
レビューしきれない?それは「全て人力でのレビュー」だからではないでしょうか
amixedcolor
0
300
Claude Code を安全に使おう勉強会 / Claude Code Security Basics
masahirokawahara
8
26k
Rapid Start: Faster Internet Connections, with Ruby's Help
kazuho
2
170
最新の脅威動向から考える、コンテナサプライチェーンのリスクと対策
kyohmizu
1
680
基盤を育てる 外部SaaS連携の運用
gamonges_dresscode
1
110
Choose your own adventure in agentic design patterns
glaforge
0
130
ハーネスエンジニアリングをやりすぎた話 ~そのハーネスは解体された~
gotalab555
4
1.5k
明日からドヤれる!超マニアックなAWSセキュリティTips10連発 / 10 Ultra-Niche AWS Security Tips
yuj1osm
0
550
AI時代 に増える データ活用先
takahal
0
190
Featured
See All Featured
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
130
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Making the Leap to Tech Lead
cromwellryan
135
9.8k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
180
First, design no harm
axbom
PRO
2
1.2k
My Coaching Mixtape
mlcsv
0
98
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
200
The untapped power of vector embeddings
frankvandijk
2
1.7k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
500
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.2k
Transcript
RxSwiftͰTwitterͷ ϥΠϒϥϦΛ࡞ͬͯΔ (͍͍ͩͨ)৽ଔΤϯδχΞ͚ٕज़ަྲྀձ vol.6 on 2016/03/12 @mihyaeru21
͓લɺ୭Α • ϛώϟΤϧ @mihyaeru21 • Swift͓͡͞Μ • Kotlin͓͡͞Μ • ͱ͖Ͳ͖PerlϚϯ
RxSwift + Twitter
RxTwift
RxTwift • RxSwiftϕʔε • ґଘؚΊͯSwiftͷΈ • ܕܕܕ • https://github.com/mihyaeru21/RxTwift
͖ͯͱ͏ͳઆ໌ • TwitterͷAPIͱରԠͨ͠ΠϯλϑΣʔε • ApiΫϥεɺ͍ͣΕRestApiʹ໊લ͕มΘΓͦ͏ let api = RxTwift.Api(…) api.get.statuses.homeTimeline()
GET /statuses/home_timeline
͖ͯͱ͏ͳઆ໌ • શͯͷAPIϝιουObservableΛฦ͢ api.get.statuses.homeTimeline() Observable<Tweet>
͖ͯͱ͏ͳઆ໌ • ObservableͷதJSONͰͳ͍ΦϒδΣΫτ api.get.statuses.homeTimeline() .subscribeNext { tweet in print("\(tweet.user.name)\n\(tweet.text)") }
͖ͯͱ͏ͳઆ໌ • ObservableͷதJSONͰͳ͍ΦϒδΣΫτ api.get.statuses.homeTimeline() .subscribeNext { tweet in print("\(tweet.user.name)\n\(tweet.text)") }
ίʔυิͰ͖Δͤʂʂʂ
ͪΐͬͱෳࡶͳྫ • ͋ΔϦετΛऔಘͯ͠ • ͦͷϦετͷϢʔβʢσϑΥϧτ20ਓʣ • ͷ͏ͪɺID͕ۮͷਓͷʢҙຯෆ໌ͳૢ࡞͚ͩͲྫ͔ͩΒڐͯ͠ʣ • λΠϜϥΠϯΛऔಘͯ͠ʢAPIΛୟ͖·͘Δʣ •
දࣔ
Θʔɺ͍͢͝
தͷ
தͷ • OAuth • ΦϒδΣΫτϚοϐϯά • Rx • ެ։APIϝιουͷྫ
OAuth • OAuthΛSwiftͰ࣮͍ͯ͠ΔϥΠϒϥϦ͋Δ • https://github.com/OAuthSwift/OAuthSwift • ͦΜͳʹଟػೳඞཁͳ͔ͬͨ • ετϦʔϛϯάʹରԠͰ͖ͳͦ͏ •
ͱ͍͏Θ͚Ͱࣗ࡞ • HMAC-SHA1͚ͩCryptoSwiftʹґଘ • https://github.com/krzyzanowskim/CryptoSwift • ඞཁͳػೳΑΓଟػೳ͗͢Δ͔Βґଘ͔Β֎͍ͨ͠
OAuth: ೝূͷϔομΛ࡞Δ෦
OAuth • Ϧετॲཧͱ͔จࣈྻॲཧ͕݁ʹॻ͖͍͢ • ʢൺֱରɿObjective-Cʣ • ྫ֎ॲཧnilΛฦ͚ͩ͢ͷखൈ͖ • खൈ͖͚ͩͲ͜Ε͘Β͍ͷॲཧʹे
ΦϒδΣΫτϚοϐϯά • Argo: https://github.com/thoughtbot/Argo • Swift͚ϥΠϒϥϦͰ࠷γϯϓϧʢͳίʔυʹͳΔ
ΦϒδΣΫτϚοϐϯά • README͔ΒҾ༻ struct User { let id: Int let
name: String let email: String? let role: Role let companyName: String let friends: [User] }
ΦϒδΣΫτϚοϐϯά • README͔ΒҾ༻ extension User: Decodable { static func decode(j:
JSON) -> Decoded<User> { return curry(User.init) <^> j <| "id" <*> j <| "name" <*> j <|? "email" <*> j <| "role" <*> j <| ["company", "name"] <*> j <|| "friends" } }
ΦϒδΣΫτϚοϐϯά • มͳه߸ͱ͔ʹ͍ͭͯྑ͍ղઆهࣄ͕͋Δ • http://qiita.com/koher/items/300d89136b515291dac4
ΦϒδΣΫτϚοϐϯά
ΦϒδΣΫτϚοϐϯά ͕ࣜෳࡶ͗ͯ͢ίϯύΠϧͰ͖Ͷʔ͔Β ׂͯͪ͠ΐ
ΦϒδΣΫτϚοϐϯά ࠅ͍͚Ͳಈ͘…
Rx • RxSwift • https://github.com/ReactiveX/RxSwift • ࠷ۙྲྀߦΓʁͷFRP • http://ninjinkun.hatenablog.com/entry/introrxja
Rx • Observable • 1ͭͷPromiseʹෳͷΛྲྀͤͯߴػೳԽͨ͠ײ͡ • ObservableΛ࡞ͬͨஈ֊ͰԿ͠ͳ͍ • ୭͔ʹsubscribe͞ΕΔ͜ͱʹΑͬͯىಈ
Rx: RxTwiftͰ • APIڞ௨ͷϦΫΤετൃߦ෦ • Observable<NSData>
Rx: RxTwiftͰ • JSONจࣈྻΛදݱ͢ΔNSDataΛσίʔυ • ArgoʹΑͬͯΦϒδΣΫτʹม • Observable<Tweet> • Observable<User>
• etc…
ެ։APIϝιουͷ࣮ྫ
ެ։APIϝιουͷ࣮ྫ
client.get(), client.post() • RxTwiftʹ͓͚ΔObservableͷݯྲྀ • Observable.create()ʹΑͬͯੜ • தෳࡶ͔ͩΒׂѪ
ެ։APIϝιουͷ࣮ྫ
Dictionary.createWithNotNil • nilͳཁૉΛল͍ͨDictionaryΛ࡞Δ
ެ։APIϝιουͷ࣮ྫ
Observable<Element>.decode()
ฦΓͷܕͰΦʔόʔϩʔυ ҧ͍͜Ε͚ͩ
ϚοϐϯάରͷܕฦΓ͔Βਪ
ϚοϐϯάରͷܕฦΓ͔Βਪ ͜ΕʹΑͬͯͲͷΦϒδΣΫτ ʹϚοϐϯά͢Δ͔ܾ·Δ
ϚοϐϯάରͷܕฦΓ͔Βਪ ͜ΕʹΑͬͯͲͷΦϒδΣΫτ ʹϚοϐϯά͢Δ͔ܾ·Δ
Observable<Element>.flatMapSequence() • Observable<[T]> Λ Observable<T> ʹม • ετϦʔϜͱͯ͠ѻ͍ͯ͘͢͠Δ • ʢ͜ͷมແ͔͘͢…ʣ
decode()ͷฦΓͷਪ • flatMapSequence() • Observable<[Tweet]> -> Observable<Tweet> • decode() •
Observable<NSData> -> Observable<[Tweet]>
ݱঢ়Ͱ͖͍ͯΔൣғ • OAuthೝূʹΑΔ௨৴ • GET/POST status/* ͷAPI • GET lists/*
ͷAPI • ࣮ࡁΈAPIͷϨεϙϯεʹରԠ͢ΔΦϒδΣΫτ • TwitterଆΤϥʔʹରԠ͢ΔΦϒδΣΫτ
ࠓޙ࣮͢Δൣғ • ೝূͷτʔΫϯऔಘॲཧ • ະ࣮ͷREST API • ετϦʔϛϯάAPI • ύϥϝʔλʹԠͯ͡ϨεϙϯεJSONͷߏ͕
มΘΔ͜ͱͷରԠ • ະ࣮ͷΦϒδΣΫτ