Monday, November 08, 2010 at 12:01 AM.


on create (longUrl, flUrlEncode=false) {
		<<3/7/08; 3:01:18 PM by DW
			<<Major rewrite, using little-known feature of the TinyUrl API.
			<<Got the scoop from Dan MacTough -- you can call using POST, and it doesn't have a problem with URL-encoding in that case.
			<<The second parameter, flUrlEncode, is still there to maintain backward compatibility, but it is ignored in all cases.
		<<1/28/08; 10:28:55 AM by DW
			<<Make the url-encoding optional, it doesn't (appear to) work with
		<<1/8/08; 7:22:10 PM by DW
			<<URL-encode the long url, this makes it work for addresses with a parameter list.
		<<10/26/07; 5:11:26 PM by DW
			<<Remove whitespace.
		<<10/26/07; 12:58:20 PM by DW
			<<Allow configuration in user.tinyUrl table.
		<<6/27/07; 11:08:57 AM by DW
			<<Created. A wonderfully simple API.
	bundle { //init
		if not defined (user.tinyUrl) {
			new (tabletype, @user.tinyUrl)};
		if not defined (user.tinyUrl.server) {
			user.tinyUrl.server = ""};
		if defined (user.tinyUrl.path) { //3/7/08 by DW
			if user.tinyUrl.path == "/create?url=" {
				<<it's the old path, the one that works with GET method, 
				delete (@user.tinyUrl.path)}};
		if not defined (user.tinyUrl.path) {
			user.tinyUrl.path = "/api-create.php"}};
	<<if flUrlEncode
		<<longUrl = string.urlencode (longUrl)
	local (s);
	bundle { //make the HTTP request, put result into "s"
		s = tcp.httpclient ("POST", server:user.tinyUrl.server, path:user.tinyUrl.path, datatype:"application/x-www-form-urlencoded", data:"url="+longUrl, flMessages:false)};
	return (string.trimwhitespace (string.httpResultSplit (s)))};
bundle { //test code
	local (url = "");
	url = "";
	url = "";
	url = "";
	url = "";
	webbrowser.openurl (create (url))}

This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.