Class: Twitter::Tweet

Inherits:
Object
  • Object
show all
Defined in:
lib/twittbot/gem_ext/twitter/tweet.rb

Instance Method Summary collapse

Instance Method Details

#expanded_textString

Returns the full expanded tweet (over 140 characters)

Returns:

  • (String)

    The expanded tweet



60
61
62
# File 'lib/twittbot/gem_ext/twitter/tweet.rb', line 60

def expanded_text
  self.attrs.dig(:extended_tweet, :full_text) || full_text || text
end

#favouriteObject Also known as: fav, fave, favorite

Favourites a tweet



32
33
34
35
36
37
# File 'lib/twittbot/gem_ext/twitter/tweet.rb', line 32

def favourite
  return if $bot.nil? or $bot[:client].nil?
  $bot[:client].favorite self.id
rescue Twitter::Error => e
  puts "caught Twitter error while favouriting: #{e.message}"
end

#mentioned_users(reply_all = true, screen_name = $bot[:config][:screen_name]) ⇒ Array

Scans the tweet text for screen names.

Parameters:

  • reply_all (Boolean) (defaults to: true)

    Include all users in the reply.

  • screen_name (String) (defaults to: $bot[:config][:screen_name])

    The user’s screen name (i.e. that one who clicked “Reply”)

Returns:

  • (Array)

    An array of user names.



47
48
49
50
51
52
53
54
55
56
# File 'lib/twittbot/gem_ext/twitter/tweet.rb', line 47

def mentioned_users(reply_all = true, screen_name = $bot[:config][:screen_name])
  userlist = [ self.user.screen_name ]
  if reply_all
    self.text.scan /@([A-Za-z0-9_]{1,16})/ do |user_name|
      user_name = user_name[0]
      userlist << user_name unless userlist.include?(user_name) or screen_name == user_name
    end
  end
  userlist
end

#reply(tweet_text, options = {}) ⇒ Object

Creates a reply to this tweet.

Parameters:

  • tweet_text (:String)

    tweet text

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :reply_all (Boolean) — default: false

    Add all users mentioned in the tweet text to the reply.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/twittbot/gem_ext/twitter/tweet.rb', line 7

def reply(tweet_text, options = {})
  return if $bot.nil? or $bot[:client].nil?
  opts = {
    reply_all: false
  }.merge(options)

  mentions = self.mentioned_users(opts[:reply_all])

  result = "@#{mentions.join(" @")} #{tweet_text}"[(0...140)]

  $bot[:client].update result, in_reply_to_status_id: self.id
rescue Twitter::Error => e
  puts "caught Twitter error while replying: #{e.message}"
end

#retweetObject Also known as: rt

Retweets this tweet.



23
24
25
26
27
28
# File 'lib/twittbot/gem_ext/twitter/tweet.rb', line 23

def retweet
  return if $bot.nil? or $bot[:client].nil?
  $bot[:client].retweet self.id
rescue Twitter::Error => e
  puts "caught Twitter error while retweeting: #{e.message}"
end