[ By Tom Hearn, Developer]
RSSBus is about accessing and customizing your data, your way, easily. You can use any of the hundreds of connectors that we have to connect to various systems or create your own to access systems that are proprietary to you. You can use the RSSBus Admin Console to easily configure and publish RSS with data extensions or use RSBScript for even more control. You can automatically serve your feed in any of the supported formats (ATOM, RSS, HTML, JSON, XLS) by simply changing an input parameter or format the feed your way using a custom feed formatter.
Custom feed formatters change how an entire feed is formatted upon execution of a script. As items are pushed from a script, they are passed to the chosen feed formatter, and the results are sent to the client.
Consider the following scenario:
You work at a construction company, and each month you compile sales reports for 10 different employees. Each of these employees stores their sales data in a different place: Excel, SQL Server, MySQL, and so forth. Your boss does not know how to access all of these different technologies, and more importantly, does not want or need to know how. He wants to see all of this data, each month, problem-free, in CSV format. RSSBus to the rescue.
With RSSBus, you configure a feed using the connectors already provided in RSSBus for Excel, SQL Server, and MySQL to access the data from those systems. Then you create a custom CSV feed formatter to format those feeds as CSV, and finally, send your boss the URL. All the other pieces are already available, so let's get started with writing a custom CSV formatter.
All RSSBus feed formatters need to implement the interface RSBFeedFormatter.
public interface RSBFeedFormatter
{
string FeedStart(RSBItem metaItem, string[] param);
string FormatItem(RSBItem item);
string FeedEnd();
string Info();
}
These methods are fairly straightforward, but a quick explanation might help.
- Info() describes the formatter and is invoked when meta-data is requested about the formatter.
- FeedStart() is invoked before iterating through the feed items and is used to start the feed.
- FormatItem() is invoked for each item in the feed and is used to format each item.
- FeedEnd() is invoked after all of the items have been formatted and is used to finalize the feed.
In our example, the primary underlying functionality of our class is contained in FeedStart() and FormatItem().
FeedStart() will retrieve the current HttpContext, set the ContentType to Excel so the users will not view the data as a normal webpage, and then set the Content-Disposition header so the file is automatically downloaded when a user visits the URL.
public string FeedStart(RSBItem metaItem, string[] param)
{
// Set the Content-type so the browser knows it isn't HTML
System.Web.HttpContext http = System.Web.HttpContext.Current;
http.Response.ContentType = "Content-type: application/vnd.ms-excel";
http.Response.AddHeader("Content-Disposition", "inline; filename=" +
metaItem.GetAttr("rss:title", 1) + ".csv");
return "";
}
FormatItem() will loop through each of the items in the feed and process them as a csv file would be formatted. FormatItem() will first go through the initial row to retrieve the actual attribute names before any items are processed, to place at the top of our csv file. Next, all of the items in the feed will be processed and added to our output.
public string FormatItem(RSBItem item) {
processFirstItem ...
for (int i = 0; i < attrnames.Length; i++) {
string val = item.GetAttr(attrnames[i]);
if (val == null) val = "";
else val = val.Replace("\n", "").Replace("\r", "");
text.Append("\"" + val + "\"");
if (i < attrnames.Length - 1) text.Append(",");
}
text.Append("\r\n");
return text.ToString();
}
That concludes our task. Simply compile the formatter and place the resulting assembly in the bin folder of your RSSBus installation. The formatter will automatically be detected and you can now use it by specifying the format tag at the end of a url, such as http://www.mywebsite.com/rssbus/examplescript.rsb?@format=csv.
For the complete code listing, download the zip file below. It includes a project that can be used with either RSSBus Feed Server or RSSBus Desktop.
Downloads:
CSVFormatter.zip