The Task and the Flickr API

One of the projects I am currently working on is Ouray Climbing. It’s for a friend’s mountain and ice climbing guide service in our hometown of Ouray, Colorado. The guys and gals at Ouray Climbing are world-renowned bad-asses (for real … I’m not exaggerating). If you ever get the chance to come to Ouray and want to head into the backcountry with some expert guides, check them out.

Just before the winter holidays, I met with my buddy and he asked if it would be possible to pull down some photos from Flickr to be displayed on the company’s destination pages using our standard slideshow widget.

I never previously used the Flickr API but knew it was possible to accomplish what they wanted. So, I set about exploring the API docs and the general steps I needed to take to implement the desired functionality. Ultimately, we decided that the Ouray Climbing team would create a photo set for each of their destinations and upload a series of photos for each set. The photoset ID would then be stored in the DB and, if one exists for a destination, we would make a call to Flickr and grab the photos. Simple enough, eh?

All in all, it was pretty simple to get the photos from a photoset. The Flickr API, IMHO, is pretty well documented and easy to use. The challenge for me was more in how to best integrate it into the site’s framework: CFWheels. Aside: I’ve said it before and I’ll say it again, I freaking dig CFWheels.*

Plugins are the Answer (if only I knew the question)

Initially, I created a Flickr CFC and called it from my Destinations controller (CFC). It all worked and worked fairly well. However, I know enough about CFWheels to be dangerous (hey-o! Couldn’t resist the cliche!). Ok, sorry. I know enough about CFWheels to know that this is not the best way to implement the desired Flickr functionality.

So, I created a plugin, called SimpleFlickr, to be used across the application from any controller. I won’t bore you (further) with how I created the plugin. The Wheels crew takes care of that just fine on their site (Using and Creating Plugins).

Why are plugins the answer? Because Wheels injects your plugin’s methods into the framework and you can call any of the plugin’s methods from your controllers (or models or views) as though they were standard methods of the framework. No object creation, no inits, nada. Just access to the methods, plain and simple. Freaking sweet!!

Why SimpleFlickr?

There are already a couple of well-thought out, robust Flickr tools for CFML: CFlickr and flickrpics. Why didn’t I use them? Well, flickrpics is for populating a Mango Blog sidebar pod and CFlickr has more functionality than I needed for this project. I just needed something, well, simple. Plus, I was excited by the prospect of creating my own plugin!

SimpleFlickr Plugin and Repository

The alpha version of this plugin is currently available for download on github (SimpleFlickr on github). For the initial release, there are three public methods available: public void $setSimpleFlickrConfig(), public array function getFlickrPhotoSetPhotos(), and public xml function getFlickrPhotoSet().

To configure the plugin with your Flickr API access data (api key, secret, etc.), you can call $setSimpleFlickrConfig(), passing in the individual parameters (or an argumentCollection struct).

Calling getFlickrPhotoSetPhotos() will return an array of structures for each photo in the set. Each structure has two keys: url and title, which can be used to populate an image tag or something else in your application.

Calling getFlickrPhotoSet() will return the XML result of the http call to the Flickr API. As a publically invoked method, it doesn’t serve much of a purpose at this juncture; rather, it is used by the getFlickrPhotoSetPhotos() method to retrieve the XML from Flickr, which will is then parsed into the aforementioned array. Down the road, however, I think it’ll be useful on its own.

Alpha Version Limitations

As of the 0.1 release, the plugin knowingly works on ACF 9 and Railo 3.2 because it makes use of the newly added http() method inside a CFSCRIPT block. Also, the configuration method currently accepts a parameter called flickrURLMethod, which maps to the relevant API call you wish to make. This really doesn’t belong in the configuration and will be moved out of it shortly (it’s a remanant from the initial Flickr CFC I built).

Going Forward

Over the coming days and week, I will be actively updating the plugin to include a few additional Flickr API methods/calls as well as making a few teaks to how Flickr API access is configured. Additionally, I will create and add true documentation for configuring and using the plugin. Once the plugin reaches version 0.5 (or so), I will submit it to the Wheels team for approval in the hopes that it will be added to the Wheels plugin directory.

Finally, if folks out there think such a tool would be useful on its own (i.e., not as a Wheels plugin), I’ll submit a project to RIAforge for general CFML usage.

Thanks, as always, for reading and I am very open to and welcoming of any and all feedback!

Disclamer … Sort Of

*I want to note that my adoration for Wheels is in no way a slight towards or dig at any of the other rockin' CF frameworks out there. Admittedly, I’m a framework fanboi and find that Wheels, in particular, both speeds up and improves my CFML development process. I note this because, in my opinion, too often a positive comment about one CFML framework is seen as an implicit dig at others … and that just isn’t the case for me. I have so much respect for the develoeprs behind our community’s frameworks, I would never want to insult them or their hard (and awesome) work. Good lord, I sound like a politico … I’ve clearly spent too much time visting my family in Washington DC the past week-and-a-half :)!

Posted via email from self.is_a? Blog

Comments