Lasso Soft Inc. > Home

[rss]

Linkrss
AuthorJason Huck
CategoryData Type
Version8.x
LicensePublic Domain
Posted24 Apr 2006
Updated26 Mar 2007
More by this author...

Description

This is a basic type for working with RSS feeds. The ->parse method is taken from this TOTW article. Currently designed to produce feeds conforming to RSS 0.91, but could easily be adapted to work with the newer spec. When you create a new object using this type, you can either provide it with the URL of a remote feed, or supply it with an array of arrays containing title, link, and description. (It's designed to be easily populated using [records_array].) The specific members are:

->title - The title of the feed.

->link - Link back to the feed you are creating.

->description - A description of the feed.

->language - The language of the feed. Defaults to US English.

->image - A map containing information about the image used to represent the feed, including title, url, link, width, and height.

->rows - The feed data; an array of arrays like that produced by [records_array].

->url - Any valid feed URL. If supplied when the object is created or as an argument to the ->parse method, will populate the type with the contents of the given feed.
->setImage - Used internally to populate the ->image member var.

->setRows - Used internally to populate the ->rows member var.

->getOutput - Returns the feed data in RSS format.

->parse - Populates the object with data from a given remote feed.

->serve - Serves the feed using [file_serve].

Sample Usage

[
// [rss] Usage Example
// -------------------------------------------------------------------------	
// Save this code as rss_example.lasso in the same folder within your web 
// root as the rss.inc file you can download from tagSwap.net here:
// http://tagswap.net/rss


// This will cause a prompt asking for your siteadmin username and password
// so that you can access Lasso's internal database via the inline below.
// This is just to make the example run with less configuration.
auth_admin;


// This loads the custom type used below. Alternatively, just place the file
// in LassoStartup and restart Lasso, and you'll be able to call it from any
// Lasso page just like the built-in tags.
library('rss.inc');


// This constructs an SQL query to return the first 10 regular tags from the
// local Lasso reference. I'm constructing the link back to the reference
// right in the query itself so I don't have to do any further manipulation 
// of the data.
var('query' = '
	SELECT
		tag_name AS title, 
		(\'http://reference.lassosoft.com/Reference.LassoApp?\' || tag_name) AS link, 
		tag_description AS description 
	FROM tags
	WHERE tag_name LIKE \'[%]\'
	LIMIT 10
');


// We retrieve the data requested in the query above using a regular inline.
// We don't have to specify username or password because of the auth_admin
// call above.
inline(
	// -username='xxxxxx',			// Use these instead of [auth_admin]
	// -password='xxxxxx',			// when working with your own data.
	-database='LDML8_Reference',
	-sql=$query
);
	// Here we're creating a new "instance" of the RSS type, specifying the
	// title, link, and description for the overall feed. We use the path to
	// the current file as the link for the feed, and shove the entire result
	// set from our inline into the -rows param using the tag records_array.
	var('myFeed') = rss(
		-title='Lasso Tags',
		-link=('http://' + server_name + response_filepath),
		-description='The first 10 Lasso tags in the reference.',
		-rows=records_array
	);
/inline;


// Finally, we serve the feed using the RSS type's member tag, ->serve.
// This is no different than using the array->get or map->find.
$myFeed->serve;
]

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(
		'rss',
		-priority='replace',
		-description='A basic RSS type.'
	);
		local(
			'url' = string,
			'channel' = map(
				'title' = string,
				'link' = string,
				'description' = string,
				'language' = 'en-us'
			),
			'image' = map(
				'title' = string,
				'url' = string,
				'link' = string,
				'width' = string,
				'height' = string
			),
			'items' = array
		);

		
		define_tag(
			'onCreate',
			-optional='url',
			-type='string',
			-optional='title',
			-type='string',
			-optional='link',
			-type='string',
			-optional='description',
			-type='string',
			-optional='language',
			-type='string',
			-optional='image',
			-type='map',
			-optional='rows',
			-type='array'
		);
			if(local_defined('url'));
				self->parse(#url);
			else;
				local_defined('title') ? self->channel->find('title') = encode_xml(#title);
				local_defined('link') ? self->channel->find('link') = encode_xml(#link);
				local_defined('description') ? self->channel->find('description') = encode_xml(#description);			
				local_defined('language') ? self->channel->find('language') = encode_xml(#language);			
				local_defined('image') ? self->setImage(#image);
				local_defined('rows') ? self->setRows(#rows);
			/if;
		/define_tag;

		
		define_tag('setImage');
			local('image' = params->first);
		
			iterate(self->image->keys, local('i'));
				self->image->find(#i) = encode_xml(#image->find(#i));
			/iterate;
		/define_tag;

		
		define_tag('setRows');
			local('rows' = params->first);
		
			iterate(#rows, local('r'));
				local('row' = map);
				#row->insert('title' = encode_xml(#r->get(1)));
				#row->insert('link' = encode_xml(#r->get(2)));
				#row->insert('description' = encode_xml(#r->get(3)));
				self->items->insert(#row);
			/iterate;
		/define_tag;

		
		define_tag('getOutput');
			local('out' = '\



	
		Lasso Programming: ' + self->channel->find('title') + '
		' + self->channel->find('link') + '
		' + self->channel->find('description') + '
		' + self->channel->find('language') + '
			');
			
			if(self->image->find('url') != '');
				#out += '
		
			' + (self->image->find('title') != '' ? 'Lasso Programming: ' + self->image->find('title') + '') + '
			' + (self->image->find('url') != '' ? '' + self->image->find('url') + '') + '
			' + (self->image->find('link') != '' ? '' + self->image->find('link') + '') + '
			' + (self->image->find('width') != '' ? '' + self->image->find('width') + '') + '
			' + (self->image->find('height') != '' ? '' + self->image->find('height') + '') + '
		
				';
			/if;
			
			iterate(self->items, local('i'));
				#out += '\
		
			Lasso Programming: ' + #i->find('title') + '
			' + #i->find('link') + '
			' + encode_xml(#i->find('description')) + '
		
				';
			/iterate;
			
			#out += '\
  

			';
			
			return(@#out);
		/define_tag;


		define_tag('parse');
			fail_if(
				!params->first || !valid_url(params->first), 
				-1, 
				'A valid url must be supplied.'
			);
			
			local('url' = params->first);			
			self->url = #url;
			local('data') = xml(include_url(#url));
			
			iterate(#data->extractone('channel')->children, local('node'));
 				if(#node->name == 'item');
					local('item' = map);
					
					iterate(#node->children, local('subnode'));
						(#subnode->name != 'text') ? #item->insert(#subnode->name = #subnode->contents);
					/iterate;
					
					self->items->insert(#item);				
				else(#node->name == 'image');
					iterate(#node->children, local('subnode'));
						(#subnode->name != 'text') ? self->image->insert(#subnode->name = #subnode->contents);
					/iterate;		
				else(#node->name != 'text');
					self->channel->insert(#node->name = #node->contents);
 				/if;
 			/iterate;
		/define_tag;

		
		define_tag('serve');			
			file_serve(
				self->getOutput,
				-type='text/xml; charset=utf-8'
			);
		/define_tag;
	/define_type;

Related Tags

Comments

No comments

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