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.



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
51
52
# File 'lib/twterm/status.rb', line 16

def initialize(tweet)
  unless tweet.retweeted_status.is_a? Twitter::NullObject
    @retweeted_by_user_id = tweet.user.id
    User.create(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_id = tweet.user.id
  User.create(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

#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

#created_at_for_sortObject (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_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



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

def all
  @@instances.values
end

.cleanupObject



146
147
148
149
150
151
152
153
154
# File 'lib/twterm/status.rb', line 146

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

.find(id) ⇒ Object



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

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

.find_or_fetch(id) ⇒ Object



135
136
137
138
139
140
# File 'lib/twterm/status.rb', line 135

def find_or_fetch(id)
  instance = find(id)
  (yield(instance) && return) if instance

  Client.current.show_status(id) { |status| yield status }
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



142
143
144
# File 'lib/twterm/status.rb', line 142

def parse_time(time)
  (time.is_a?(String) ? Time.parse(time) : time.dup).localtime
end

Instance Method Details

#==(other) ⇒ Object



122
123
124
# File 'lib/twterm/status.rb', line 122

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

#dateObject



62
63
64
65
# File 'lib/twterm/status.rb', line 62

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



67
68
69
70
# File 'lib/twterm/status.rb', line 67

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

#favorite!Object



72
73
74
75
# File 'lib/twterm/status.rb', line 72

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

#in_reply_to_status(&block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/twterm/status.rb', line 91

def in_reply_to_status(&block)
  if @in_reply_to_status_id.nil?
    block.call(nil)
    return
  end

  status = Status.find(@in_reply_to_status_id)
  unless status.nil?
    block.call(status)
    return
  end

  Client.current.show_status(@in_reply_to_status_id, &block)
end

#repliesObject



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

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

#retweet!Object



82
83
84
85
# File 'lib/twterm/status.rb', line 82

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

#retweeted_byObject



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

def retweeted_by
  User.find(@retweeted_by_user_id)
end

#split(width) ⇒ Object



87
88
89
# File 'lib/twterm/status.rb', line 87

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

#touch!Object



114
115
116
# File 'lib/twterm/status.rb', line 114

def touch!
  @touched_at = Time.now
end

#unfavorite!Object



77
78
79
80
# File 'lib/twterm/status.rb', line 77

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

#update!(tweet) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/twterm/status.rb', line 54

def update!(tweet)
  @retweet_count = tweet.retweet_count
  @favorite_count = tweet.favorite_count
  @retweeted = tweet.retweeted?
  @favorited = tweet.favorited?
  self
end

#userObject



118
119
120
# File 'lib/twterm/status.rb', line 118

def user
  User.find(user_id)
end