Status
automated voice on the Utility Service phone is creepily alive
Location
1055 N Nelson St, Arlington, VA
Subscribe to GeoRSS Subscribe to KML


Sunlight Datakit - Congress in your App

Published in Mashup, Programming, Ruby, Web  |  1 Comment


Sunlight Labs has released a public API, their Sunlight Datakit. It’s a straight-forward, simple API for getting access to their Civic data, like Congressional Representatives, zipcodes, timezones, and some geographic information.

There is some basic information about elected representatives that makes politico mashups easier: the ability to tie a name to a state, the district and zip codes that they represent, their office telephone number, and so on. We have put together a simple labs “datakit” that does this for us, drawing from several publicaly available data sources. We are making this fully available and have provided a fully documented API for the methods we have developed for those sources. Find out about the datakit here.

Of course, any API needs a nice little client to tie it into your applications. Here is my Ruby client. It’s very simple, because is uses the fallback method_missing to handle any function passed to the class. This also allows the class to be extended by implementing specific methods if more processing of the response is needed.

require 'open-uri'
require 'rexml/document'
require 'cgi'
SUNLIGHT_HOST = 'http://sunlightlabs.com/datakit/'

class Sunlight
  def self.method_missing(service_method, *args)
    params = args[0].collect {|k,v| CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)}.join('&')
    url = SUNLIGHT_HOST + service_method.to_s + "?" + params
    open(url).read.split("|")
  end
end

resp = Sunlight.getDistrictFromZip5({:zip => 20740})
puts resp.inspect
  # MD:5
  # MD:4

resp = Sunlight.getRepresentativeNameFromCityState({:city => 'Detroit', :state => "MI"})
puts resp.inspect
  # Kilpatrick, Carolyn C.
  # Conyers, John  Jr.
  # Levin, Sander M.
  # McCotter, Thaddeus G.
  # Dingell, John D.

The Sunlight Datakit currently offers the following functions. Check out the documentation for information on the parameters and returned values.

  • getDistrictFromZip5
  • getStateFromZip5
  • getDistrictFromZip9
  • getStateFromZip9.php
  • getRepresentativeNameFromDistrict
  • getRepresentativePhoneNumberFromDistrict
  • getRepresentativeRoomNumberFromDistrict
  • getCityFromZip5
  • getCityStateFromZip5
  • getLatitudeFromCityState
  • getLongitudeFromCityState
  • getZipCodesFromCityState
  • getTimezoneFromCityState
  • getRepresentativeNameFromCityState
  • getRepresentativeNameFromState
  • getStateAbbreviationFromStateName
  • getStateNameFromStateAbbreviation

Responses

  1. Bob says:

    February 28th, 2007 at 1:48 pm (#)

    Just a quick comment — the braces surrounding the parameters to the two calls don’t seem to be required, as they’re automatically collected into a Hash and passed as a single parameter, as in:

    resp = Sunlight.getDistrictFromZip5(:zip => 20740)

    resp = Sunlight.getRepresentativeNameFromCityState(:city => ‘Detroit‘, :state => “MI”)

    Is there a benefit to adding the braces that I’m missing?

    Thanks for the post!

    Bob

Leave a Response