From 5d15fa43b5c64a18d95c8d2969a4e5e576528070 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Sun, 29 Jan 2012 17:42:17 -0500 Subject: [PATCH] take url shortening into effect before truncating twitter posts --- app/models/services/twitter.rb | 11 +++++++- spec/models/services/twitter_spec.rb | 42 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index 7d35f6899..80563d97d 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -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 diff --git a/spec/models/services/twitter_spec.rb b/spec/models/services/twitter_spec.rb index ca4cab562..e061f44f3 100644 --- a/spec/models/services/twitter_spec.rb +++ b/spec/models/services/twitter_spec.rb @@ -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").