Merge pull request #2777 from stwf/tweak-twitter-truncation

take url shortening into effect before truncating twitter posts
This commit is contained in:
Daniel Grippi 2012-02-29 22:49:46 -08:00
commit 7a774661bb
2 changed files with 51 additions and 1 deletions

View file

@ -1,5 +1,8 @@
require 'uri'
class Services::Twitter < Service class Services::Twitter < Service
MAX_CHARACTERS = 140 MAX_CHARACTERS = 140
SHORTENED_URL_LENGTH = 21
def provider def provider
"twitter" "twitter"
@ -18,8 +21,14 @@ class Services::Twitter < Service
end end
end end
def public_message(post, url) def public_message(post, url)
super(post, MAX_CHARACTERS, url) buffer_amt = 0
URI.extract( post.text(:plain_text => true), ['http','https'] ) do |a_url|
buffer_amt += (a_url.length - SHORTENED_URL_LENGTH)
end
super(post, MAX_CHARACTERS + buffer_amt, url)
end end
def profile_photo_url def profile_photo_url

View file

@ -27,7 +27,48 @@ describe Services::Twitter do
@service.post(@post, url) @service.post(@post, url)
end end
end end
describe "message size limits" do
before :each do
@long_message_start = ActiveSupport::SecureRandom.hex(25)
@long_message_end = ActiveSupport::SecureRandom.hex(25)
end
it "should not truncate a short message" do
short_message = ActiveSupport::SecureRandom.hex(20)
short_post = stub(:text => short_message )
@service.public_message(short_post, '').should == short_message
end
it "should truncate a long message" do
long_message = ActiveSupport::SecureRandom.hex(220)
long_post = stub(:text => long_message )
@service.public_message(long_post, '').should == long_message.first(137) + "..."
end
it "should not truncate a long message with an http url" do
long_message = @long_message_start + " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end
long_post = stub(:text => long_message )
answer = @service.public_message(long_post, '')
answer.starts_with?( @long_message_start ).should be_true
answer.ends_with?( @long_message_end ).should be_true
end
it "should not truncate a long message with an https url" do
long_message = @long_message_start + " https://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end
long_post = stub(:text => long_message )
answer = @service.public_message(long_post, '')
answer.starts_with?( @long_message_start ).should be_true
answer.ends_with?( @long_message_end ).should be_true
end
it "should truncate a long message with an ftp url" do
long_message = @long_message_start + " ftp://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end
long_post = stub(:text => long_message )
answer = @service.public_message(long_post, '')
answer.starts_with?( @long_message_start ).should be_true
answer.ends_with?( @long_message_end ).should_not be_true
end
end
describe "#profile_photo_url" do describe "#profile_photo_url" do
it 'returns the original profile photo url' do it 'returns the original profile photo url' do
stub_request(:get, "https://api.twitter.com/1/users/profile_image/joindiaspora?size=original"). stub_request(:get, "https://api.twitter.com/1/users/profile_image/joindiaspora?size=original").