class Ebooks::TweetMeta

Meta information about a tweet that we calculate for ourselves

Attributes

bot[RW]

@return [Ebooks::Bot] associated bot

limit[RW]

@return [Integer] available chars for reply

mentionless[RW]

@return [String] text of tweets with mentions removed

mentions[RW]

@return [Array<String>] usernames mentioned in tweet

reply_mentions[RW]

@return [Array<String>] usernames to include in a reply

reply_prefix[RW]

@return [String] mentions to start reply with

tweet[RW]

@return [Twitter::Tweet] associated tweet

Public Class Methods

new(bot, ev) click to toggle source

@param bot [Ebooks::Bot] @param ev [Twitter::Tweet]

# File lib/bot_twitter_ebooks/bot.rb, line 84
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|
    m.downcase == @bot.username.downcase || !@bot.conversation(ev).can_include?(m)
  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

Public Instance Methods

media_uris(size_input = '') click to toggle source

Get an array of media uris in tweet. @param size [String] A twitter image size to return. Supported sizes are thumb, small, medium (default), large @return [Array<String>] image URIs included in tweet

# File lib/bot_twitter_ebooks/bot.rb, line 117
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?() click to toggle source

Check whether this tweet mentions our bot @return [Boolean]

# File lib/bot_twitter_ebooks/bot.rb, line 74
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