Twitter gem を使用して、アプリから API 呼び出しを行い、データを取得しています。
の配列がuser_idsあり、次のようにして各ユーザーのすべてのツイートを取得したいと考えています。
user_ids.map {|user_id| Client.user_timeline(user_id)}
これらの呼び出しを同時に行う方法はありますか? typhoeusまたは同様の gem をtwitterで使用できる方法はありますか? この操作を高速化する他の方法はありますか?
API 呼び出しをRuby スレッドでラップして、同時に実行します。
tweets, threads = [], []
threads = user_ids.map do |user_id|
Thread.new { tweets << Client.user_timeline(user_id) }
end
threads.each(&:join)
# All the tweets are in the `tweets` array
puts tweets