Lasso Soft Inc. > Home

[twitter]

Linktwitter
AuthorJason Huck
CategoryUtility
Version8.5.x
LicensePublic Domain
Posted04 Jan 2008
Updated03 Jan 2011
More by this author...

Description

This custom type provides an interface to the Twitter API from Lasso. It requires Fletcher Sandbeck's JSON library for Lasso and the [string_truncate] tag here. In addition to the usage examples below, consult the official documentation for a complete list of supported methods. A few method names were changed slightly to avoid duplicate member tag names. For example, there are several "show" methods, which I renamed to be more specific, such as ->show_user and ->show_status. A couple methods required special formatting for dates, so a [date_toHTTP] tag is included in this download.

Sample Usage

// create a new instance of the twitter type
var('t') = twitter(
	-username='xxxxxxxxx',
	-password='xxxxxxxxx'
);

// get the first item from the public timeline
$t->public_timeline->first;

// get a slice of your friends timeline
$t->friends_timeline( -since=date->subtract( -hour=2)&);

// get 4 items from the user timeline
$t->user_timeline( -count=4);

// show a specific status message
$t->show_status(xxxxxxxxx);

// update your twitter status
$t->update('This Tweet Powered By Lasso.');

// get all replies
$t->replies;

// get a list of your friends
$t->friends;

// get a list of your followers (the -lite option does not appear to work)
$t->followers( -lite=true);

// DEPRECATED: get the current featured twitterers
// $t->featured;

// view all of your direct messages
$t->direct_messages;

// view all of your sent direct messages
$t->sent;

// send a new direct message to a specific user
$t->new( -user='xxxxxxxxx', -text='Sent from Lasso. Let me know if you get this.');

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.

// Twitter API for Lasso
// (c)2007 - 2011 Jason Huck/Core Five Creative
// Thanks to Lieven Gekiere, Brian Loomis, and Rich Fortnum for contributions.
// requires: JSON library, string_truncate, date_toHTTP (below)

