You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

Writing an RSS Aggregator Part 1

Building an application

So often in this column I feel that I'm writing about basic concepts and using trivial examples. It's often up to you, as the reader, to figure out how to apply these concepts to your development. I thought it might be a good idea to take some space to try to bring a lot of the concepts together and build an application. I don't always take the space to do so, but thought I'd give it a try here.

I'm sure that most of you read (or at least have heard of) blogs. Blogs are a great way to keep up on the up-to-the-minute comings and going of whatever community you choose. There are tons of bloggers throughout the ColdFusion community, some blogging from inside Adobe. Many blogs provide an RSS feed for content. RSS stands for Really Simple Syndication and is an XML dialect. There are a handful of RSS aggregators in the ColdFusion community such as mxna (http://weblogs.macromedia.com/mxna/), Full As a Goog (www.fullasagoog.com/), and Feed Squirrel (www.feed-squirrel.com/). I've always thought it would be cool to create something like a LiveJournal friends list of my very own. In this series of articles, I'm going to step you through the thought process that I'd go through to create one. First I'll concentrate on designing the database and fleshing out the ColdFusion components.

Designing the Back End
When developing an application, I normally start with the database design, and that is where we'll start here. If you need a refresher on database design techniques, you can review the database design article from this column at http://coldfusion.sys-con.com/read/49177.htm. What data do you want to store in an RSS Aggregator application? You can read all about the RSS 2.0 spec at www.rssboard.org/rss-specification. For purposes of this article, I'm only going to concentrate on required fields.

  • RSS Feeds: The RSS feed seems like a good place to start. The app will have to store the name of the RSS feed and the URL location of the feed.
  • Items: Since we're aggregating feed data from a lot of different sources, we'll need to store the posts from each individual feed. It'll be more efficient to store them locally than to create the feed manually each time we want to display something. A post is put into the RSS XML document as an item. According to the spec, nothing is required in the item except for title or description; there's no guarantee that one will be there. For the sake of this article, we'll assume that title, link, and description will be there. Items have a one-to-many relationship with RSS feeds. Each blog can contain many posts, but each post belongs to only one blog. To implement this in the database, we put a primary key for the RSS feed into the items table.
  • Categories: Within the app, I want to be able to categorize each feed, so I can filter the group of messages that I'm reading. We'll add a category table to the database. This data won't be coming from the RSS, but some identifier that we create.
The database structure is shown in Figure 1.

For the model portion of this app, I'm going to use ColdFusion components. You can read more about component basics in two of my articles at http://coldfusion.sys-con.com/read/47203.htm and http://coldfusion.sys-con.com/read/47446.htm. I'd create one CFC for each element: an RSS Feed, an Item, and a Category. You can see the model in Figure 2. You may notice that the CFCs are named in the singular tense, while the database tables are plural. This is because each CFC is designed to represent a single item, while the database tables contain many items. I left out individual getter and setter methods from the diagram, but the code will use the generic getter and setter methods from Hal Helm's BaseComponent.cfc http://halhelms.com/webresources/BaseComponent.cfc. Each component includes an init and a commit method. The init will load information out of the database, while the commit will create (or update) information in the database.

The RSSFeed and RSSCategory components will be used for maintaining feeds and categories. You might notice that the RSSFeed component contains an RSSCategory component instead of the CategoryID foreign key. I thought about putting an array of item objects in the RSSFeed, but decided against it. The application won't need to load all the items from the feed in question; a gateway object can be used to handle this type of action when viewing the data (although that's beyond the scope of this article). The RSSaggregator component is where most of the magic takes place. It will be the background process, run as part of a scheduled task. It will loop through each feed in the database, collect the latest data, and store the new items in the database. It will use the item component.

