0

In 30: Yahoo Pipes in ColdFusion

In 30

I like to spend some time writing proof of concept (POC) code. If such an experiment works I will often use it in an application and write it properly. Like many I have been impressed with the Yahoo Pipes. At a basic level Yahoo Pipes puts together two or more RSS feeds. Here I'll show how to do the backend in CF.

Ok first I made a pipe for myself of Ben Forta and Ray Camden's blog feeds which you can see here.

So, CF 7 does not handle RSS feeds natively but fortunately Ray Camden (does the man ever sleep? :)) has written a RSS.cfc that will read and write feeds. So with that downloaded I created an array of feeds.

<cfset variables.arFeeds = ArrayNew(1)>
<cfset arrayAppend(variables.arFeeds,"http://feeds.feedburner.com/RaymondCamdensColdfusionBlog")>
<cfset arrayAppend(variables.arFeeds,"http://www.forta.com/blog/rss.cfm?mode=short")>

Next lets create an object of RSS and then loop over that array and put the results of the feed in an array.

 

<cfset variables.rss = createObject("component","rss")>
<cfset variables.arEntries = ArrayNew(1)>
<cfloop from="1" to="#arrayLen(variables.arFeeds)#" index="variables.i">
	<cfset arrayAppend(variables.arEntries,variables.rss.getEntries(variables.arFeeds[variables.i]))>
</cfloop>

<cfdump var="#variables.arEntries#" expand="false">

So now we have an array with a query of feeds but we really want to make one query. Time to step up Query-of-Queries! And the UNION operator.  Though one limitation is that Q-o-Q's need to refer to a query that does not have a dot in it. D'oh. However, the code below will get around that by setting a variable to the array with the query. Not fully sustainable but this is a POC.

<cfquery name="pipeSelect" dbtype="query">
<cfloop from="1" to="#arrayLen(variables.arFeeds)#" index="variables.i">
	<cfswitch expression="#variables.i#">
		<cfcase value="1">
		<cfset qry1 = arEntries[variables.i]>
		SELECT qry1.title,
			qry1.[date],
			qry1.description,
			qry1.link
		FROM qry1
		</cfcase>
		<cfcase value="2">
		<cfset qry2 = arEntries[variables.i]>
		SELECT qry2.title,
			qry2.[date],
			qry2.description,
			qry2.link
		FROM qry2
		</cfcase>
	</cfswitch>
	<cfif variables.i neq arrayLen(variables.arFeeds)>
	UNION
	</cfif>
</cfloop>
	ORDER BY [date] desc
</cfquery>

Not the qry1.[date] notation to get around reserved words in Query-of-Queries.

Now a little outputting:

<cfoutput query="pipeSelect">
<p style="font-family: Verdana;"><a href="#pipeSelect.link#">#pipeSelect.title#</a>
 on #dateFormat(pipeSelect.date,"short")# #timeFormat(pipeSelect.date,"short")#<br />
#pipeSelect.description#</p>
</cfoutput>

And the concept has been proved!

Full code:

<cfset variables.arFeeds = ArrayNew(1)>
<cfset arrayAppend(variables.arFeeds,"http://feeds.feedburner.com/RaymondCamdensColdfusionBlog")>
<cfset arrayAppend(variables.arFeeds,"http://www.forta.com/blog/rss.cfm?mode=short")>

<cfset variables.rss = createObject("component","rss")>
<cfset variables.arEntries = ArrayNew(1)>
<cfloop from="1" to="#arrayLen(variables.arFeeds)#" index="variables.i">
	<cfset arrayAppend(variables.arEntries,variables.rss.getEntries(variables.arFeeds[variables.i]))>
</cfloop>

<cfdump var="#variables.arEntries#" expand="false">

<cfquery name="pipeSelect" dbtype="query">
<cfloop from="1" to="#arrayLen(variables.arFeeds)#" index="variables.i">
	<cfswitch expression="#variables.i#">
		<cfcase value="1">
		<cfset qry1 = arEntries[variables.i]>
		SELECT qry1.title,
			qry1.[date],
			qry1.description,
			qry1.link
		FROM qry1
		</cfcase>
		<cfcase value="2">
		<cfset qry2 = arEntries[variables.i]>
		SELECT qry2.title,
			qry2.[date],
			qry2.description,
			qry2.link
		FROM qry2
		</cfcase>
	</cfswitch>
	<cfif variables.i neq arrayLen(variables.arFeeds)>
	UNION
	</cfif>
</cfloop>
	ORDER BY [date] desc
</cfquery>
<cfoutput query="pipeSelect">
<p style="font-family: Verdana;"><a href="#pipeSelect.link#">#pipeSelect.title#</a>
 on #dateFormat(pipeSelect.date,"short")# #timeFormat(pipeSelect.date,"short")#<br />
#pipeSelect.description#</p>
</cfoutput>

tags:
In 30
Matthew Reinbold said:
 
You mention that copying the queries to a variable for use in a QofQ is only workable as a Proof of Concept. Considering that what I was trying to do before - a set of loops - was quite a bit more complicated this seems like an elegant solution.

If this wasn't POC how would you avoid copying to a separate variable?
 
posted 1069 days ago
Add Comment Reply to: this comment OR this thread
 
 
Matthew -- It was the first solition I thought of and it seemed an ugly way to do it with the switch and case statements. However, thinking about it more I can't think of an easier solution so I say go for it!
 
posted 1069 days ago
Add Comment Reply to: this comment OR this thread
 
Tighe K. Lory said:
 
I am in the process of using your Yahoo Pipes Example in Coldfusion, and have come up with a way of not using a case statement.

Just replace your cfquery with this and it will dynamically create the variables, and will also allow the addition of more feeds without adding cases:




SELECT qry#variables.i#.title,
qry#variables.i#.[date],
qry#variables.i#.description,
qry#variables.i#.link
FROM qry#variables.i#

UNION


ORDER BY [date] desc

 
posted 813 days ago
Add Comment Reply to: this comment OR this thread
 
Tighe K. Lory said:
 
Woops, it stripped out the CFML! I hope this works!

<cfquery name="pipeSelect" dbtype="query">
<cfloop from="1" to="#arrayLen(variables.arFeeds)#" index="variables.i">
   <cfset "qry#variables.i#" = arEntries[variables.i]>
   SELECT qry#variables.i#.title,
    qry#variables.i#.[date],
    qry#variables.i#.description,
    qry#variables.i#.link
   FROM qry#variables.i#
<cfif variables.i neq arrayLen(variables.arFeeds)>
UNION
</cfif>
</cfloop>
ORDER BY [date] desc
</cfquery>
 
posted 813 days ago
Add Comment Reply to: this comment OR this thread
 
 
@Tighe: Very nice!
 
posted 812 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Twitter
You should follow me on Twitter here
About Me
I am a 34-year old Web Developer specializing in ColdFusion. I live and work in downtown Washington, DC with my wife and two daughters. Read more About Me

2007 CFeMmy Best Newcommer winner
As voted on by fellow CF Bloggers.