annotate app/models/tweet.rb @ 69:37356e98b784

Ensure options for cache key isn't changed
author nanaya <me@myconan.net>
date Tue, 19 Jan 2016 22:32:08 +0900
parents 1161ad2a4390
children e9a6019a34ea
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 = {})
28
03f904c070f7 Basic caching support.
edogawaconan <me@myconan.net>
parents: 23
diff changeset
3 @timeline ||=
69
37356e98b784 Ensure options for cache key isn't changed
nanaya <me@myconan.net>
parents: 66
diff changeset
4 Rails.cache.fetch({ :timeline => @twitter_id, :options => options.dup }, :expires_in => 5.minutes) do
66
1161ad2a4390 Take care of rt removal from gem api
nanaya <me@myconan.net>
parents: 48
diff changeset
5 @client.user_timeline(@twitter_id, options)
28
03f904c070f7 Basic caching support.
edogawaconan <me@myconan.net>
parents: 23
diff changeset
6 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
7 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
8
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
9 def user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
10 @user ||=
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
11 if timeline.any?
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
12 timeline.first.user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
13 else
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
14 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
15 @client.user(@twitter_id)
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
16 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
17 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
18 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
19
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
20 def initialize(twitter_id)
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
21 @client = Twitter::REST::Client.new do |config|
47
e5082c4c234d Also allow config-file-based configuration.
nanaya <me@myconan.net>
parents: 30
diff changeset
22 CFG[:twitter].each do |cfg_key, cfg_value|
21
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
23 config.public_send("#{cfg_key}=", cfg_value)
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
24 end
6e3ff830e94a Realign.
edogawaconan <me@myconan.net>
parents: 15
diff changeset
25 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
26 @twitter_id = twitter_id
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
27 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
28 end