Mercurial > rsstweet
diff app/models/tweet.rb @ 135:3fc882b0884d
Refactored client initialization
author | nanaya <me@nanaya.pro> |
---|---|
date | Wed, 13 Dec 2017 02:51:27 +0900 |
parents | 3646b3e319c7 |
children | 59991d10f8a3 |
line wrap: on
line diff
--- a/app/models/tweet.rb Wed Dec 13 02:28:34 2017 +0900 +++ b/app/models/tweet.rb Wed Dec 13 02:51:27 2017 +0900 @@ -1,20 +1,24 @@ class Tweet + def initialize(twitter_id) + @clients = {} + @twitter_id = twitter_id + end + def cache_expires_time (15 + rand(15)).minutes end def timeline - start_client_config_id = @client_config_id + initial_config_id = client_config_id @timeline ||= Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do begin - @client.user_timeline(@twitter_id, :count => 100, :exclude_replies => false, :include_rts => true, :tweet_mode => :extended) + client.user_timeline(@twitter_id, :count => 100, :exclude_replies => false, :include_rts => true, :tweet_mode => :extended) rescue Twitter::Error::TooManyRequests - next_client_config_id = @client_config_id + 1 - init_client next_client_config_id + @client_config_id += 1 - if @client_config_id == start_client_config_id + if initial_config_id == client_config_id raise else retry @@ -34,21 +38,18 @@ end end - def initialize(twitter_id) - @client = init_client - @twitter_id = twitter_id + def client + @clients[client_config_id] ||= + Twitter::REST::Client.new do |config| + $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value| + config.public_send("#{cfg_key}=", cfg_value) + end + end end - def init_client(config_id = 0) - @client_config_id = config_id - config_hash_key = "twitter#{config_id}".to_sym - - return init_client(0) if $cfg[config_hash_key].nil? && config_id != 0 + def client_config_id + @client_config_id ||= 0 - @client = Twitter::REST::Client.new do |config| - $cfg[config_hash_key].each do |cfg_key, cfg_value| - config.public_send("#{cfg_key}=", cfg_value) - end - end + @client_config_id % $cfg[:twitter].size end end