Kevin Nelson Marshall
Other entries:
« A little XML-RPC Factor style ...

Hopefully you'll remember our messy example from yesterday (here it is again just incase you don't):

{ H{ { "api_key" "YOUR_API_KEY_GOES_HERE" } { "per_page" 1 } } } "flickr.interestingness.getList" "http://www.flickr.com/services/xmlrpc/" invoke-method rpc-response-params 0 over nth nip string>xml "photo" tag-named* "http://farm" swap "farm" over at swap ".static.flickr.com/" swap "server" over at swap "/" swap "id" over at swap "_" swap "secret" over at swap ".jpg" swap drop append append append append append append append append



While this code actually works, it's near impossible to understand (unless you're a true genius like me!).

In reality this code is not doing that much, but there's a lot of flip flopping of things with the data stack, and trying to do it all in one line like this becomes something of a master SUDOKU puzzle. Sure it works, but good luck trying to debug it or expand it down the road!

So now let's take a look at my factored out version. Since we are going to define a number of words that we might want to reuse down the road, we'll now save the code in a file called "flickr.factor" (I'm putting mine directly in my Factor folder, but you can save your to whatever location you prefer).

USING: arrays kernel sequences xml xml.utilities xml-rpc namespaces assocs ;

: getsecret ( tag -- string )
"secret" over at swap drop ;

: getid ( tag -- string )
"id" over at swap drop ;

: getserver ( tag -- string )
"server" over at swap drop ;

: getfarm ( tag -- string )
"farm" over at swap drop ;

: buildurl ( tag -- string )
"http://farm" swap dup getfarm swap >r append r>
".static.flickr.com/" swap dup getserver swap >r append r>
"/" swap dup getid swap >r append r>
"_" swap getsecret append
".jpg" append 4array concat ;

: getphoto ( xml -- tag )
"photo" tag-named* ;

: interestingness-getlist ( per_page api_key -- string )
[ "api_key" set "api_key" get drop "per_page" set "per_page" get drop ]
H{ } make-assoc 1array
"flickr.interestingness.getList" "http://www.flickr.com/services/xmlrpc/" invoke-method
rpc-response-params 0 over nth nip string>xml ;



Once we've got that file created, we can now go back into our Listener, load it, and then use it with the following commands (keep in mind my path is where I saved flickr.factor, yours might be different - and yes today I'm working on a MAC):

"/Applications/Factor/flickr.factor" run-file
1 "MY_API_KEY" interestingness-getlist getphoto buildurl



Assuming you copied everything correctly (and you used a valid API_KEY) you should now have a URL on your stack that you can copy/past into a browser (just like our initial messy example!).

Best of all, this new version is a lot more simple to understand all the steps that are going on (and therefore it will be easier to maintain, upgrade, and reuse down the road if need be). Sure it was a little more typing, but the benefits of being readable and usable GREATLY outweighs the extra few keystrokes it took!

So now that it's a little easier to follow, let me go ahead and break down some of the more interesting things that are happening here.

1. The actual xml-rpc call to the flickr service is done in the interestingness-getlist word. Here we take our per_page and api_key values off the data stack and create an array which contains a Hash. Once we've built up the proper parameter details (in the proper structure) we use them along with the method and URL to invoke the XML-RPC service. Doing this returns a Tuple to us, hopefully with a rpc-response-params value (I need to update this to deal with rpc-faults). Then we simply pull out the xml from that Tuple as a string and convert it to proper xml.

2. We use the getphoto word to simply pull out the first child photo tag and we return it as an XML tag (if we wanted to get more than one photo at a time we could simply step through our rpc-response-params calling this word as many times as we needed)

3. Finally we call the buildurl word which simply pushes strings onto the datastack and calls various attributes of the photo tag as needed. In the end, we append all the strings on the stack into one larger string and we have our URL.

Simple, simple, right? OK it's true, I'm not doing anything too complex here and really getting the URL of an 'interesting' photo is next to worthless by itself. Still, as a small example it really seems to dive into a lot of the more subtle tips and tricks of working in Factor. I hope it reveals some of the core ideas of 'thinking in Factor', at least from a newbies point of view...

But I'll leave it to you to let me know what you think (and what we can do next to improve on this as a group of newbies)!

posted by Kevin Marshall on 2008-01-18 00:00:00+00

Subscribe »

BotFu feed with RSS reader

BotFu feed by Email


Search All Posts »

Blog Details »

This blog now includes 286 wonderfully exciting posts from 1 unique and very special writer!


Kevin Marshall - Who's That?

I'm just your basic programmer. I can't spell to save my life, I'm not the greatest story teller, and I often ramble on about nothing. This blog showcases all of that!

If you're bored drop me an email at info at falicon.com or view my outdated resume.


Stalk me on »

Twitter (@falicon) »
Delicious »
Digg »
Disqus »
Facebook »
Flickr »
FriendFeed »
Last.fm »
LinkedIn »
StumbleUpon »

Archives by Category »

(24) Code »
(5) ColdFusion »
(11) Database »
(7) Factor »
(286) General »
(9) JavaScript »
(15) Perl »
(13) PHP »
(17) Ruby »

Archives by Month »

(1) February 2010 »
(5) January 2010 »
(2) October 2009 »
(6) August 2009 »
(11) July 2009 »
(2) May 2009 »
(3) April 2009 »
(2) March 2009 »
(7) February 2009 »
(9) January 2009 »
(14) December 2008 »
(5) November 2008 »
(12) October 2008 »
(13) September 2008 »
(16) August 2008 »
(23) July 2008 »
(20) June 2008 »
(24) May 2008 »
(23) April 2008 »
(27) March 2008 »
(28) February 2008 »
(26) January 2008 »
(7) December 2007 »

Published Works »

Beginning Amazon's SimpleDB (Apress in dev.)
Pro Active Record (Apress 2007)
Web Services with Rails (O'Reilly 2006).

Contributed To »

Ruby Cookbook (O'Reilly 2006)
SQL Cookbook (O'Reilly 2005)
Various Reviews published in Computing Reviews

Free Code I've Created »

SimpleDB library in Python 3.0

Fantasy focused domains »

draftwizard.com
fantasy-football-draft.com
fantasyfootballkit.com
fantasyfootballquiz.com
hockeynotes.com
pegg.it
rosterhelp.com
sportsxml.com
statsfeed.com
supermug.com

Tech. focused domains »

factorcode.com
perlquiz.com
simpledb.info

Social Tool focused domains »

conversationlist.com
friendstat.us
fuzzypop.com
gawk.it
grou.pe
halfbite.com
jivegas.com
pu.ly
tagli.st
timelylinks.com
tym.ly
wow.ly

Utility focused domains »

fubnub.com

Other domains »

betaread.com
botfu.com
falicon.com
storyrank.com

Not yet live domains »

bar.ackoba.ma
basketballnotes.com
buddydirt.com
budrank.com
cakntoba.com
coachwizard.com
cointhief.com
ezbcs.com
falconsrule.com
fantasydeke.com
fantasyfootballrank.com
ffkit.com
footballnotes.com
footballpublishing.com
giggletweet.com
greentile.com
herobrawl.com
kacode.com
kickasscode.com
knowabout.it
leaguewizard.com
nfldraftnews.com
pa.ly
rorbe.com
slidepitch.com
startfail.com
survivorhub.com
tagli.st
thedfl.com
thescoutsreport.com
toptenify.com
tripacation.com
tweetwiki.com
umock.com

* Yes I realize I have a bit of an addiction to domain names, but I really do have specific ideas for each of the above.



This blog is powered by KickAssCode.