Sha256: 3c11c7a3cb4293e20d38c6e826a69e645ed05780e8ee0591813efa0fdf1f8f96

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require 'timeout'
require 'twitter'

class Timeline < ActiveRecord::Base
  TIMEOUT = 10
  TTL = 30.minutes
  
  validates_presence_of :username
  
  def get_timeline(tweet_amount=1)
    
    # Remove the cached timeline if its expired
    if self.expires_at.nil? || self.expires_at < Time.now.utc
      Rails.cache.delete('Timeline.Tweets') 
    end

    # Make sure to invalidate the cache if the amount chanages
    Rails.cache.fetch('Timeline.Tweets' + tweet_amount.to_s) { get_timeline_from_twitter(tweet_amount) }
    
  end
  
  private
  
  def get_timeline_from_twitter(tweet_amount=1)
    
    begin

      # New expires time for this timeline
      update_attribute(:expires_at, Time.now.utc + TTL)
      
      Timeout.timeout(TIMEOUT) {
        content = Twitter.user_timeline(self.username)[0..(tweet_amount.to_i - 1)]
      }
            
    rescue SocketError, Timeout::Error, Twitter::General => exception
      logger.error("Loading timeline for user #{self.username} failed with the following error #{exception.inspect}")        
    end

  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcms_twitter_module-1.1.3 app/models/timeline.rb