view app/models/tweet.rb @ 54:e01fb1aa55a3

Update gems.
author nanaya <me@myconan.net>
date Sat, 27 Jun 2015 14:47:28 +0900
parents 8983c426e256
children 1161ad2a4390
line wrap: on
line source

class Tweet
  def timeline
    @timeline ||=
      Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => 5.minutes) do
        @client.user_timeline(@twitter_id)
      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|
        config.public_send("#{cfg_key}=", cfg_value)
      end
    end
    @twitter_id = twitter_id
  end
end