view app/models/tweet.rb @ 60:122df25a3466

Always enable npm Current one was wrong since rails doesn't require non-default groups.
author nanaya <me@myconan.net>
date Mon, 16 Nov 2015 01:19:26 +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