Skip to content

Instantly share code, notes, and snippets.

@mhl
Created September 20, 2009 18:06
Show Gist options
  • Save mhl/189866 to your computer and use it in GitHub Desktop.
Save mhl/189866 to your computer and use it in GitHub Desktop.
Create a tinyurl from Ruby
#!/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