[rss]

Description

Link: [rss]
Author: Jason Huck
Category: Data Type
Version: 8.x
License: Public Domain
Posted: Apr. 24, 2006
Updated: Mar. 26, 2007
More by this author...
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].

Parameters

none


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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' = '\
<?xml version="1.0"?>
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
<rss version="0.91">
	<channel>
		<title>' + self->channel->find('title') + '</title>
		<link>' + self->channel->find('link') + '</link>
		<description>' + self->channel->find('description') + '</description>
		<language>' + self->channel->find('language') + '</language>
			');
			
			if(self->image->find('url') != '');
				#out += '
		<image>
			' + (self->image->find('title') != '' ? '<title>' + self->image->find('title') + '</title>') + '
			' + (self->image->find('url') != '' ? '<url>' + self->image->find('url') + '</url>') + '
			' + (self->image->find('link') != '' ? '<link>' + self->image->find('link') + '</link>') + '
			' + (self->image->find('width') != '' ? '<width>' + self->image->find('width') + '</width>') + '
			' + (self->image->find('height') != '' ? '<height>' + self->image->find('height') + '</height>') + '
		</image>
				';
			/if;
			
			iterate(self->items, local('i'));
				#out += '\
		<item>
			<title>' + #i->find('title') + '</title>
			<link>' + #i->find('link') + '</link>
			<description>' + encode_xml(#i->find('description')) + '</description>
		</item>
				';
			/iterate;
			
			#out += '\
  </channel>
</rss>
			';
			
			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;

 

Comments

none

Email:


Password:



Newest

Most Popular