Class: Twterm::Status

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

Constant Summary collapse

MAX_CACHED_STATUSES_COUNT =
1000
@@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
# 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 = {}

  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_byObject (readonly)

Returns the value of attribute retweeted_by.



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

def retweeted_by
  @retweeted_by
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

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.cleanupObject



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

#dateObject



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 expand_url!
  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