Posts tagged Web Services
Versatile RESTful APIs Beyond XML
Mar 14th
An article I wrote has just been published over at InfoQ. It’s called Versatile RESTful APIs Beyond XML and shows how easy it can be to extend Rails’ RESTful behaviour to input and output resources not only as XML but also as JSON and Microformatted HTML.
The article builds on some posts on this blog, such as Intercepting Microformats In Rails Input, but offers a bit more context. The timing of the article fits nicely with a post on the microformats-rest list about Rails, REST and microformats, so hopefully we’ll see more discussion of these concepts over the coming weeks.
Rails Geo Plugins: acts_as_geocodable
Mar 7th
acts_as_geocodable (blog entry, repository) is the newest kid on the rails geo plugin block. It actually consists of two parts, a gem called graticule which handles the actual geocoding, interacting with external services, etc, and the plugin which offers extensions to your models.
I like that separation. Having the generalised code in a gem and the rails-specific hooks in a plugin makes a lot of sense and makes it much easier to use the core code in non-rails ruby apps, and having a single gem that supports multiple services allows for built-in failover should the preferred geocoder be unavailable.
Much of the functionality of the plugin is already integrated into my application, but not with quite so many options. In such cases I really enjoy installing plugins; there’s something very satisfying about going through my application deleting code.
The plugin adds two tables to your database. The first, geocodes, holds longitudes/latitudes for given addresses, while the other, geocodings, polymorphically links those geocodes to your existing models. In my case, this meant re-geocoding all the locations already in my database, but since I’m operating on a fairly small data set, that was a pretty simple case of iterating across them all and re-saving them. For those operating with very large databases, you may want to write a more sophisticated migration to handle that.
The trickiest thing was re-coding my search queries to use the new database. acts_as_geocodable offers a number of neat methods for running queries such as (from their documentation)
event.distance_to "49423" Event.find(:all, :within => 50, :origin => "97232") Event.find(:nearest, :origin => "Portland, OR")
But I wanted a way to build more sophisticated searches so I could, say, limit by title and order by distance. It turns out that’s pretty easy too:
Location.find(:all, :origin => 'Grand Rapids MI', :conditions => ['title LIKE ?', 'My Title'] :order => 'distance')
The one place where I had a problem was when trying to use the last of the examples.
Location.find(:nearest, :origin => 'Portland, OR')
blew up with:
ActiveRecord::StatementInvalid: Mysql::Error: Incorrect parameter count in the call to native function 'RADIANS': SELECT locations.*, geocodes.*, (ACOS( SIN(RADIANS()) * SIN(RADIANS(geocodes.latitude)) + COS(RADIANS()) * COS(RADIANS(geocodes.latitude)) * COS(RADIANS(geocodes.longitude) - RADIANS()) ) * 3963.1676) AS distance FROM locations JOIN geocodings ON locations.id = geocodings.geocodable_id AND geocodings.geocodable_type = 'Location' JOIN geocodes ON geocodings.geocode_id = geocodes.id ORDER BY distance ASC LIMIT 1
but when I used a full address (my home) in the same query, I got an appropriate result. It looks as though perhaps if it fails to get an appropriate pair of co-ordinates for the specified location, it tries to perform the query anyway, with an exception resulting.
I also found some problems when trying to use the plugin with locations outside North America, but that is a limitation of the geocoding services and not of the gem or plugin themselves. Hasten the day when enough data is open that global geocoding services can become a reality.
Working with acts_as_geocodable has so far been a very straightforward experience and has allowed me to rid my code of some pieces I’d always meant to refactor out. It’d be good to see the error I ran into handled more neatly, and perhaps an obvious API to take advantage of the failover options presented by graticule, but the plugin is still early in its life and shows a lot of promise.
Governmental Pipes
Mar 5th
I’ve refrained from blogging much about Yahoo! Pipes, mainly because everyone else seemed to be. It’s definitely an interesting development, and shows how far we’ve come with open data, but also how far we still are from that really making an obvious impact for non-geeks.
Two of the more interesting pieces on the use of Pipes that I’ve seen so far are two blog entries that Tim McGhee pointed out on the govtrack list. He’s done some work using Pipes to repurpose various feeds about government activity, and they’re worth a look. Check out: Managing the volume of content from Congress and Geek Out: Mashing Yahoo! Pipes and the Congressional Record over on his blogs.
Relax over REST
Feb 27th
Mark Nottingham has a good post running through a few topics on which people get needlessly caught when designing RESTful applications. If you’re new to working on RESTful application design (as many rails developers are) it’s worth checking out to save yourself needless anguish.
Thankfully for Rails developers at least some of the issues he identifies will be a little simpler than they might be for people designing systems from scratch. In particular, while there are a few URL design choices (numeric IDs, other parameters, or a hybrid? nested vs. flat?) the conventions are good and changing isn’t all that hard.
And while some sort of schema definition is important to ensure that server and client can be sure they’re speaking the same language, for those using tools like ActiveResource which take full advantage of dynamic languages, there’s a little more flexibility than there may have been before.
More on OpenID
Feb 26th
It seems DHH is hopping on the OpenID bandwagon, and that the next 37signals app will allow openid-based authentication. He’s talking about releasing his code as a plugin, so maybe I won’t need to find the time.
For those following the OpenID buzz, Simon Willison’s cool things you can build with OpenID is well worth reading to begin to get a sense of the new opportunities opened up once we have unique IDs for users that map between sites. And the comments on this post at Tim Bray’s site may help people with some lingering questions.
Update: DHH has committed the code as a plugin, and Dan Webb has posted another tutorial that’s well worth a look