view app/models/tweet.rb @ 73:e9a6019a34ea

Avoid using constant. Use global instead =D
author nanaya <me@myconan.net>
date Fri, 13 May 2016 04:52:20 +0900
parents 37356e98b784
children 0c023d35cd80
line wrap: on
line source

class Tweet
  def timeline(options = {})
    @timeline ||=
      Rails.cache.fetch({ :timeline => @twitter_id, :options => options.dup }, :expires_in => 5.minutes) do
        @client.user_timeline(@twitter_id, options)
      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