diff app/models/tweet.rb @ 48:8983c426e256

Prevent exploding on empty timeline.
author nanaya <me@myconan.net>
date Thu, 23 Apr 2015 16:14:35 +0900
parents e5082c4c234d
children 1161ad2a4390
line wrap: on
line diff
--- a/app/models/tweet.rb	Thu Apr 23 16:05:08 2015 +0900
+++ b/app/models/tweet.rb	Thu Apr 23 16:14:35 2015 +0900
@@ -6,6 +6,17 @@
       end
   end
 
+  def user
+    @user ||=
+      if timeline.any?
+        timeline.first.user
+      else
+        Rails.cache.fetch({ :user => @twitter_id }, :expires_in => 5.minutes) do
+          @client.user(@twitter_id)
+        end
+      end
+  end
+
   def initialize(twitter_id)
     @client = Twitter::REST::Client.new do |config|
       CFG[:twitter].each do |cfg_key, cfg_value|