Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:twitter/twitter-text-rb
Browse files Browse the repository at this point in the history
  • Loading branch information
keita committed May 18, 2012
2 parents 4d6ac3b + 07899bb commit 36fc9b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/validation.rb
Expand Up @@ -2,6 +2,11 @@ module Twitter
module Validation extend self
MAX_LENGTH = 140

DEFAULT_TCO_URL_LENGTHS = {
:short_url_length => 20,
:short_url_length_https => 21
}.freeze

# Returns the length of the string as it would be displayed. This is equivilent to the length of the Unicode NFC
# (See: http://www.unicode.org/reports/tr15). This is needed in order to consistently calculate the length of a
# string no matter which actual form was transmitted. For example:
Expand All @@ -14,8 +19,17 @@ module Validation extend self
#
# The string could also contain U+00E9 already, in which case the canonicalization will not change the value.
#
def tweet_length(text)
ActiveSupport::Multibyte::Chars.new(text).normalize(:c).length
def tweet_length(text, options = {})
options = DEFAULT_TCO_URL_LENGTHS.merge(options)

length = ActiveSupport::Multibyte::Chars.new(text).normalize(:c).length

Twitter::Extractor.extract_urls_with_indices(text) do |url, start_position, end_position|
length += start_position - end_position
length += url.downcase =~ /^https:\/\// ? options[:short_url_length_https] : options[:short_url_length]
end

length
end

# Check the <tt>text</tt> for any reason that it may not be valid as a Tweet. This is meant as a pre-validation
Expand Down
4 changes: 4 additions & 0 deletions test/conformance_test.rb
Expand Up @@ -173,4 +173,8 @@ def self.def_conformance_test(file, test_type, &block)
def_conformance_test("validate.yml", :hashtags) do
assert_equal expected, valid_hashtag?(text), description
end

def_conformance_test("validate.yml", :lengths) do
assert_equal expected, tweet_length(text), description
end
end

0 comments on commit 36fc9b2

Please sign in to comment.