Creating the RSS Feed Components
In real-world applications, I often like to start by building the administrator first. That way I don't have to worry about filling the tables with fake data to test the public-facing part of the site. Before we can start to process feeds on the back-end, we have to be able to enter the feeds into the database. We can start by fleshing out the RSSFeed and RSSCategory components. First, let's examine the RSSCategory component, as shown in Listing 1. Nothing in the RSSCategory.cfc should surprise you. It contains two instance variables, the CategoryID and Category. The CategoryID is initialized as a string because this app uses UUIDs as primary keys instead of auto-incrementing integers. The component has two methods (plus a few inherited from Hal's BaseComponent.cfc). There's an init method that accepts the CategoryID and the datasource. It queries the database and sets the instance variables, if applicable. The second method is a commit method. It accepts the datasource name as an argument. If the CategoryID is blank, a new category is created. If it isn't, an update is performed. This isn't much different, in concept, from the address.cfc I wrote about in one of my previous component articles.

The RSSFeed component is shown in Listing 2. You'll notice many similarities to the RSSCategory component. There are more properties in RSSFeed than RSSCategory, but the concept is the same. You can notice that the category property of the component is an instance of the RSSCategory component. Instead of containing the category information directly, only the reference is stored. To access the category information, we can use the get, or set, methods off the component instance. The set methods are demonstrated in init; while the get methods are demonstrated in commit.

Entering the Data
To enter the data, a simple HTML form is used, as shown in Listing 3. The first lines include a syslib.cfm file. This is just a utility library of functions. Two functions from the library are used in this component. The GetCategories function contains a query to get all categories. The "CreateSelectList" function is very similar to the cfselect tag for those who don't use cfform. (I'll make sure that all the code behind this article gets up on my blog; I left out the syslib.cfm for brevity).

The file creates an instance of the RSSFeed component. If an RSSFeedID is defined, it will run the init method; otherwise the base component is used. A form is created that asks for the category, and the http link of the RSS feed. The title and description are displayed on this form, but are actually populated from the RSS feed, so they're not editable.

The form submits onto Feedip.cfm, which is shown in Listing 4. The initialization code is the same as in Listing 3. Then the code enters a try block. To get the description and title, we need to call the RSS feed. If the cfhttp call times out, we know that the RSS feed is wrong. The URL could be invalid in other ways, but that's the only thing we check for in this code. XMLParse is called on cfhttp.filecontent. I wrote an article about getting and processing XML feeds this way, and you can read it at http://coldfusion.sys-con.com/read/117667.htm. The remainder of the code sets the instance variables and calls the commit. Congratulations, you've saved a feed in the database.

What's Next
I've just about run out of space for this article, but this app is far from complete. In the next article I'll talk about the RSSAggregator and item componentS. It will create the scheduled task to call the aggregation code. Space permitting, I'll go into a gateway component along with some code that can be used to look at the aggregated data. If there isn't space in the next article for that, I'll fit it into some future article. Until next time, keep coding.

More Stories By Jeffry Houser

Jeffry is a technical entrepreneur with over 10 years of making the web work for you. Lately Jeffry has been cooped up in his cave building the first in a line of easy to use interface components for Flex Developers at www.flextras.com . He has a Computer Science degree from the days before business met the Internet and owns DotComIt, an Adobe Solutions Partner specializing in Rich Internet Applications. Jeffry is an Adobe Community Expert and produces The Flex Show, a podcast that includes expert interviews and screencast tutorials. Jeffry is also co-manager of the Hartford CT Adobe User Group, author of three ColdFusion books and over 30 articles, and has spoken at various events all over the US. In his spare time he is a musician, old school adventure game aficionado, and recording engineer. He also owns a Wii. You can read his blog at www.jeffryhouser.com, check out his podcast at www.theflexshow.com or check out his company at www.dot-com-it.com.

Comments (1)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the competition, or worse, just keep up. Each new opportunity, whether embracing machine learning, IoT, or a cloud migration, seems to bring new development, deployment, and management models. The results are more diverse and federated computing models than any time in our history.
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...