define_tag(
	'toHTTP',
	-namespace='date_',
	-req='in', -type='date',
	-priority='replace',
	-encodenone,
	-description='HTTP-formats the given date.'
);
	local('out') = null;
	!#in->gmt ? #in = date_localtogmt(#in);
	#in->setformat('%Q %T');
	#out = encode_url(string(#in))->replace('%20','+')&;
	return(#out);	
/define_tag;


define_type(
	'twitter',
	-description='Implements the Twitter API in Lasso.'
);
	local(
		'username' = string,
		'password' = string,
		'version' = 1
	);
	
	define_tag(
		'oncreate',
		-opt='username', -type='string',
		-opt='password', -type='string',
		-opt='version'
	);
		local_defined('username') ? self->username = #username;
		local_defined('password') ? self->password = #password;
		local_defined('version') ? self->version = #version;
	/define_tag;

	define_tag('authcheck');
		return(self->username != '' && self->password != '');
	/define_tag;
	
	define_tag(
		'retrieve', 
		-req='path', -type='string',
		-opt='post', -type='array',
		-encodenone
	);
		fail_if(!self->authcheck, -1, 'Username or password not set.');
		
		protect;
			local('response') = string(
				local_defined('post') ? include_url(
					'http://api.supertweet.net/' + self->version + #path,
					-username=self->username,
					-password=self->password,
					-postparams=#post,
					-timeout=15
				) | include_url(
					'http://api.supertweet.net/' + self->version + #path,
					-username=self->username,
					-password=self->password,
					-timeout=15
				)
			);
			
			handle_error;
				local('response') = 'There was a problem communicating with Twitter.';
			/handle_error;
		/protect;
		
		return(decode_json(#response));
	/define_tag;
	
	define_tag(
		'public_timeline',
		-opt='since_id', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/public_timeline.json' + (local_defined('since_id') ? '?since_id=' + #since_id);
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'friends_timeline',
		-opt='id',
		-opt='since', -type='date',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/friends_timeline' + (local_defined('id') ? '/' + #id) + '.json';
		local_defined('since') || local_defined('page') ? #path += '?';
		local_defined('since') ? #path += 'since=' + date_toHTTP(#since);
		local_defined('page') ? #path += (!#path->endswith('?') ? '&') + 'page=' + #page;
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'user_timeline',
		-opt='id',
		-opt='count', -type='integer',
		-opt='since', -type='date',
		-encodenone
	);
		local('path') = '/statuses/user_timeline' + (local_defined('id') ? '/' + #id) + '.json';
		local_defined('count') || local_defined('since') ? #path += '?';
		local_defined('count') ? #path += 'count=' + #count;
		local_defined('since') ? #path += (!#path->endswith('?') ? '&') + 'since=' + date_toHTTP(#since);
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'show_status',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/show/' + #id + '.json';
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'update',
		-req='status', -type='string',
		-encodenone
	);
		local('path') = '/statuses/update.json';
		local('post') = array('status' = string_truncate(#status, 160));
		return(self->retrieve(#path, #post));			
	/define_tag;

	define_tag(
		'replies',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/replies.json';
		local_defined('page') ? #path += '?page=' + #page;
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'destroy_status',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/destroy/' + #id + '.json';
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'friends',
		-opt='id',
		-encodenone
	);
		local('path') = '/statuses/friends' + (local_defined('id') ? '/' + #id) + '.json';
		return(self->retrieve(#path));			
	/define_tag;
			
	define_tag(
		'followers',
		-opt='id',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/followers';
		#path += (local_defined('id') ? '/' + #id) + '.json';
		local_defined('page') && #page ? #path += '?page=' + #page;
		return(self->retrieve(#path));
	/define_tag;

	/* deprecated?
	define_tag('featured');
		local('path') = '/statuses/featured.json';
		return(self->retrieve(#path));
	/define_tag;
	*/
	
	define_tag( // not working
		'show_user',
		-opt='id',
		-opt='email', -type='string',
		-encodenone
	);
		local_defined('id') ? local('path') = '/users/show/' + #id + '.json';
		local_defined('email') ? local('path') = '/users/show.json?email=' + #email;
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'direct_messages',
		-opt='since', -type='date',
		-opt='since_id', -type='integer',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/direct_messages.json';
		params->size ? #path += '?';
		local_defined('since') ? #path += 'since=' + date_toHTTP(#since) + '&';
		local_defined('since_id') ? #path += 'since_id=' + #since_id + '&';
		local_defined('page') ? #path += 'page=' + #page;
		#path->removetrailing('&');
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'sent_direct',
		-opt='since', -type='date',
		-opt='since_id', -type='integer',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/direct_messages/sent.json';
		params->size ? #path += '?';
		local_defined('since') ? #path += 'since=' + date_toHTTP(#since) + '&';
		local_defined('since_id') ? #path += 'since_id=' + #since_id + '&';
		local_defined('page') ? #path += 'page=' + #page;
		#path->removetrailing('&');
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'new_direct',
		-req='user',
		-req='text', -type='string',
		-encodenone
	);
		local('path') = '/direct_messages/new.json';
		local('post') = array(
			'user' = #user,
			'text' = string_truncate(#text, -length=140)
		);
		
		return(self->retrieve(#path, #post));
	/define_tag;
	
	define_tag(
		'destroy_direct',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/direct_messages/destroy/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'create_friendship',
		-req='id',
		-encodenone
	);
		local('path') = '/friendships/create/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'destroy_friendship',
		-req='id',
		-encodenone
	);
		local('path') = '/friendships/destroy/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'exists_friendship',
		-req='user_a',
		-req='user_b',
		-encodenone
	);
		local('path') = '/friendships/exists.json?user_a=';
		#path += encode_url(#user_a) + '&user_b=' + encode_url(#user_b);
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'ids_friends',
		-opt='id',
		-encodenone
	);
		local('path') = '/friends/ids';
		local_defined('id') ? #path += '/' + encode_url(#id);
		#path += '.json';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'ids_followers',
		-opt='id',
		-encodenone
	);
		local('path') = '/followers/ids';
		local_defined('id') ? #path += '/' + encode_url(#id);
		#path += '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'verify_credentials',
		-encodenone
	);
		local('path') = '/account/verify_credentials.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'end_session',
		-encodenone
	);
		local('path') = '/account/end_session';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'update_location',
		-req='location', -type='string',
		-encodenone
	);
		local('path') = '/account/update_location.json';
		local('post') = array('location' = string_truncate(#location, 100));
		return(self->retrieve(#path, #post));
	/define_tag;

	define_tag(
		'update_delivery_device',
		-req='device', -type='string',
		-encodenone
	);
		fail_if((: 'sms', 'im', 'none') !>> #device, -1, 'Not an allowed device.');
		local('path') = '/account/update_delivery_device.json?device=' + #device;
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'update_profile_colors',
		-opt='profile_background_color',
		-opt='profile_text_color',
		-opt='profile_link_color',
		-opt='profile_sidebar_fill_color',
		-opt='profile_sidebar_border_color',
		-encodenone
	);
		fail_if(!params->size, -1, 'At least one color must be specified.');
		local('path') = '/account/update_profile_colors.json';
		local('post') = array;
		
		iterate(params, local('i'));
			#post->insert(string(#i->first)->removeleading('-')& = #i->second);
		/iterate;
		
		return(self->retrieve(#path, #post));
	/define_tag;

	/* Not currently implemented: 
		update_profile_image, 
		update_profile_background_image
	*/

	define_tag(
		'rate_limit_status',
		-encodenone
	);
		local('path') = '/account/rate_limit_status.json';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'update_profile',
		-opt='name',
		-opt='email',
		-opt='url',
		-opt='location',
		-opt='description',
		-encodenone
	);
		fail_if(!params->size, -1, 'At least one parameter must be provided.');
		local('path') = '/account/update_profile.json';
		local('post') = array;
		
		iterate(params, local('i'));
			#post->insert(string(#i->first)->removeleading('-')& = #i->second);
		/iterate;
		
		return(self->retrieve(#path, #post));
	/define_tag;
	
	/* deprecated?
	define_tag(
		'archive',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/account/archive.json';
		local_defined('page') ? #path += '?page=' + #page;
		return(self->retrieve(#path));
	/define_tag;
	*/
	
	define_tag(
		'favorites',
		-opt='id',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/favorites' + (local_defined('id') ? '/' + #id) + '.json';
		local_defined('page') ? #path += '?page=' + #page;
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'create_favorite',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/favorites/create/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'destroy_favorite',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/favorites/destroy/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'follow_notification',
		-req='id',
		-encodenone
	);
		local('path') = '/notifications/follow/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'leave_notification',
		-req='id',
		-encodenone
	);
		local('path') = '/notifications/leave/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'create_block',
		-req='id',
		-encodenone
	);
		local('path') = '/blocks/create/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'destroy_block',
		-req='id',
		-encodenone
	);
		local('path') = '/blocks/destroy/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag('test');
		local('path') = '/help/test.json';
		return(self->retrieve(#path));
	/define_tag;
/define_type;

Related Tags

Comments

03 Nov 2011, Wade Maxfield

t.co wrapping?

Please ignore: Testing to see if new t.co wrapping breaks hyperlinks in twitterific

03 Jan 2011, Jason Huck

Updated to reflect API changes.

Updated to include the version number in the API endpoints. Defaults to '1', but can be overridden by passing a value to the -version keyword in the constructor. Thanks to Rich Fortnum for bringing this to my attention.

03 Jan 2011, Jason Huck

Testing API changes (again).

Testing update to include version number in API endpoints.

09 Sep 2010, Jason Huck

Okay, one more try. :)

Testing SuperTweet integration.

09 Sep 2010, Jason Huck

Testing SuperTweet Integration

tagSwap's Twitter notifications should be back up and running now, thanks to SuperTweet!

09 Sep 2010, Jason Huck

SuperTweet Integration

Updated to use the SuperTweet proxy to work around the need for OAuth support. See http://www.supertweet.net/.

24 Feb 2009, Jason Huck

Bug Fixes

Corrected several syntax errors caused by some 4am coding.

24 Feb 2009, Jason Huck

More New Methods

As of this posting, the [twitter] type supports all documented methods of the Twitter API with the exception of ->update_profile_image and ->update_profile_background_image.

24 Feb 2009, Jason Huck

New Methods Added

Two new methods have been added: ->follows_user, contributed by Brian Loomis, and ->update_location, contributed by Lieven Gekiere.

14 May 2008, Lieven Gekiere

Twitter API updated

The last few weeks the Twitter API has received a few updates....

It is now possible to add your location to your profile... Just add the following lines to the Twitter custom type.


define_tag(
'update_location',
-req='location', -type='string',
-encodenone
);
local('path') = '/account/update_location.json';
local('post') = array('location' = string_truncate(#location, 100));
return(self->retrieve(#path, #post));
/define_tag;


And post to Twitter via :

$t->update_location('My location in the world');


02 Feb 2008, Jason Huck

Follow tagSwap on Twitter

Thanks to this custom type, you can now receive updates about tagSwap.net via Twitter. Follow tagSwap to get instant notifications whenever tags are added or edited, or when comments are posted. (http://twitter.com/tagSwap)

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

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