The following is a guest post by John Richardson and originally appeared on his blog. John is currently in the Ruby-003 class at The Flatiron School.
Should I run for the train?
Whenver I need to catch the LIRR, I usually end up running. And If I’m dodging and weaving around the fine people of NYC, I hope there’s a train warmed up and ready to go when I get to the station. Given that I spend so much time in the terminal (…on my computer) these days, I decided to build a gem that makes sure everything is ok with my train before the race begins by pulling the latest train service status data off the MTA webiste.
The gem can be installed by typing the following into your terminal:
gem install mta_status
Type in the following command into your terminal and you will get the subway service status by default.
If you type lirr, metro-north, or a host of other related terms after the mta_status command you should get the service status for that particular branch of the MTA. For example, the following will get you the service status for the LIRR.
In addition to service status, the developer site also has links to schedules and route information all meant to follow the General Transit Feed Specification (GTFS) specs. In other words, if you build something for one transit system, it will likely be transferrable to one of these other transits systems in the US and through the rest of the world.
The MTA service status page is a text file with no css. In my previous (and limited) expierence with Nokogiri I had searched for nodes using css, so with this page I had to search for nodes using xpath.
# xpath @doc.xpath("//name").collect do |name| name.children.text # css student_page.css('div.social-icons a').collect do |link| link.attr('href')
The page was layed out well so it didn’t take too long to figure out how to get xpath to find what I was looking for. Let me rephrase that, the page was pretty well laid out expcept for this:
<text> <span class="TitlePlannedWork" >Planned Work</span> <span class="DateStyle"> &nbsp;Posted:&nbsp;10/29/2013&nbsp; 9:43AM </span><br/><br/> <a class="plannedWorkDetailLink" onclick=ShowHide(50159);> <b>Busing at Melrose and Tremont Stations </a><br/><br/><div id= 50159 class="plannedWorkDetail" ></b>Beginning Aug 19 until further notice <br> <br>As part of the Bronx Right-of-Way Improvements Project, substitute bus service <br>will be provided at Melrose and Tremont Stations in both directions. Buses will <br>connect with trains at Fordham Station. Please visit <a href=http://new.mta.info/mnr target=_blank><font color=#0000FF><b><u>mta.info</u></b></font></a> for further details. <br> <br>Weekday Bus Schedule | <a href=http://advisory.mtanyct.info/pdf_files/HARLEM_08-19-2013%20FINAL%20Buses%20Only%20Mon-Fri.pdf target=_blank><font color=#0000FF><b><u>pdf</u></b></font></a> <br>Weekend Bus Schedule | <a href=http://advisory.mtanyct.info/pdf_files/HARLEM_08-19-2013%20FINAL%20Buses%20Only%20Sat-Sun.pdf target=_blank><font color=#0000FF><b><u>pdf</u></b></font></a> <br><b> <br></div></b><br/> <br/><br/> </text>
This is the “text” that explains the service disruption. My next task is to make sense of this mess and then potentially work it into a new version of the gem. Only issue there is the more features I add, the less useful the gem could become, especially since the service change information is often confusing.
I get the following gem related error everytime the gem runs. It doesn’t seem to effect how the gem runs, but it is ugly.
WARN: Unresolved specs during Gem::Specification.reset: mini_portile (~> 0.5.0) WARN: Clearing out unresolved specs. Please report a bug if this causes problems.
Also, there’s a ton of information on the bus system that I didn’t incorporate and also some information on the bridges and tunnels operated by the MTA.
Make yourself useful.