Check whether the given text is in 140 character limit. URL is taken into account.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
TWEET_MAX_LEN = 140 | |
def tweetable?(text) | |
text = text.dup | |
len = 0 | |
while text.sub!(/http:\/\/[^\s]*/, '') | |
len += 20 | |
end | |
while text.sub!(/https:\/\/[^\s]*/, '') | |
len += 21 | |
end | |
(len + text.length) <= TWEET_MAX_LEN | |
end | |
tweetable?('こんにちは') # => true | |
tweetable?('あ' * 140) # => true | |
tweetable?('あ'*119 + " http://mikutter.hachune.net/") # => true | |
tweetable?('あ'*120 + " http://mikutter.hachune.net/") # => false | |
tweetable?('あ'*118 + " https://mikutter.hachune.net/") # => true | |
tweetable?('あ'*119 + " https://mikutter.hachune.net/") # => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment