Today I got the very basic bus data collection working using Cucumber TDD. That means that I wrote the basic test I wanted to prove BEFORE I wrote the code that operates the test.
The Cucumber feature test looks like this:
Feature: Mobile Access
In order to ensure that location updates are captured
School Bus Location providers
want to have data they send stored on the siteScenario: Update Location
When bus named “lion” in the “eanes” district with a id of “1234″ goes to “32,-97″
When I go to the bus “1234″ page
Then json has an object called “buses”
And json has a record “1234″ in “buses” with “lat” value “32″
And json has a record “1234″ in “buses” with “lng” value “-97″
There’s is some code behind this feature that calls the web page and gets the JSON response back. The code that actually does the work in the bus controller is even simpler:
The at routine takes location updates just parses the parameters and stuffs it into our cache. For now, we’ll ignore names and district data.
def at
Rails.cache.write params[:id], “#{params[:lat]},#{params[:lng]},#{params[:name]},#{params[:district]}”, :raw=>:true, :unless_exist => false, :expires_in => 5.minutes
render :nothing => trueend
The code that returns the location (index) pulls the string out of the cache and returns the value as simple JSON.
def index
data = Rails.cache.read(params[:id], :raw => true).split(‘,’)
if data.nil?
render :nothing => true
else
render :json => {:buses => { params[:id].to_sym => { :lat => data[0], :lng => data[1] } } }
endend
Not much to it! It’s handy that Rails has memcache support baked right in! I just had to add a line to the environment.rb file and start my memcached server.
