annotate app/models/tweet.rb @ 77:fc2bc34e6d89

Don't loop if twitter0 is not configured
author nanaya <me@myconan.net>
date Sun, 26 Jun 2016 16:49:15 +0900
parents 0c023d35cd80
children 924b3a4ed3ef
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
66
1161ad2a4390 Take care of rt removal from gem api
nanaya <me@myconan.net>
parents: 48
diff changeset
2 def timeline(options = {})
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
3 start_client_config_id = @client_config_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
4
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
5 begin
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
6 @timeline ||=
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
7 Rails.cache.fetch({ :timeline => @twitter_id, :options => options.dup }, :expires_in => 5.minutes) do
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
8 @client.user_timeline(@twitter_id, options)
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
9 end
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
10 rescue Twitter::Error::TooManyRequests
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
11 next_client_config_id = @client_config_id + 1
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
12 init_client next_client_config_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
13 retry unless @client_config_id == start_client_config_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
14 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
15 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
16
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
17 def user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
18 @user ||=
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
19 if timeline.any?
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
20 timeline.first.user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
21 else
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
22 Rails.cache.fetch({ :user => @twitter_id }, :expires_in => 5.minutes) do
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
23 @client.user(@twitter_id)
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
24 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
25 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
26 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
27
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
28 def initialize(twitter_id)
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
29 @client = init_client
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
30 @twitter_id = twitter_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
31 end
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
32
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
33 def init_client(config_id = 0)
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
34 @client_config_id = config_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
35 config_hash_key = "twitter#{config_id}".to_sym
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
36
77
fc2bc34e6d89 Don't loop if twitter0 is not configured
nanaya <me@myconan.net>
parents: 76
diff changeset
37 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
38
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
39 @client = Twitter::REST::Client.new do |config|
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
40 $cfg[config_hash_key].each do |cfg_key, cfg_value|
21
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
41 config.public_send("#{cfg_key}=", cfg_value)
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
42 end
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
43 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
44 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
45 end