annotate app/models/tweet.rb @ 125:61414d0a367b

Less breakage
author nanaya <me@nanaya.pro>
date Wed, 23 Aug 2017 16:35:58 +0900
parents 8b2d770160e3
children 3646b3e319c7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
1 class Tweet
97
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
2 def cache_expires_time
107
de342c5df747 Extend cache time to up to 30 minutes
nanaya <me@myconan.net>
parents: 100
diff changeset
3 (15 + rand(15)).minutes
97
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
4 end
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
5
86
5bfc986200db The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents: 78
diff changeset
6 def timeline
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
7 start_client_config_id = @client_config_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
8
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
9 begin
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
10 @timeline ||=
97
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
11 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do
108
8b2d770160e3 Increase count to 100
nanaya <me@myconan.net>
parents: 107
diff changeset
12 @client.user_timeline(@twitter_id, :count => 100, :exclude_replies => false, :include_rts => true, :tweet_mode => :extended)
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
13 end
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
14 rescue Twitter::Error::TooManyRequests
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
15 next_client_config_id = @client_config_id + 1
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
16 init_client next_client_config_id
78
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
17
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
18 if @client_config_id == start_client_config_id
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
19 raise
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
20 else
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
21 retry
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
22 end
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
23 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
24 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
25
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
26 def user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
27 @user ||=
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
28 if timeline.any?
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
29 timeline.first.user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
30 else
97
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
31 Rails.cache.fetch({ :user => @twitter_id }, :expires_in => cache_expires_time) do
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
32 @client.user(@twitter_id)
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
33 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
34 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
35 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
36
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
37 def initialize(twitter_id)
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
38 @client = init_client
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
39 @twitter_id = twitter_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
40 end
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
41
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
42 def init_client(config_id = 0)
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
43 @client_config_id = config_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
44 config_hash_key = "twitter#{config_id}".to_sym
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
45
77
fc2bc34e6d89 Don't loop if twitter0 is not configured
nanaya <me@myconan.net>
parents: 76
diff changeset
46 return init_client(0) if $cfg[config_hash_key].nil? && config_id != 0
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
47
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
48 @client = Twitter::REST::Client.new do |config|
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
49 $cfg[config_hash_key].each do |cfg_key, cfg_value|
21
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
50 config.public_send("#{cfg_key}=", cfg_value)
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
51 end
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
52 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
53 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
54 end