Last active
December 16, 2015 18:00
-
-
Save naps62/5474888 to your computer and use it in GitHub Desktop.
Ruby script to quickly shorten a url using the bit.ly API. The only requirements are the bitly and clipboard gems, and BITLY_USERNAME and BITLY_API_KEY environment variables
This file contains 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
#!/usr/bin/env ruby | |
require 'bitly' | |
require 'clipboard' | |
require 'uri' | |
username = ENV['BITLY_USERNAME'] | |
api_key = ENV['BITLY_API_KEY'] | |
url = ARGV.shift | |
unless username && api_key | |
puts 'BITLY_USERNAME or BITLY_API_KEY not set' | |
exit | |
end | |
parsed_url = URI.parse(url) | |
parsed_url = URI.parse('http://' + url) unless parsed_url.scheme | |
Bitly.use_api_version_3 | |
bitly = Bitly.new(username, api_key) | |
response = bitly.shorten(parsed_url) | |
result = response.short_url if response.respond_to? :short_url | |
if result | |
Clipboard.copy result | |
puts "Result copied to clipboard:\n#{result}" | |
else | |
puts 'Could not complete the request' | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment