Getting and Parsing the Citi Bike JSON Feed With HTTParty
BigIdeas Citi Bike Finder Challenge
For our Flatiron School Meetup Presentation, Amy Johnson and I are trying to tackle at least part of one of NYCBigApps’ BigIdeas Challenges posted by Citi Bike, http://citibikefinder.splashthat.com/. The main problem they outline is that during high commute times people often find themselves being “dockblocked” or “bluebiked”, as the Gothamist put it, because there aren’t any available spaces to deposit their bike.
The main idea for a solution is to have some sort of suggestion app for starting and ending locations with estimated availability based on historical data. There are some other features they have on their wishlist for the app, but we figure this alone is about all we want to try to bite off for the moment.
Our Potential Solution
Obviously the first thing we need to figure out is, is what data is available to us. It seems, for the moment at least, that there isn’t historical data on bike dock stations readily available to us (we’ve reached out and are waiting to hear back on this), so for the time being it looks like we’ll potentially need to hit the real time JSON feed, and gather data that way. Plus even if there is historical data we can get we still may want to collect our own and we’ll definitely want to be grabbing data about station’s current available bikes and docks, if they’re in service, etc.
It’s a HTTParty
The first to do then is figure out how to get and parse this JSON feed. JSON by the way stands for JavaScript Object Notation and gives you back data in attribute-value pairs…basically a hash. After a quick search on the Google I found the HTTParty gem, which is a HTTP client gem that allows you to easily make HTTP requests as well as automatically parses JSON or XML responses you get back.
Ok down to business…Parsing the Citi Bike Feed.
This is super simple with HTTParty. All you have to do is require the gem and then use HTTParty.get(“the_url”)
, and if you stick pry in your file it becomes super duper easy to play with the HTTParty::Response
object that is returned to you.
1 2 3 4 5 6 7 |
|
What you get back is technically, as mentioned, a HTTParty::Response
object, but really it’s a hash. So if we call .keys
on it we can see what keys it has.
1 2 |
|
My guess is “stationBeanList” is what we want and sure enough it gives us back a big array of 332 station hashes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
From here it’s easy to grab specific data out of this array of stations. For example, the stations whose “statusKey” does not equal 1 since that probably will mean they’re not in service and might show me the other possible status keys.
1 2 3 |
|
Or let’s say we want to see the stations that have over 50 docks i.e the big ass docks in the city.
1 2 3 |
|
Next Steps
Getting the JSON feed and parsing it was way simpler than I had imagined. From here we need to figure out what our absolute bare bones app needs to do and then plan from there. We can also for sure create a Station class and generate a bunch of Station objects from the stations array. This way anytime we hit the JSON feed we can have these objects update their attributes.