Mercurial > rsstweet
comparison app/models/tweet.rb @ 76:0c023d35cd80
Allows usage of multiple twitter keys
Working around api rate limit
author | nanaya <me@myconan.net> |
---|---|
date | Sun, 26 Jun 2016 16:46:02 +0900 |
parents | e9a6019a34ea |
children | fc2bc34e6d89 |
comparison
equal
deleted
inserted
replaced
75:585952f98978 | 76:0c023d35cd80 |
---|---|
1 class Tweet | 1 class Tweet |
2 def timeline(options = {}) | 2 def timeline(options = {}) |
3 @timeline ||= | 3 start_client_config_id = @client_config_id |
4 Rails.cache.fetch({ :timeline => @twitter_id, :options => options.dup }, :expires_in => 5.minutes) do | 4 |
5 @client.user_timeline(@twitter_id, options) | 5 begin |
6 end | 6 @timeline ||= |
7 Rails.cache.fetch({ :timeline => @twitter_id, :options => options.dup }, :expires_in => 5.minutes) do | |
8 @client.user_timeline(@twitter_id, options) | |
9 end | |
10 rescue Twitter::Error::TooManyRequests | |
11 next_client_config_id = @client_config_id + 1 | |
12 init_client next_client_config_id | |
13 retry unless @client_config_id == start_client_config_id | |
14 end | |
7 end | 15 end |
8 | 16 |
9 def user | 17 def user |
10 @user ||= | 18 @user ||= |
11 if timeline.any? | 19 if timeline.any? |
16 end | 24 end |
17 end | 25 end |
18 end | 26 end |
19 | 27 |
20 def initialize(twitter_id) | 28 def initialize(twitter_id) |
29 @client = init_client | |
30 @twitter_id = twitter_id | |
31 end | |
32 | |
33 def init_client(config_id = 0) | |
34 @client_config_id = config_id | |
35 config_hash_key = "twitter#{config_id}".to_sym | |
36 | |
37 return init_client(0) if $cfg[config_hash_key].nil? | |
38 | |
21 @client = Twitter::REST::Client.new do |config| | 39 @client = Twitter::REST::Client.new do |config| |
22 $cfg[:twitter].each do |cfg_key, cfg_value| | 40 $cfg[config_hash_key].each do |cfg_key, cfg_value| |
23 config.public_send("#{cfg_key}=", cfg_value) | 41 config.public_send("#{cfg_key}=", cfg_value) |
24 end | 42 end |
25 end | 43 end |
26 @twitter_id = twitter_id | |
27 end | 44 end |
28 end | 45 end |