take url shortening into effect before truncating twitter posts

This commit is contained in:
Steven Fuchs 2012-01-29 17:42:17 -05:00
parent 0e0d40d00e
commit 5d15fa43b5
2 changed files with 52 additions and 1 deletions

View file

@ -1,5 +1,8 @@
require 'uri'
class Services::Twitter < Service
MAX_CHARACTERS = 140
SHORTENED_URL_LENGTH = 21
def provider
"twitter"
@ -18,8 +21,14 @@ class Services::Twitter < Service
end
end
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
def profile_photo_url

View file

@ -27,7 +27,49 @@ describe Services::Twitter do
@service.post(@post, url)
end
end
describe "message size limits" do
before :each do
end
it "should not truncate a short message" do
short_message = ActiveSupport::SecureRandom.hex(20)
short_post = @user.post(:status_message, :text => short_message, :to =>@user.aspects.first.id)
@service.public_message(short_post, '').should == short_message
end
it "should truncate a long message" do
long_message = ActiveSupport::SecureRandom.hex(220)
long_post = @user.post(:status_message, :text => long_message, :to =>@user.aspects.first.id)
@service.public_message(long_post, '').should_not == long_message
end
it "should not truncate a long message with an http url" do
long_message_part = ActiveSupport::SecureRandom.hex(25)
long_message = long_message_part + " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + long_message_part
long_post = @user.post(:status_message, :text => long_message, :to =>@user.aspects.first.id)
answer = @service.public_message(long_post, '')
answer.starts_with?( long_message_part ).should == true
answer.ends_with?( long_message_part ).should == true
end
it "should not truncate a long message with an https url" do
long_message_part = ActiveSupport::SecureRandom.hex(25)
long_message = long_message_part + " https://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + long_message_part
long_post = @user.post(:status_message, :text => long_message, :to =>@user.aspects.first.id)
answer = @service.public_message(long_post, '')
answer.starts_with?( long_message_part ).should == true
answer.ends_with?( long_message_part ).should == true
end
it "should truncate a long message with an ftp url" do
long_message_part = ActiveSupport::SecureRandom.hex(25)
long_message = long_message_part + " ftp://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + long_message_part
long_post = @user.post(:status_message, :text => long_message, :to =>@user.aspects.first.id)
answer = @service.public_message(long_post, '')
answer.starts_with?( long_message_part ).should == true
answer.ends_with?( long_message_part ).should == false
end
end
describe "#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").