Created
September 20, 2009 18:06
-
-
Save mhl/189866 to your computer and use it in GitHub Desktop.
Create a tinyurl from Ruby
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/ruby | |
require 'cgi' | |
unless ARGV.length == 1 | |
STDERR.puts "Usage: tinyurl <URL>" | |
exit(1) | |
end | |
url = ARGV[0] | |
def tinyurl(url) | |
tinyurl = nil | |
Kernel.open( "|-", "r" ) do |f| | |
if f | |
tinyurl = f.read | |
else | |
exec "curl", "-s", "http://tinyurl.com/api-create.php?url=#{CGI.escape(url)}" | |
end | |
end | |
tinyurl | |
end | |
puts "tinyurl is #{tinyurl(url)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment