Class: Twterm::User
- Inherits:
-
Object
- Object
- Twterm::User
- Defined in:
- lib/twterm/user.rb
Constant Summary collapse
- MAX_CACHED_TIME =
3600
- COLORS =
[:red, :blue, :green, :cyan, :yellow, :magenta]
- @@instances =
{}
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#favorites_count ⇒ Object
readonly
Returns the value of attribute favorites_count.
-
#followers_count ⇒ Object
readonly
Returns the value of attribute followers_count.
-
#friends_count ⇒ Object
readonly
Returns the value of attribute friends_count.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#protected ⇒ Object
(also: #protected?)
readonly
Returns the value of attribute protected.
-
#screen_name ⇒ Object
readonly
Returns the value of attribute screen_name.
-
#statuses_count ⇒ Object
readonly
Returns the value of attribute statuses_count.
-
#touched_at ⇒ Object
readonly
Returns the value of attribute touched_at.
-
#verified ⇒ Object
(also: #verified?)
readonly
Returns the value of attribute verified.
-
#website ⇒ Object
readonly
Returns the value of attribute website.
Class Method Summary collapse
- .all ⇒ Object
- .cleanup ⇒ Object
- .find(id) ⇒ Object
- .find_or_fetch(id) ⇒ Object
- .ids ⇒ Object
- .new(user) ⇒ Object
Instance Method Summary collapse
- #blocked_by?(user_id) ⇒ Boolean
- #blocking?(user_id) ⇒ Boolean
- #followed_by?(user_id) ⇒ Boolean
- #following?(user_id) ⇒ Boolean
- #following_requested?(user_id) ⇒ Boolean
- #following_requested_by?(user_id) ⇒ Boolean
-
#initialize(user) ⇒ User
constructor
A new instance of User.
- #matches?(query) ⇒ Boolean
- #muted_by?(user_id) ⇒ Boolean
- #muting?(user_id) ⇒ Boolean
- #touch! ⇒ Object
- #update!(user) ⇒ Object
Constructor Details
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def color @color end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def description @description end |
#favorites_count ⇒ Object (readonly)
Returns the value of attribute favorites_count.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def favorites_count @favorites_count end |
#followers_count ⇒ Object (readonly)
Returns the value of attribute followers_count.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def followers_count @followers_count end |
#friends_count ⇒ Object (readonly)
Returns the value of attribute friends_count.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def friends_count @friends_count end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def id @id end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def location @location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def name @name end |
#protected ⇒ Object (readonly) Also known as: protected?
Returns the value of attribute protected.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def protected @protected end |
#screen_name ⇒ Object (readonly)
Returns the value of attribute screen_name.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def screen_name @screen_name end |
#statuses_count ⇒ Object (readonly)
Returns the value of attribute statuses_count.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def statuses_count @statuses_count end |
#touched_at ⇒ Object (readonly)
Returns the value of attribute touched_at.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def touched_at @touched_at end |
#verified ⇒ Object (readonly) Also known as: verified?
Returns the value of attribute verified.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def verified @verified end |
#website ⇒ Object (readonly)
Returns the value of attribute website.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def website @website end |
Class Method Details
.all ⇒ Object
97 98 99 |
# File 'lib/twterm/user.rb', line 97 def self.all @@instances.values end |
.cleanup ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/twterm/user.rb', line 116 def self.cleanup referenced_users = Status.all.map(&:user) referenced_users.each(&:touch!) cond = -> (user) { user.touched_at > Time.now - MAX_CACHED_TIME } users = all.select(&cond) user_ids = users.map(&:id) @@instances = Hash[user_ids.zip(users)] end |
.find(id) ⇒ Object
101 102 103 |
# File 'lib/twterm/user.rb', line 101 def self.find(id) @@instances[id] end |
.find_or_fetch(id) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/twterm/user.rb', line 105 def self.find_or_fetch(id) Promise.new do |resolve, reject| instance = find(id) (resolve.(instance) && next) if instance Client.current.show_user(id).then do |user| resolve.(user) end end end |
.ids ⇒ Object
126 127 128 |
# File 'lib/twterm/user.rb', line 126 def self.ids @@instances.keys end |
.new(user) ⇒ Object
130 131 132 133 |
# File 'lib/twterm/user.rb', line 130 def self.new(user) instance = find(user.id) instance.nil? ? super : instance.update!(user) end |
Instance Method Details
#blocked_by?(user_id) ⇒ Boolean
14 15 16 |
# File 'lib/twterm/user.rb', line 14 def blocked_by?(user_id) Friendship.blocking?(user_id, id) end |
#blocking?(user_id) ⇒ Boolean
18 19 20 |
# File 'lib/twterm/user.rb', line 18 def blocking?(user_id) Friendship.blocking?(id, user_id) end |
#followed_by?(user_id) ⇒ Boolean
22 23 24 |
# File 'lib/twterm/user.rb', line 22 def followed_by?(user_id) Friendship.following?(user_id, id) end |
#following?(user_id) ⇒ Boolean
26 27 28 |
# File 'lib/twterm/user.rb', line 26 def following?(user_id) Friendship.following?(id, user_id) end |
#following_requested?(user_id) ⇒ Boolean
30 31 32 |
# File 'lib/twterm/user.rb', line 30 def following_requested?(user_id) Friendship.following_requested?(id, user_id) end |
#following_requested_by?(user_id) ⇒ Boolean
34 35 36 |
# File 'lib/twterm/user.rb', line 34 def following_requested_by?(user_id) Friendship.following_requested?(user_id, id) end |
#matches?(query) ⇒ Boolean
47 48 49 |
# File 'lib/twterm/user.rb', line 47 def matches?(query) [name, screen_name, description, website].any? { |x| x.to_s.downcase.include? query.downcase } end |
#muted_by?(user_id) ⇒ Boolean
51 52 53 |
# File 'lib/twterm/user.rb', line 51 def muted_by?(user_id) Friendship.muting?(user_id, id) end |
#muting?(user_id) ⇒ Boolean
55 56 57 |
# File 'lib/twterm/user.rb', line 55 def muting?(user_id) Friendship.muting?(id, user_id) end |
#touch! ⇒ Object
59 60 61 |
# File 'lib/twterm/user.rb', line 59 def touch! @touched_at = Time.now end |
#update!(user) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/twterm/user.rb', line 63 def update!(user) return self if recently_updated? @name = user.name @screen_name = user.screen_name @description = user.description || '' @location = user.location.is_a?(Twitter::NullObject) ? '' : user.location @website = user.website @protected = user.protected? @statuses_count = user.statuses_count @favorites_count = user.favorites_count @friends_count = user.friends_count @followers_count = user.followers_count @verified = user.verified? client_id = Client.current.user_id if user.following? Friendship.follow(client_id, user.id) else Friendship.unfollow(client_id, user.id) end if user.follow_request_sent? Friendship.following_requested(client_id, user.id) else Friendship.following_not_requested(client_id, user.id) end @updated_at = Time.now self end |