Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
Since Twitter automatically shortens all URLs to t.co-URLs, it isn't …
Browse files Browse the repository at this point in the history
…necessary to shorten the links ourselves. Now the tweets can get as long as possible.
  • Loading branch information
fabianonline committed Aug 5, 2012
1 parent 0c208c9 commit 3ac112d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
22 changes: 16 additions & 6 deletions services/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ def receive_push
if data['digest'] == '1'
commit = payload['commits'][-1]
author = commit['author'] || {}
tiny_url = shorten_url("#{payload['repository']['url']}/commits/#{ref_name}")
status = "[#{repository}] #{tiny_url} #{author['name']} - #{payload['commits'].length} commits"
status.length >= 140 ? statuses << status[0..136] + '...' : statuses << status
url = "#{payload['repository']['url']}/commits/#{ref_name}"
status = "[#{repository}] #{url} #{author['name']} - #{payload['commits'].length} commits"
length = status.length - url.length + 21 # The URL is going to be shortened by twitter. It's length will be at most 21 chars (HTTPS).
# How many chars of the status can we actually use?
# We can use 140 chars, have to reserve 3 chars for the railing dots (-3)
# also 21 chars for the t.co-URL (-21) but can fit the whole URL into the tweet (+url.length)
usable_chars = 140 - 3 - 21 + url.length
length >= 140 ? statuses << status[0..(usable_chars-1)] + '...' : statuses << status
else
payload['commits'].each do |commit|
author = commit['author'] || {}
tiny_url = shorten_url(commit['url'])
status = "[#{repository}] #{tiny_url} #{author['name']} - #{commit['message']}"
status.length >= 140 ? statuses << status[0..136] + '...' : statuses << status
url = commit['url']
status = "[#{repository}] #{url} #{author['name']} - #{commit['message']}"
length = status.length - url.length + 21 # The URL is going to be shortened by twitter. It's length will be at most 21 chars (HTTPS).
# How many chars of the status can we actually use?
# We can use 140 chars, have to reserve 3 chars for the railing dots (-3)
# also 21 chars for the t.co-URL (-21) but can fit the whole URL into the tweet (+url.length)
usable_chars = 140 - 3 - 21 + url.length
length >= 140 ? statuses << status[0..(usable_chars-1)] + '...' : statuses << status
end
end

Expand Down
21 changes: 21 additions & 0 deletions test/twitter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ def test_oauth_consumer
assert_equal 'cs', svc.consumer_secret
assert svc.consumer
end

def test_tweet_length
p = payload
p['commits'][0]['message']="This is a very long message specifically designed to test the new behaviour of the twitter service hook with extremely long tweets. As should be happening now."
svc = service({'token' => 't', 'secret' => 's'}, p)

def svc.statuses
@statuses ||= []
end

def svc.post(status)
statuses << status
end

svc.receive_push

svc.statuses.each do |st|
st = st.gsub(/http[^ ]+/, "a"*21) # replace the URL with a substitute for the shortened one
assert st.length<=140
end
end

def service(*args)
super Service::Twitter, *args
Expand Down

0 comments on commit 3ac112d

Please sign in to comment.