Created
February 27, 2016 00:37
-
-
Save mindplace/3bbe7bfa9193eed87e76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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