Module: TweetSanitizer

Defined in:
lib/tweet_sanitizer.rb,
lib/tweet_sanitizer/version.rb,
lib/tweet_sanitizer/twitter_extension.rb

Defined Under Namespace

Modules: TwitterExtension

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.expand_urls_text(tweet, text) ⇒ String

Parameters:

  • tweet (Twitter:Tweet)
  • text (String)

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
# File 'lib/tweet_sanitizer.rb', line 29

def self.expand_urls_text(tweet, text)
  return text unless tweet.uris?

  tweet.uris.reverse.each_with_object(text.dup) do |uri, expanded|
    pos1 = uri.indices[0]
    pos2 = uri.indices[1]
    expanded[pos1, pos2-pos1] = uri.expanded_url
  end
end

.remove_media_urls_in_tweet(tweet, text) ⇒ String

Parameters:

  • tweet (Twitter:Tweet)
  • text (String)

Returns:

  • (String)


50
51
52
53
54
55
56
57
# File 'lib/tweet_sanitizer.rb', line 50

def self.remove_media_urls_in_tweet(tweet, text)
  return text unless tweet.media?

  tweet.media.each_with_object(text.dup) do |media, t|
    t.gsub!(media.url, "")
    t.strip!
  end
end

.sanitize(tweet, use_retweeted_tweet: true, expand_url: true, remove_media_url: true, unescape: true) ⇒ String

Sanitize tweet

Parameters:

  • tweet (Twitter:Tweet)
  • use_retweeted_tweet (Boolean) (defaults to: true)

    Whether use original retweeted tweet if exists

  • expand_url (Boolean) (defaults to: true)

    Whether expand url in tweet (e.g. ‘t.co` url -> original url)

  • remove_media_url (Boolean) (defaults to: true)

    Whether remove media url in tweet

  • unescape (Boolean) (defaults to: true)

    Whether unescape in tweet (e.g. ‘(&gt; &lt;)` -> `(> <)`)

Returns:

  • (String)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tweet_sanitizer.rb', line 13

def self.sanitize(tweet, use_retweeted_tweet: true, expand_url: true, remove_media_url: true, unescape: true)
  # Original RT status is exists in retweeted_status
  if use_retweeted_tweet && tweet.attrs[:retweeted_status]
    tweet = Twitter::Tweet.new(tweet.attrs[:retweeted_status])
  end

  text = tweet_full_text(tweet)
  text = expand_urls_text(tweet, text) if expand_url
  text = remove_media_urls_in_tweet(tweet, text) if remove_media_url
  text = CGI.unescapeHTML(text) if unescape
  text
end

.tweet_full_text(tweet) ⇒ String

Returns ‘full_text` attribute if exist.

Parameters:

  • tweet (Twitter:Tweet)

Returns:

  • (String)

    ‘full_text` attribute if exist



41
42
43
44
45
# File 'lib/tweet_sanitizer.rb', line 41

def self.tweet_full_text(tweet)
  # NOTE: Twitter::Tweet#full_text doesn't returns full_text attribute
  # https://github.com/sferik/twitter/blob/v6.1.0/lib/twitter/tweet.rb#L37-L44
  tweet.attrs[:full_text] || tweet.text
end