Class: Twterm::Status
- Inherits:
-
Object
- Object
- Twterm::Status
- Defined in:
- lib/twterm/status.rb
Constant Summary collapse
- MAX_CACHED_STATUSES_COUNT =
1000
- @@instances =
{}
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#created_at_for_sort ⇒ Object
readonly
Returns the value of attribute created_at_for_sort.
-
#favorite_count ⇒ Object
readonly
Returns the value of attribute favorite_count.
-
#favorited ⇒ Object
(also: #favorited?)
readonly
Returns the value of attribute favorited.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#in_reply_to_status_id ⇒ Object
readonly
Returns the value of attribute in_reply_to_status_id.
-
#media ⇒ Object
readonly
Returns the value of attribute media.
-
#retweet_count ⇒ Object
readonly
Returns the value of attribute retweet_count.
-
#retweeted ⇒ Object
(also: #retweeted?)
readonly
Returns the value of attribute retweeted.
-
#retweeted_by ⇒ Object
readonly
Returns the value of attribute retweeted_by.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#touched_at ⇒ Object
readonly
Returns the value of attribute touched_at.
-
#urls ⇒ Object
readonly
Returns the value of attribute urls.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #date ⇒ Object
- #expand_url! ⇒ Object
- #favorite! ⇒ Object
- #in_reply_to_status(&block) ⇒ Object
-
#initialize(tweet) ⇒ Status
constructor
A new instance of Status.
- #retweet! ⇒ Object
- #split(width) ⇒ Object
- #touch! ⇒ Object
- #unfavorite! ⇒ Object
- #update!(tweet) ⇒ Object
Constructor Details
#initialize(tweet) ⇒ Status
Returns a new instance of Status.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/twterm/status.rb', line 16 def initialize(tweet) unless tweet.retweeted_status.is_a? Twitter::NullObject @retweeted_by = User.new(tweet.user) retweeted_at = Status.parse_time(tweet.created_at) tweet = tweet.retweeted_status end @id = tweet.id @text = CGI.unescapeHTML(tweet.full_text.dup) @created_at = Status.parse_time(tweet.created_at) @created_at_for_sort = retweeted_at || @created_at @retweet_count = tweet.retweet_count @favorite_count = tweet.favorite_count @in_reply_to_status_id = tweet.in_reply_to_status_id @retweeted = tweet.retweeted? @favorited = tweet.favorited? @media = tweet.media @urls = tweet.urls @user = User.new(tweet.user) @splitted_text = {} @touched_at = Time.now tweet..each do |hashtag| History::Hashtag.instance.add(hashtag.text) end @@instances[id] = self end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def created_at @created_at end |
#created_at_for_sort ⇒ Object (readonly)
Returns the value of attribute created_at_for_sort.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def created_at_for_sort @created_at_for_sort end |
#favorite_count ⇒ Object (readonly)
Returns the value of attribute favorite_count.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def favorite_count @favorite_count end |
#favorited ⇒ Object (readonly) Also known as: favorited?
Returns the value of attribute favorited.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def favorited @favorited end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def id @id end |
#in_reply_to_status_id ⇒ Object (readonly)
Returns the value of attribute in_reply_to_status_id.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def in_reply_to_status_id @in_reply_to_status_id end |
#media ⇒ Object (readonly)
Returns the value of attribute media.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def media @media end |
#retweet_count ⇒ Object (readonly)
Returns the value of attribute retweet_count.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def retweet_count @retweet_count end |
#retweeted ⇒ Object (readonly) Also known as: retweeted?
Returns the value of attribute retweeted.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def retweeted @retweeted end |
#retweeted_by ⇒ Object (readonly)
Returns the value of attribute retweeted_by.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def retweeted_by @retweeted_by end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def text @text end |
#touched_at ⇒ Object (readonly)
Returns the value of attribute touched_at.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def touched_at @touched_at end |
#urls ⇒ Object (readonly)
Returns the value of attribute urls.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def urls @urls end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
5 6 7 |
# File 'lib/twterm/status.rb', line 5 def user @user end |
Class Method Details
.cleanup ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/twterm/status.rb', line 115 def cleanup count = MAX_CACHED_STATUSES_COUNT return if @@instances.count < count statuses = @@instances.values.sort_by(&:touched_at).take(count) status_ids = statuses.map(&:id) @@instances = status_ids.zip(statuses).to_h end |
.find(id) ⇒ Object
107 108 109 |
# File 'lib/twterm/status.rb', line 107 def find(id) @@instances[id] end |
.new(tweet) ⇒ Object
11 12 13 14 |
# File 'lib/twterm/status.rb', line 11 def self.new(tweet) instance = find(tweet.id) instance.nil? ? super : instance.update!(tweet) end |
.parse_time(time) ⇒ Object
111 112 113 |
# File 'lib/twterm/status.rb', line 111 def parse_time(time) (time.is_a?(String) ? Time.parse(time) : time.dup).localtime end |
Instance Method Details
#==(other) ⇒ Object
102 103 104 |
# File 'lib/twterm/status.rb', line 102 def ==(other) other.is_a?(self.class) && id == other.id end |
#date ⇒ Object
60 61 62 63 |
# File 'lib/twterm/status.rb', line 60 def date format = Time.now - @created_at < 86_400 ? '%H:%M:%S' : '%Y-%m-%d %H:%M:%S' @created_at.strftime(format) end |
#expand_url! ⇒ Object
65 66 67 68 |
# File 'lib/twterm/status.rb', line 65 def sub = -> (x) { @text.sub!(x.url, x.display_url) } (@media + @urls).each(&sub) end |
#favorite! ⇒ Object
70 71 72 73 |
# File 'lib/twterm/status.rb', line 70 def favorite! @favorite_count += 1 @favorited = true end |
#in_reply_to_status(&block) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/twterm/status.rb', line 89 def in_reply_to_status(&block) block.call(nil) if @in_reply_to_status_id.nil? status = Status.find(@in_reply_to_status_id) block.call(status) unless status.nil? Client.current.show_status(@in_reply_to_status_id, &block) end |
#retweet! ⇒ Object
80 81 82 83 |
# File 'lib/twterm/status.rb', line 80 def retweet! @retweet_count += 1 @retweeted = true end |
#split(width) ⇒ Object
85 86 87 |
# File 'lib/twterm/status.rb', line 85 def split(width) @splitted_text[:width] ||= @text.split_by_width(width) end |
#touch! ⇒ Object
98 99 100 |
# File 'lib/twterm/status.rb', line 98 def touch! @touched_at = Time.now end |
#unfavorite! ⇒ Object
75 76 77 78 |
# File 'lib/twterm/status.rb', line 75 def unfavorite! @favorite_count -= 1 @favorited = false end |
#update!(tweet) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/twterm/status.rb', line 52 def update!(tweet) @retweet_count = tweet.retweet_count @favorite_count = tweet.favorite_count @retweeted = tweet.retweeted? @favorited = tweet.favorited? self end |