Class: Tweetable::User

Inherits:
Persistable show all
Defined in:
lib/tweetable/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Persistable

#client, #config, find_or_create, #needs_update?

Class Method Details

.create_from_timeline(user) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/tweetable/user.rb', line 21

def self.create_from_timeline(user)
  u = User.find_or_create(:screen_name, user.screen_name.downcase)
  u.update(
    :user_id => user[:id],
    :profile_image_url => user.profile_image_url,
    :followers_count => user.followers_count, 
    :friends_count => user.friends_count)
  u
end

Instance Method Details



81
82
83
# File 'lib/tweetable/user.rb', line 81

def twitter_link
  "http://twitter.com/#{screen_name}"
end

#update_all(force = false) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/tweetable/user.rb', line 31

def update_all(force = false)
  return unless needs_update?(force)
  update_info if self.config[:include_on_update].include?(:info)
  update_friend_ids if self.config[:include_on_update].include?(:friend_ids)
  update_follower_ids if self.config[:include_on_update].include?(:follower_ids)
  update_friend_messages if self.config[:include_on_update].include?(:friend_messages)
  self.update(:updated_at => Time.now.utc.to_s)            
  self.config[:include_on_update].include?(:messages) ? update_messages : []  # return newly found messages
end

#update_follower_idsObject



58
59
60
61
# File 'lib/tweetable/user.rb', line 58

def update_follower_ids
  fids = self.client.follower_ids(:screen_name => self.screen_name, :page => 1) # limit to 5000 friend ids
  fids.each{|fid| self.follower_ids << fid}
end

#update_friend_idsObject



53
54
55
56
# File 'lib/tweetable/user.rb', line 53

def update_friend_ids    
  fids = self.client.friend_ids(:screen_name => self.screen_name, :page => 1) # limit to 5000 friend ids      
  fids.each{|fid| self.friend_ids << fid}
end

#update_friend_messages(options = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/tweetable/user.rb', line 72

def update_friend_messages(options = {})
  most_recent_message = self.friend_messages.first(:order => 'DESC', :by => :message_id) 
  options.merge!(:count => self.config[:max_message_count], :screen_name => self.screen_name)
  options[:since_id] = most_recent_message.message_id if most_recent_message

  timeline = self.client.friends_timeline(options)      
  build_messages(timeline, self.friend_messages, :create_user => true)
end

#update_infoObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tweetable/user.rb', line 41

def update_info
  uid  = self.user_id.blank? ? self.screen_name : self.user_id
  info = self.client.user(uid)

  self.user_id = info[:id]
  self.screen_name = info[:screen_name].downcase
  self.profile_image_url = info[:profile_image_url]
  self.friends_count = info[:friends_count]
  self.followers_count = info[:followers_count]      
  self.save
end

#update_messages(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/tweetable/user.rb', line 63

def update_messages(options = {})      
  most_recent_message = self.messages.first(:order => 'DESC', :by => :message_id) 
  options.merge!(:count => self.config[:max_message_count], :screen_name => self.screen_name)
  options[:since_id] = most_recent_message.message_id if most_recent_message

  timeline = self.client.user_timeline(options)
  build_messages(timeline, self.messages)
end