annotate app/models/tweet.rb @ 82:f91e2d00f03f

Reduce title length limit
author nanaya <me@myconan.net>
date Sun, 26 Jun 2016 18:22:05 +0900
parents 924b3a4ed3ef
children 5bfc986200db
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
78
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
13
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
14 if @client_config_id == start_client_config_id
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
15 raise
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
16 else
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
17 retry
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
18 end
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
19 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
20 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
21
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
22 def user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
23 @user ||=
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
24 if timeline.any?
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
25 timeline.first.user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
26 else
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
27 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
28 @client.user(@twitter_id)
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
29 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
30 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
31 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
32
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
33 def initialize(twitter_id)
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
34 @client = init_client
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
35 @twitter_id = twitter_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
36 end
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
37
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
38 def init_client(config_id = 0)
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
39 @client_config_id = config_id
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
40 config_hash_key = "twitter#{config_id}".to_sym
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
41
77
fc2bc34e6d89 Don't loop if twitter0 is not configured
nanaya <me@myconan.net>
parents: 76
diff changeset
42 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
43
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
44 @client = Twitter::REST::Client.new do |config|
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
45 $cfg[config_hash_key].each do |cfg_key, cfg_value|
21
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
46 config.public_send("#{cfg_key}=", cfg_value)
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
47 end
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
48 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
49 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
50 end