a work on process

Viewing posts tagged: API

Input formats and content types in Rails 1.2

3 February 2007 (2:54 pm)

By James Stewart
Filed under: Notes
Tagged: , , , ,

One feature of recent releases of Rails I hadn’t spotted before is the ability to define your own parameter parsing based on content type. I’m working on an application that will employ a RESTful API and that I hope will take its input in either standard http parameters, microformatted HTML, XML or JSON.

I don’t really want to have to write custom code within the controllers to interpret the input based on content type, so I started looking for how rails parses XML input and came across the following in the actionpack changelog:

    # Assign a new param parser to a new content type
    ActionController::Base.param_parsers['application/atom+xml'] = Proc.new do |data| 
      node = REXML::Document.new(post) 
     { node.root.name => node.root }
    end
 
    # Assign the default XmlSimple to a new content type
    ActionController::Base.param_parsers['application/backpack+xml'] = :xml_simple

Looking at the actual source code it appears it’s actually being implemented slightly differently, with the Mime::Type object being used as the key, rather than the text content type. Since the json content type is already defined (with a reference to the object in Mime::JSON), JSON can (usually) be parsed as YAML, and the :yaml symbol is a shortcut to a YAML parser, handling it transparently is almost as simple as adding:

ActionController::Base.param_parsers[Mime::JSON] = :yaml

or if we wanted to be a bit more explicit:

ActionController::Base.param_parsers[Mime::JSON] = Proc.new do |data| 
	YAML::load(data) 
end

to environment.rb. Building these APIs is even easier than I’d thought!

Recommend this post:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

 

For some time now I’ve been interested in the possibility of bringing together political information from all different layers of government and finding ways of layering it. Too few of us understand where the key decisions on the issues that concern or affect us are taken. Action at a local level can be a very powerful political tool it’s hard to find out which level is most appropriate, or to trace how issues move between layers. Unfortunately it can seem even harder to find well-structured data at more local levels than it is on a national level.

That’s why I was very interested to discover Civic Footprint, a project of the Center for Neighborhood Technology that provides a simple web interface (and since May 2006 an API) for residents of Cook County, Illinois to find out the ‘political geography’ of their address.

For users of the website those districts are matched up with representatives, so you can quickly find out who represents you on each level, and from there jump off to that representative’s website or wikipedia entry, or a Google News or Technorati search for them. It’d be nice if the congressional pages (such as this for Danny Davis (D)) were integrated with a site like govtrack for more targetted information that google or technorati can provide, but it’s still a great source of information.

It doesn’t look like the API will yet tell you who the representatives are for each of your districts, simply providing the IDs of those districts. Hopefully it will soon. It’ll be very interesting to see how the site develops, as it shows potential to become something of an example of how civic and political data can be made accessible and how services can be built on top of that.

Recommend this post:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

 

The good people at mySociety have been discussing the API they’ve opened up for They Work For You. They also have a few examples of how the API might be applied.

Best of them, and possibly the best API demo I’ve seen, is a text adventure run over telnet. Entering a UK postcode will select an MP for you, and you have to guide that MP to Tony Blair’s Sedgefield constituency, doing battle with any opposing MPs you pass along the way, and eventually fight the PM himself.

Entering my parents’ Tunbridge Wells postcode unfortunately selects me a Conservative MP, but in the spirit of role-playing I guided him to Sedgefield and defeated Mr. Blair. Greg Clark MP’s verbal diarrhoea skills are not to be trifled with, and he picked up 3246 Experience Points along the way. Very entertaining.

Read more about ‘Battle your way to Sedgefield’ here, and try your hand here (or here if you really want to avoid telnet).

Recommend this post:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

 

Services_Technorati

8 February 2005 (3:11 pm)

By James Stewart
Filed under: Announcements
Tagged: , , ,

With the initial stages of all three current projects out the door, it seemed like time to get my Technorati API implementation up to date. There have been quite a few changes to the API since I released the last version at the beginning of October and I felt it was time to begin catching up.

In the end, I opted for a complete rewrite, following the PEAR coding standards as I intend to propose it for inclusion in PEAR soon. Having benefitted so much lately from the tools included in PEAR I decided it was a good idea to give something back and this seems an obvious starting point.

Right now there’s a good bit of tightening up to do. The core functionality is all in place, but the system isn’t accepting the passwords sent through my attention XML functions and the caching options could be more finely grained. Hopefully there’ll be time later in the week to fix those up and begin proposing the package.

Recommend this post:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

 
« Previous Page