Class: Ebooks::TweetMeta

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_ebooks/bot.rb

Overview

Meta information about a tweet that we calculate for ourselves

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, ev) ⇒ TweetMeta

Returns a new instance of TweetMeta.

Parameters:



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
# 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|
    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

Instance Attribute Details

#botEbooks::Bot

Returns associated bot.

Returns:



62
63
64
# File 'lib/twitter_ebooks/bot.rb', line 62

def bot
  @bot
end

#limitInteger

Returns available chars for reply.

Returns:

  • (Integer)

    available chars for reply



59
60
61
# File 'lib/twitter_ebooks/bot.rb', line 59

def limit
  @limit
end

#mentionlessString

Returns text of tweets with mentions removed.

Returns:

  • (String)

    text of tweets with mentions removed



53
54
55
# File 'lib/twitter_ebooks/bot.rb', line 53

def mentionless
  @mentionless
end

#mentionsArray<String>

Returns usernames mentioned in tweet.

Returns:

  • (Array<String>)

    usernames mentioned in tweet



51
52
53
# File 'lib/twitter_ebooks/bot.rb', line 51

def mentions
  @mentions
end

#reply_mentionsArray<String>

Returns usernames to include in a reply.

Returns:

  • (Array<String>)

    usernames to include in a reply



55
56
57
# File 'lib/twitter_ebooks/bot.rb', line 55

def reply_mentions
  @reply_mentions
end

#reply_prefixString

Returns mentions to start reply with.

Returns:

  • (String)

    mentions to start reply with



57
58
59
# File 'lib/twitter_ebooks/bot.rb', line 57

def reply_prefix
  @reply_prefix
end

#tweetTwitter::Tweet

Returns associated tweet.

Returns:

  • (Twitter::Tweet)

    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.

Parameters:

  • size (String)

    A twitter image size to return. Supported sizes are thumb, small, medium (default), large

Returns:

  • (Array<String>)

    image URIs included in tweet



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/twitter_ebooks/bot.rb', line 111

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

Returns:

  • (Boolean)


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