Class: Tweet

Inherits:
Object
  • Object
show all
Defined in:
lib/twiterator/tweet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user = nil) ⇒ Tweet

Returns a new instance of Tweet.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/twiterator/tweet.rb', line 5

def initialize(url, user=nil)
  @html = open(url)
  @doc = Nokogiri::HTML(html)
  @url = url
  @user = user
  # Had these as setter/getters but it was more efficient to scale them down into methods:
  # @content = doc.css('.js-tweet-text-container p')[0].text
  # @display_name = doc.css('.permalink-header fullname').text
  # @user_name = doc.css('.js-action-profile-name b')[0].text.strip
  # @time = doc.css('.client-and-actions').text.strip.split(" - ")[0]
  # @date = doc.css('.client-and-actions').text.strip.split(" - ")[1]
  @replies = []
  # @retweets = doc.css('.request-retweeted-popup strong').text
  # @likes = doc.css('.js-stat-favorites strong').text
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



3
4
5
# File 'lib/twiterator/tweet.rb', line 3

def doc
  @doc
end

#htmlObject (readonly)

Returns the value of attribute html.



3
4
5
# File 'lib/twiterator/tweet.rb', line 3

def html
  @html
end

#repliesObject

Returns the value of attribute replies.



2
3
4
# File 'lib/twiterator/tweet.rb', line 2

def replies
  @replies
end

#reply_counterObject

Returns the value of attribute reply_counter.



2
3
4
# File 'lib/twiterator/tweet.rb', line 2

def reply_counter
  @reply_counter
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/twiterator/tweet.rb', line 3

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/twiterator/tweet.rb', line 3

def user
  @user
end

Instance Method Details

#contentObject



21
22
23
# File 'lib/twiterator/tweet.rb', line 21

def content
  doc.css('.js-tweet-text-container p')[0].text
end

#dateObject



37
38
39
# File 'lib/twiterator/tweet.rb', line 37

def date
  doc.css('.client-and-actions').text.strip.split(" - ")[1]
end

#display_nameObject



25
26
27
# File 'lib/twiterator/tweet.rb', line 25

def display_name
  doc.css('.permalink-header fullname').text
end

#likesObject



45
46
47
# File 'lib/twiterator/tweet.rb', line 45

def likes
  doc.css('.js-stat-favorites strong').text
end

#reply_countObject



49
50
51
# File 'lib/twiterator/tweet.rb', line 49

def reply_count
  self.doc.css('.ProfileTweet-actionCount')[0].text.strip.gsub(/[^0-9]/, "").to_i
end

#retweeted?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/twiterator/tweet.rb', line 53

def retweeted?
  self.user_name.downcase != self.user.user_name.downcase || false
end

#retweetsObject



41
42
43
# File 'lib/twiterator/tweet.rb', line 41

def retweets
  doc.css('.request-retweeted-popup strong').text
end

#set_repliesObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/twiterator/tweet.rb', line 57

def set_replies
  reply_counter = 0
  #binding.pry
  # The code snippet in "reply_count" reads off the number of replies that there are, then would have helped this method loop for exactly that many times. Sadly, twitter only displays 15 replies to ruby, so I had to settle for a 15-iteration loop. So now the method just checks to see if the reply count is UNDER 15.
  (reply_count < 15 ? reply_count : 16).times do
      parent_tweet = self
      self.replies << Reply.new(reply_counter, parent_tweet)
      reply_counter += 1
    end
  self.replies.pop
end

#timeObject



33
34
35
# File 'lib/twiterator/tweet.rb', line 33

def time
  doc.css('.client-and-actions').text.strip.split(" - ")[0]
end

#user_nameObject



29
30
31
# File 'lib/twiterator/tweet.rb', line 29

def user_name
  doc.css('.js-action-profile-name b')[0].text.strip
end