Mercurial > rsstweet
annotate app/models/tweet.rb @ 104:f2398d2a6859
Prefer .attrs to .to_hash
author | nanaya <me@myconan.net> |
---|---|
date | Mon, 26 Sep 2016 02:40:08 +0900 |
parents | c1d5d8be92c6 |
children | de342c5df747 |
rev | line source |
---|---|
15 | 1 class Tweet |
97 | 2 def cache_expires_time |
3 (10 + rand(15)).minutes | |
4 end | |
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 | 11 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do |
100 | 12 @client.user_timeline(@twitter_id, :count => 40, :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 | 17 |
18 if @client_config_id == start_client_config_id | |
19 raise | |
20 else | |
21 retry | |
22 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
23 end |
15 | 24 end |
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 | 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 | 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 | 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 | 50 config.public_send("#{cfg_key}=", cfg_value) |
51 end | |
52 end | |
15 | 53 end |
54 end |