Tutorial: How I used Hartford’s open data to build my app

Print More

Editor’s note: We’ve been doing tutorials that require varying levels of technical proficiency, understanding that many people will not be able to make it ‘work’ in just a few hours. This week’s tutorial has an especially high barrier to entry — and we promise that it won’t always be that way. But these posts will exist on the internet for the foreseeable future, so if you ever decide to embark on one of these projects, you can come back and follow these tutorials.

This tutorial will walk through how I built TowedCar.info, which was covered in TrendCT a few weeks ago.

In case you missed it, the app uses Hartford’s open data portal to check whether your car has been towed — and, if it has, the app will send you a text message.

Not many apps have been built using the data portal, so I wanted to walk through my process. If you don’t know how to get started, read my first post, which gives you the lay of the land.

Difficulty Level

Intermediate to Hard: It requires you to set up Ruby on Rails, and know how to put your project on the internet. We’ll walk through some of this today.

Accessing the data with Ruby on Rails

TowedCar.info was built using Ruby on Rails. One of the important features of Rails is its ability to communicate with other programs through an application programming interface (API). Every dataset on Hartford’s open data portal has an API that makes it easier for other computer programs to receive data from it. The data portal documents the API for the tow data. Using that API I was able to write a basic method called “towed” to query whether a vehicle was towed:

def towed
    vehicle_plate = params[:q].gsub(/[^A-Za-z0-9]/, "")
    base_url = 'https://data.hartford.gov/resource/hefc-wgp8.json?$where=vehicle_plate%20=%20\''
    full_url = base_url + vehicle_plate + "\'"
    car = JSON.parse(open(full_url).read)
    if car.first.nil? 
      @body = "No, keep looking."
      @body = "Shit, yes it was."
      @car = car

The first line creates a variable called vehicle_plate and sets it to whatever value you input on the main page, stripping out any extraneous characters or whitespace by using a tool called regular expressions. Params[:q] is the text that was typed into the form on the front page, and then by appending .gsub I am telling Ruby I want to use regular expressions to change that text in some manner. The /[0]/, "" portion inside the parenthesis tells gsub that I only want vehicle_plate to include capital letters, lowercase letters, and numbers. Everything else will be discarded.

Base_url is the url of the API that I retrieved from the API documentation. The ?$where=vehicle_plate%20=%20 portion tells the API that this request is going to look for a vehicle with a specific plate number. I then append my vehicle_plate to the URL to get the full URL. You can type a URL with a vehicle_plate appended to the end into a browser to see what response you might get directly from the API for example https://data.hartford.gov/resource/hefc-wgp8.json?$where=vehicle_plate%20=%20’121WMS’ gives you the following response:

[ {
  "tow_from_address" : "BROAD ST & LINCOLN ST",
  "model" : "I35",
  "tow_firm_phone" : "860-236-2602",
  "townum" : "308318",
  "geom" : {
    "needs_recoding" : false,
    "longitude" : "-72.6871096658919",
    "latitude" : "41.7533953707842",
    "human_address" : "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}"
  "vehicle_state" : "CT",
  "date" : "04282015",
  "vehicle_plate" : "121WMS",
  "time" : "1113",
  "color" : "RED",
  "vehicle_year" : "2002",
  "tow_firm_address" : "971 CAPITOL AV. HARTFORD,CT 06106",
  "tow_firm" : "CAPITOL AUTOMOTIVE LLC",
  "make" : "INFI"
Checking if the car has been towed

My next step was to pull the response from the API into my program. To do this I created a variable called car. The open(full_url) portion of this line goes to the API and makes the request for information. The read portion then lets the program read the same text from the response that you see if you type the URL into the browser and look at the response on a web page. The real magic in this step is the fact we wrapped this entire command in JSON.parse. JSON.parse is a method that makes it easier to access the data in the text that we will explore in a bit. Finally we ask if there is a car in the response by typing if car.first.nil?. If the API did not return any automobile matches, the answer to this question will be true, and we will tell the user to keep looking for their car. However if the answer to this is false, then we want to let the user know and give them information on the car. Putting the @ symbol in front of our variable means that it is an instance variable and thus the information is accessible both in our controller file and our view file.

We then can give the website user information about their car by accessing it from the @body and @car variables. The code is put into another file called a view and if your car was towed it looks like:

<p>It was towed from <%= @car.first["tow_from_address"].titleize %>. It is now at <%= @car.first["tow_firm_address"].titleize %> which you can call at <%= @car.first["tow_firm_phone"] %>.</p>
Accessing data about the towed car

Because we used JSON.parse earlier we can access data about the towed car by putting the names of the data fields inside brackets. We call @car.first because we only want data from the first car in our list of results. If there were two cars with the same license plate we could access them by using @car[0], @car[1], and so forth. We then access the relevant information about the car by typing things like @car[0][“tow_firm_address”]. In the real world we should not see the same car twice in our tow database, but even if we encounter the edge case where it is, we know that @car.first will return the most recent automobile. So you can now see what information will be displayed by the webpage that is returned to the user by looking at the code above. The advantage of this is that my program can ask this other program for information, retrieve it, and then show it to users of my program in a way that I think they will find helpful.

Matt Zagaja is an attorney who has been involved in state and municipal politics. We welcome contributors to share data, analysis and perspectives that follow our guidelines.

What do you think?