Class: Twterm::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/twterm/status.rb

Constant Summary collapse

MAX_CACHED_TIME =
3600
@@instances =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tweet) ⇒ Status

Returns a new instance of Status.



51
52
53
54
55
56
57
58
59
60
61
62
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
# File 'lib/twterm/status.rb', line 51

def initialize(tweet)
  unless tweet.retweeted_status.is_a? Twitter::NullObject
    @retweeted_by_user_id = tweet.user.id
    User.new(tweet.user)
    retweeted_at = tweet.created_at.dup.localtime
    tweet = tweet.retweeted_status
  end

  @id = tweet.id
  @text = CGI.unescapeHTML(tweet.full_text.dup)
  @created_at = tweet.created_at.dup.localtime
  @appeared_at = 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_id = tweet.user.id
  User.new(tweet.user)

  @splitted_text = {}

  expand_url!

  @touched_at = Time.now

  tweet.hashtags.each do |hashtag|
    History::Hashtag.instance.add(hashtag.text)
  end

  @@instances[id] = self
end

Instance Attribute Details

#appeared_atObject (readonly)

Returns the value of attribute appeared_at.



5
6
7
# File 'lib/twterm/status.rb', line 5

def appeared_at
  @appeared_at
end

#created_atObject (readonly)

Returns the value of attribute created_at.



5
6
7
# File 'lib/twterm/status.rb', line 5

def created_at
  @created_at
end

#favorite_countObject (readonly)

Returns the value of attribute favorite_count.



5
6
7
# File 'lib/twterm/status.rb', line 5

def favorite_count
  @favorite_count
end

#favoritedObject (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

#idObject (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_idObject (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

#mediaObject (readonly)

Returns the value of attribute media.



5
6
7
# File 'lib/twterm/status.rb', line 5

def media
  @media
end

#retweet_countObject (readonly)

Returns the value of attribute retweet_count.



5
6
7
# File 'lib/twterm/status.rb', line 5

def retweet_count
  @retweet_count
end

#retweetedObject (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_user_idObject (readonly)

Returns the value of attribute retweeted_by_user_id.



5
6
7
# File 'lib/twterm/status.rb', line 5

def retweeted_by_user_id
  @retweeted_by_user_id
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/twterm/status.rb', line 5

def text
  @text
end

#touched_atObject (readonly)

Returns the value of attribute touched_at.



5
6
7
# File 'lib/twterm/status.rb', line 5

def touched_at
  @touched_at
end

#urlsObject (readonly)

Returns the value of attribute urls.



5
6
7
# File 'lib/twterm/status.rb', line 5

def urls
  @urls
end

#user_idObject (readonly)

Returns the value of attribute user_id.



5
6
7
# File 'lib/twterm/status.rb', line 5

def user_id
  @user_id
end

Class Method Details

.allObject



132
133
134
# File 'lib/twterm/status.rb', line 132

def self.all
  @@instances.values
end

.cleanupObject



136
137
138
139
140
141
142
143
144
# File 'lib/twterm/status.rb', line 136

def self.cleanup
  TabManager.instance.each_tab do |tab|
    tab.touch_statuses if tab.is_a?(Tab::Statuses::Base)
  end
  cond = -> (status) { status.touched_at > Time.now - MAX_CACHED_TIME }
  statuses = all.select(&cond)
  status_ids = statuses.map(&:id)
  @@instances = Hash[status_ids.zip(statuses)]
end

.delete(id) ⇒ Object



146
147
148
# File 'lib/twterm/status.rb', line 146

def self.delete(id)
  @@instances.delete(id)
end

.find(id) ⇒ Object



150
151
152
# File 'lib/twterm/status.rb', line 150

def self.find(id)
  @@instances[id]
end

.find_or_fetch(id) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/twterm/status.rb', line 154

def self.find_or_fetch(id)
  Promise.new do |resolve, reject|
    instance = find(id)
    (resolve.(instance) && next) if instance

    Client.current.show_status(id) { |status| resolve.(status) }
  end
end

.new(tweet) ⇒ Object



163
164
165
166
# File 'lib/twterm/status.rb', line 163

def self.new(tweet)
  instance = find(tweet.id)
  instance.nil? ? super : instance.update!(tweet)
end

Instance Method Details

#==(other) ⇒ Object



13
14
15
# File 'lib/twterm/status.rb', line 13

def ==(other)
  other.is_a?(self.class) && id == other.id
end

#dateObject



17
18
19
20
# File 'lib/twterm/status.rb', line 17

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



22
23
24
25
# File 'lib/twterm/status.rb', line 22

def expand_url!
  sub = -> (x) { @text.sub!(x.url, x.display_url) }
  (@media + @urls).each(&sub)
end

#favorite!Object



27
28
29
30
# File 'lib/twterm/status.rb', line 27

def favorite!
  @favorite_count += 1
  @favorited = true
end

#in_reply_to_status(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/twterm/status.rb', line 37

def in_reply_to_status(&block)
  Promise.new do |resolve, reject|
    (resolve.(nil) && next) if in_reply_to_status_id.nil?

    instance = Status.find(in_reply_to_status_id)
    (resolve.(instance) && next) if instance

    Client.current.show_status(in_reply_to_status_id)
    .then do |status|
      resolve.(status)
    end
  end
end

#matches?(query) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/twterm/status.rb', line 32

def matches?(query)
  [text, user.screen_name, user.name]
    .any? { |x| x.downcase.include?(query.downcase) }
end

#repliesObject



89
90
91
# File 'lib/twterm/status.rb', line 89

def replies
  Status.all.select { |s| s.in_reply_to_status_id == id }
end

#retweet!Object



93
94
95
96
# File 'lib/twterm/status.rb', line 93

def retweet!
  @retweet_count += 1
  @retweeted = true
end

#retweeted_byObject



98
99
100
# File 'lib/twterm/status.rb', line 98

def retweeted_by
  User.find(@retweeted_by_user_id)
end

#split(width) ⇒ Object



102
103
104
# File 'lib/twterm/status.rb', line 102

def split(width)
  @splitted_text[width] ||= @text.split_by_width(width)
end

#touch!Object



106
107
108
# File 'lib/twterm/status.rb', line 106

def touch!
  @touched_at = Time.now
end

#unfavorite!Object



110
111
112
113
# File 'lib/twterm/status.rb', line 110

def unfavorite!
  @favorite_count -= 1
  @favorited = false
end

#update!(tweet) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/twterm/status.rb', line 115

def update!(tweet)
  return self if recently_updated?

  @retweet_count = tweet.retweet_count
  @favorite_count = tweet.favorite_count
  @retweeted = tweet.retweeted?
  @favorited = tweet.favorited?

  @updated_at = Time.now

  self
end

#userObject



128
129
130
# File 'lib/twterm/status.rb', line 128

def user
  User.find(user_id)
end