Lasso Soft Inc. > Home

[bitly]

Linkbitly
AuthorJason Huck
CategoryUtility
Version8.5.x
LicensePublic Domain
Posted01 Jul 2009
Updated01 Jul 2009
More by this author...

Description

This is a simple wrapper for the bit.ly URL shortener. Bit.ly offers tracking and stats on shortened URLs. Requires a bit.ly account and API key. Use the method name as a member tag and the method's arguments as keywords to the tag. Be sure to match the case shown in the documentation as the API is case-sensitive. Requires [decode_json] or [xml_tree] depending on the output format specified.

Sample Usage

// NOTE: parameters are case sensitive! That means -apiKey will work but -apikey won't!
var('bit') = bitly(
	-login='xxxxxxx',
	-apiKey='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);

// shorten a URL
$bit->shorten( -longUrl='http://code.google.com/p/bitly-api/wiki/ApiDocumentation');

// undocumented way to create custom short urls, i.e. this would create http://bit.ly/myshorturl
$bit->shorten( -longUrl='http://code.google.com/p/bitly-api/wiki/ApiDocumentation', -keyword='myshorturl');

// expand a short URL, takes either the full URL or just the hash
$bit->expand( -hash='r3lsS');
$bit->expand( -shortUrl='http://bit.ly/r3lsS');

// get info on a short URL
$bit->info( -hash='r3lsS');

// use a comma separated list of keys to limit the info returned
$bit->info( -shortUrl='http://bit.ly/r3lsS', -keys='thumbnail');

// get stats on a short URL
$bit->stats( -hash='r3lsS');
$bit->stats( -shortUrl='http://bit.ly/r3lsS');

// show a list of error messages and codes
$bit->errors;

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_type(
	'bitly',
	-prototype,
	-description='Wrapper for the bit.ly URL shortener API.'
);
	local(
		'login' = string,
		'apiKey' = string,
		'version' = '2.0.1',
		'format' = 'json', // options: json, xml, text
		'history' = 1
	);
	
	define_tag(
		'oncreate',
		-req='login',
		-req='apiKey',
		-opt='format',
		-opt='history'
	);
		self->'login' = #login;
		self->'apiKey' = #apiKey;
		local_defined('format') ? self->'format' = #format;
		local_defined('history') ? self->'history' = #history;
	/define_tag;
	
	define_tag(
		'send',
		-req='method',
		-req='params', -type='array',
		-encodenone
	);
		local('getparams') = array(
			'login' = self->'login',
			'apiKey' = self->'apiKey',
			'version' = self->'version',
			'format' = self->'format',
			'history' = self->'history'
		);
		
		#getparams->merge(#params);			
		local('response') = string;
		
		protect;
			#response = string(include_url(
				'http://api.bit.ly/' + #method,
				-getparams=#getparams,
				-connecttimeout=15,
				-timeout=15
			));
			
			handle_error;
				return(map('errorCode' = error_code, 'errorMessage' = error_msg));
			/handle_error;
		/protect;
		
		if(self->'format' == 'json');
			#response = decode_json(#response);
		else;
			#response = xml_tree(#response);
		/if;
		
		return(#response);
	/define_tag;
	
	define_tag('_unknowntag');
		local('params') = array;
		
		iterate(params, local('i'));
			#params->insert(string(#i->first)->removeleading('-')& = #i->second);
		/iterate;
	
		return(self->send(tag_name, #params));
	/define_tag;
/define_type;

Related Tags

Comments

No comments

Please log in to comment

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft