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.
-
#followers_count ⇒ Object
readonly
Returns the value of attribute followers_count.
-
#following ⇒ Object
(also: #following?)
readonly
Returns the value of attribute following.
-
#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.
-
#website ⇒ Object
readonly
Returns the value of attribute website.
Class Method Summary collapse
- .all ⇒ Object
- .cleanup ⇒ Object
- .create ⇒ Object
- .find(id) ⇒ Object
- .find_or_fetch(id) ⇒ Object
- .new(user) ⇒ Object
Instance Method Summary collapse
-
#initialize(user) ⇒ User
constructor
A new instance of User.
- #touch! ⇒ Object
- #update!(user) ⇒ Object
Constructor Details
#initialize(user) ⇒ User
22 23 24 25 26 27 28 29 |
# File 'lib/twterm/user.rb', line 22 def initialize(user) @id = user.id update!(user) @color = COLORS[@id % 6] touch! @@instances[@id] = self end |
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 |
#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 |
#following ⇒ Object (readonly) Also known as: following?
Returns the value of attribute following.
3 4 5 |
# File 'lib/twterm/user.rb', line 3 def following @following 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 |
#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
52 53 54 |
# File 'lib/twterm/user.rb', line 52 def self.all @@instances.values end |
.cleanup ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/twterm/user.rb', line 67 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 = user_ids.zip(users).to_h end |
.create ⇒ Object
9 |
# File 'lib/twterm/user.rb', line 9 alias_method :create, :new |
.find(id) ⇒ Object
56 57 58 |
# File 'lib/twterm/user.rb', line 56 def self.find(id) @@instances[id] end |
.find_or_fetch(id) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/twterm/user.rb', line 60 def self.find_or_fetch(id) instance = find(id) (yield(instance) && return) if instance Client.current.show_user(id) { |user| yield user } end |
.new(user) ⇒ Object
17 18 19 20 |
# File 'lib/twterm/user.rb', line 17 def self.new(user) instance = find(user.id) instance.nil? ? super : instance.update!(user) end |
Instance Method Details
#touch! ⇒ Object
31 32 33 |
# File 'lib/twterm/user.rb', line 31 def touch! @touched_at = Time.now end |
#update!(user) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/twterm/user.rb', line 35 def update!(user) @name = user.name @screen_name = user.screen_name @description = user.description || '' @location = user.location.is_a?(Twitter::NullObject) ? '' : user.location @website = user.website @following = user.following? @protected = user.protected? @statuses_count = user.statuses_count @friends_count = user.friends_count @followers_count = user.followers_count History::ScreenName.instance.add(user.screen_name) self end |