find_friends_worker.rb 638 Bytes
Newer Older
Eugen Rochko's avatar
Eugen Rochko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# frozen_string_literal: true

class FindFriendsWorker
  include Sidekiq::Worker
  include Sidekiq::Status::Worker

  def perform(user_id)
    client         = User.find(user_id).twitter_client
    all_friend_ids = []
    twitter_user   = client.user

    total twitter_user.friends_count

    begin
      client.friend_ids.each do |friend_id|
        all_friend_ids << friend_id
        at all_friend_ids.size
      end
    rescue Twitter::Error::TooManyRequests => error
      sleep error.rate_limit.reset_in + 1
      retry
    end

    Rails.cache.write("#{user_id}/twitter-friends", all_friend_ids, expires_in: 45.minutes)
  end
end