Class: Tweet
- Inherits:
-
Object
- Object
- Tweet
- Defined in:
- lib/twiterator/tweet.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#html ⇒ Object
readonly
Returns the value of attribute html.
-
#replies ⇒ Object
Returns the value of attribute replies.
-
#reply_counter ⇒ Object
Returns the value of attribute reply_counter.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #content ⇒ Object
- #date ⇒ Object
- #display_name ⇒ Object
-
#initialize(url, user = nil) ⇒ Tweet
constructor
A new instance of Tweet.
- #likes ⇒ Object
- #reply_count ⇒ Object
- #retweeted? ⇒ Boolean
- #retweets ⇒ Object
- #set_replies ⇒ Object
- #time ⇒ Object
- #user_name ⇒ Object
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
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
3 4 5 |
# File 'lib/twiterator/tweet.rb', line 3 def doc @doc end |
#html ⇒ Object (readonly)
Returns the value of attribute html.
3 4 5 |
# File 'lib/twiterator/tweet.rb', line 3 def html @html end |
#replies ⇒ Object
Returns the value of attribute replies.
2 3 4 |
# File 'lib/twiterator/tweet.rb', line 2 def replies @replies end |
#reply_counter ⇒ Object
Returns the value of attribute reply_counter.
2 3 4 |
# File 'lib/twiterator/tweet.rb', line 2 def reply_counter @reply_counter end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
3 4 5 |
# File 'lib/twiterator/tweet.rb', line 3 def url @url end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
3 4 5 |
# File 'lib/twiterator/tweet.rb', line 3 def user @user end |
Instance Method Details
#content ⇒ Object
21 22 23 |
# File 'lib/twiterator/tweet.rb', line 21 def content doc.css('.js-tweet-text-container p')[0].text end |
#date ⇒ Object
37 38 39 |
# File 'lib/twiterator/tweet.rb', line 37 def date doc.css('.client-and-actions').text.strip.split(" - ")[1] end |
#display_name ⇒ Object
25 26 27 |
# File 'lib/twiterator/tweet.rb', line 25 def display_name doc.css('.permalink-header fullname').text end |
#likes ⇒ Object
45 46 47 |
# File 'lib/twiterator/tweet.rb', line 45 def likes doc.css('.js-stat-favorites strong').text end |
#reply_count ⇒ Object
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
53 54 55 |
# File 'lib/twiterator/tweet.rb', line 53 def retweeted? self.user_name.downcase != self.user.user_name.downcase || false end |
#retweets ⇒ Object
41 42 43 |
# File 'lib/twiterator/tweet.rb', line 41 def retweets doc.css('.request-retweeted-popup strong').text end |
#set_replies ⇒ Object
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 |
#time ⇒ Object
33 34 35 |
# File 'lib/twiterator/tweet.rb', line 33 def time doc.css('.client-and-actions').text.strip.split(" - ")[0] end |
#user_name ⇒ Object
29 30 31 |
# File 'lib/twiterator/tweet.rb', line 29 def user_name doc.css('.js-action-profile-name b')[0].text.strip end |