Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created February 27, 2016 00:37
Show Gist options
  • Save mindplace/3bbe7bfa9193eed87e76 to your computer and use it in GitHub Desktop.
Save mindplace/3bbe7bfa9193eed87e76 to your computer and use it in GitHub Desktop.
def shorten_url(message)
puts "Shortening links..."
urls = Hash.new(0)
url = message.split.select{|item| item.include?("http") || item.include?("www")}
text = message.split.select{|item| !url.include?(item)}.join(" ")
url.each do |item|
url_index = message.split.index(item)
item = "https://" + item unless item.include?("https://")
Bitly.use_api_version_3
bitly = Bitly.new("###", "####")
item = bitly.shorten(item).short_url
urls[item] = url_index
end
new_message = text
urls.each do |item, i|
new_message = new_message.split.insert(i, item).join(" ")
end
puts "New tweet: #{new_message}"
new_message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment