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.2"
Class Method Summary collapse
- .expand_urls_text(tweet, text) ⇒ String
- .remove_media_urls_in_tweet(tweet, text) ⇒ String
-
.sanitize(tweet, use_retweeted_tweet: true, expand_url: true, remove_media_url: true, unescape: true) ⇒ String
Sanitize tweet.
-
.tweet_full_text(tweet) ⇒ String
full_text
attribute if exist.
Class Method Details
.expand_urls_text(tweet, text) ⇒ String
29 30 31 32 33 34 35 36 37 |
# File 'lib/tweet_sanitizer.rb', line 29 def self.(tweet, text) return text unless tweet.uris? tweet.uris.reverse.each_with_object(text.dup) do |uri, | pos1 = uri.indices[0] pos2 = uri.indices[1] [pos1, pos2-pos1] = uri. if uri. end end |
.remove_media_urls_in_tweet(tweet, text) ⇒ 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
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 = (tweet, text) if 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.
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 |