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



97
98
99
# File 'lib/tweetable/user.rb', line 97

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

#update_all(force = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# 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



61
62
63
64
# File 'lib/tweetable/user.rb', line 61

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



56
57
58
59
# File 'lib/tweetable/user.rb', line 56

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



88
89
90
91
92
93
94
95
# File 'lib/tweetable/user.rb', line 88

def update_friend_messages(options = {})
  most_recent_message = self.friend_messages.sort(:order => 'DESC', :by => :message_id, :limit => 1).first
  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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tweetable/user.rb', line 44

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tweetable/user.rb', line 66

def update_messages(options = {})      
  most_recent_message = self.messages.sort(:order => 'DESC', :by => :message_id).first
  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)

  puts "**** #{options.inspect}"
  puts "********************************************************************************************************"
  puts "**** #{most_recent_message.inspect}"
  puts "********************************************************************************************************"
  puts "**** #{timeline[0].inspect}"
  puts "********************************************************************************************************"
  puts "**** #{timeline[-1].inspect}"
  puts "********************************************************************************************************"
  puts "**** #{timeline.size}"

  # puts "**** #{timeline.inspect}"
  
  build_messages(timeline, self.messages)
end