How to consume Project Nimbus dataservices in Ruby

Project Nimbus is a community initiative by developers working in key corporations and government agencies in Singapore that aims to open up data and services for local innovators who wants to create applications with localized data.

The project is current supported by a mix of government agencies like LTA, NEA as well as private businesses/startups like Chlkboard and Shownearby. The list is no where exhaustive and the dataset curators are constantly updating their site with new data providers, so please refer to the Project Nimbus page for a more up to date list.

Project Nimbus currently provides PHP/.NET and Java sample codes along side their data set, but not for Ruby yet. So the purpose of this post is to try and fill this missing gap by demostrating how to consume Project Nimbus data in Ruby.

So without further ado, you’re going to need:

  • Ruby 1..8.7 (and above)
  • HTTParty gem (gem install httparty)
  • Nokogiri gem (gem install nokogiri)

For the purpose of this demonstration, I’m going to use the LTA dataset to get a list of current road conditions.

I was actually contemplating doing a step by step tutorial but I’m afraid I might get too long winded, so instead I’m pasting the entire sample code here together with comments that should help you understand what its doing.

If after going through the code you still have some questions, please feel free to drop a comment, I’ll do my best to help out in anyway I can.

require 'rubygems'
require 'httparty'
require 'nokogiri'

class Nimbus
  include HTTParty

  # this is where you input your AccountKey and UserID
  # if you don't already have a key, you can request one from projectnimbus
  headers 'AccountKey' => '', 
          'UniqueUserID' => '' 

  # if your dataset uri is given as
  # is your base_uri
  base_uri ''
  def self.get_roadconditions
    # path to dataset (excluding base_uri)

# and you're done! that's all you need to consume
# Nokogiri is used to parse the data
# if you have a better way of parsing and iterating through the xml, please do
# not hesitate to share. I'm pretty sure my way is not the most efficient way.

doc = Nokogiri.XML(Nimbus.get_roadconditions.to_s)
doc.xpath('//m:properties').each do |wad|
  # please refer to the metadata given on project nimbus to find out what data you can
  # extract
  puts wad.xpath('d:RoadName').text
  puts wad.xpath('d:Speed').text
  puts "======"

# this returns:
#   90
#   ======

You can also grab the source .rb file here.

I hope you find this snippet useful (or at least informative). It was pretty frustrating for me at first trying to understand how to get nimbus to work with ruby so I hope this would be able to help people avoid going down that route.