Skip to content

Instantly share code, notes, and snippets.

@naps62
Last active December 16, 2015 18:00
Show Gist options
  • Save naps62/5474888 to your computer and use it in GitHub Desktop.
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
#!/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