Class: Ebooks::TweetMeta
- Inherits:
-
Object
- Object
- Ebooks::TweetMeta
- Defined in:
- lib/twitter_ebooks/bot.rb
Overview
Meta information about a tweet that we calculate for ourselves
Instance Attribute Summary collapse
-
#bot ⇒ Ebooks::Bot
Associated bot.
-
#limit ⇒ Integer
Available chars for reply.
-
#mentionless ⇒ String
Text of tweets with mentions removed.
-
#mentions ⇒ Array<String>
Usernames mentioned in tweet.
-
#reply_mentions ⇒ Array<String>
Usernames to include in a reply.
-
#reply_prefix ⇒ String
Mentions to start reply with.
-
#tweet ⇒ Twitter::Tweet
Associated tweet.
Instance Method Summary collapse
-
#initialize(bot, ev) ⇒ TweetMeta
constructor
A new instance of TweetMeta.
-
#media_uris(size_input = '') ⇒ Array<String>
Get an array of media uris in tweet.
-
#mentions_bot? ⇒ Boolean
Check whether this tweet mentions our bot.
Constructor Details
#initialize(bot, ev) ⇒ TweetMeta
Returns a new instance of TweetMeta.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/twitter_ebooks/bot.rb', line 78 def initialize(bot, ev) @bot = bot @tweet = ev @mentions = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] } # Process mentions to figure out who to reply to # i.e. not self and nobody who has seen too many secondary mentions reply_mentions = @mentions.reject do |m| username = m.downcase username == @bot.username || !@bot.conversation(ev).can_include?(username) end @reply_mentions = ([ev.user.screen_name] + reply_mentions).uniq @reply_prefix = @reply_mentions.map { |m| '@'+m }.join(' ') + ' ' @limit = 140 - @reply_prefix.length mless = ev.text begin ev.attrs[:entities][:user_mentions].reverse.each do |entity| last = mless[entity[:indices][1]..-1]||'' mless = mless[0...entity[:indices][0]] + last.strip end rescue Exception p ev.attrs[:entities][:user_mentions] p ev.text raise end @mentionless = mless end |
Instance Attribute Details
#bot ⇒ Ebooks::Bot
Returns associated bot.
62 63 64 |
# File 'lib/twitter_ebooks/bot.rb', line 62 def bot @bot end |
#limit ⇒ Integer
Returns available chars for reply.
59 60 61 |
# File 'lib/twitter_ebooks/bot.rb', line 59 def limit @limit end |
#mentionless ⇒ String
Returns text of tweets with mentions removed.
53 54 55 |
# File 'lib/twitter_ebooks/bot.rb', line 53 def mentionless @mentionless end |
#mentions ⇒ Array<String>
Returns usernames mentioned in tweet.
51 52 53 |
# File 'lib/twitter_ebooks/bot.rb', line 51 def mentions @mentions end |
#reply_mentions ⇒ Array<String>
Returns usernames to include in a reply.
55 56 57 |
# File 'lib/twitter_ebooks/bot.rb', line 55 def reply_mentions @reply_mentions end |
#reply_prefix ⇒ String
Returns mentions to start reply with.
57 58 59 |
# File 'lib/twitter_ebooks/bot.rb', line 57 def reply_prefix @reply_prefix end |
#tweet ⇒ Twitter::Tweet
Returns associated tweet.
64 65 66 |
# File 'lib/twitter_ebooks/bot.rb', line 64 def tweet @tweet end |
Instance Method Details
#media_uris(size_input = '') ⇒ Array<String>
Get an array of media uris in tweet.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/twitter_ebooks/bot.rb', line 112 def media_uris(size_input = '') case size_input when 'thumb' size = ':thumb' when 'small' size = ':small' when 'medium' size = ':medium' when 'large' size = ':large' else size = '' end # Start collecting uris. uris = [] if @tweet.media? @tweet.media.each do |each_media| uris << each_media.media_url.to_s + size end end # and that's pretty much it! uris end |
#mentions_bot? ⇒ Boolean
Check whether this tweet mentions our bot
68 69 70 71 72 73 74 |
# File 'lib/twitter_ebooks/bot.rb', line 68 def mentions_bot? # To check if this is someone talking to us, ensure: # - The tweet mentions list contains our username # - The tweet is not being retweeted by somebody else # - Or soft-retweeted by somebody else @mentions.map(&:downcase).include?(@bot.username.downcase) && !@tweet.retweeted_status? && !@tweet.text.match(/([`'‘’"“”]|RT|via|by|from)\s*@/i) end |