Skip to content

Commit

Permalink
Handle overlong retweeted messsage
Browse files Browse the repository at this point in the history
  • Loading branch information
mlandauer committed Dec 12, 2011
1 parent 5c0ca82 commit ef55ae2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app.rb
Expand Up @@ -65,7 +65,13 @@ def respond_to_tweet(status)
authority = Authority.find_by_location("#{status.geo.coordinates[1]},#{status.geo.coordinates[0]}")
if authority
if authority.twitter_screen_name
Twitter.update("#{authority.twitter_screen_name} RT @#{status.user.screen_name}: #{status.text.gsub(hashtag, '')}")
message = "#{authority.twitter_screen_name} RT @#{status.user.screen_name}: #{status.text.gsub(hashtag, '')}"
if message.length > 140
# Truncate the message to 116 characters so that we can append "... " and the url of the original tweet which
# will get shortened to 20 characters
message = message[0..115] + "... " + "https://twitter.com/#{status.user.screen_name}/status/#{status.id_str}"
end
Twitter.update(message)
elsif authority.contact_email
AuthorityMailer.email(authority.contact_email, status.text.gsub(hashtag, ''), "https://twitter.com/#{status.user.screen_name}/status/#{status.id_str}").deliver
end
Expand Down
15 changes: 15 additions & 0 deletions spec/app_spec.rb
Expand Up @@ -88,5 +88,20 @@ def app
respond_to_tweet(status)
end
end

it "should shorten the RT to council to 140 characters and add a link to the original tweet if necessary" do
status = mock(:geo => mock(:coordinates => [-33.8736, 151.2076]), :user => user, :id => 1001, :id_str => "1001",
:text => "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234 #tmyc")
# This message is the maximum possible in Twitter
status.text.length.should == 140

authority = mock(:twitter_screen_name => "@mycouncil")
Geo2gov.should_receive(:new).with("151.2076,-33.8736").and_return(mock(:lga_code => "LGA123"))
Authority.should_receive(:find_by_lga_code).with(123).and_return(authority)
# The link at the end will be shortened to 20 characters (by Twitter)
Twitter.should_receive(:update).with("@mycouncil RT @matthewlandauer: 123456789012345678901234567890123456789012345678901234567890123456789012345678901234... https://twitter.com/matthewlandauer/status/1001")

respond_to_tweet(status)
end
end
end

0 comments on commit ef55ae2

Please sign in to comment.