Mind the Gap — a CLI app using the TFL API — Part 2

artismarti
3 min readNov 27, 2018

--

This post details what was built for the MVP. To read about the planning, go here. Details about how I used the TFL API are here. The final code repo can be found here.

MVP

Londoners (present company included) love to complain about the tube, but honestly — our public transport is amazing! Just the underground network alone, covers over 400 km and is used by 1.35 billion (yes, BILLION!) passengers annually.

Photo by Nick Fewings on Unsplash

There are 11 underground lines (not including DLR or Overground) and each line has several stations. For instance — The Northern Line has 50 Stations. One of those stations is, Moorgate. But Moorgate also has the Circle, Hammersmith & City and the Metropolitan Lines.

The relationship looks like this:

A Line has_many Stations. A Station has_many Lines.

What this needed was a join class:

A Line has_many Stops. A Station has_many Stops.

Line Model:

class Line <ActiveRecord::Base
has_many :stops
has_many :stations, through: :stops
end

Station Model:

class Station < ActiveRecord::Base
has_many :stops
has_many :lines, through: :stops
end

Stop Model:

class Stop < ActiveRecord::Base
belongs_to :line
belongs_to :station
end

Dummy Seed Data:

A Line has:

  • tfl_id (this is what TFL uses in the API as a unique identifier for each line)
  • name
  • colour (the colour of the line as depicted on the tube maps)
Line.create(tfl_id: "northern", name: "Northern", colour: "000000")
Line.create(tfl_id: "bakerloo", name: "Bakerloo", colour: "B36305")
Line.create(tfl_id: "central", name: "Central", colour: "E32017")
Line.create(tfl_id: "circle", name: "Circle", colour: "FFD300")
Line.create(tfl_id: "district", name: "District", colour: "00782A")
Line.create(tfl_id: "victoria", name: "Victoria", colour: "0098D4")

A Station has

  • name
Station.create(name: "Moorgate")

A Stop has

  • line_id
  • station_id.
Stop.create(station_id: 1, line_id: 3)
Stop.create(station_id: 1, line_id: 2)

With the classes and relationships for MVP done, I wrote some quick & dirty methods to check that could actually answer the questions we had set out as goals in the MVP & we could finally check these off:

  1. What stations are on a Tube Line? ✅
  2. What lines does a Tube Station belong to? ✅
  3. What Tube Stations have the most Lines? ✅
  4. What Tube Stations have have least Lines? ✅
  5. What Tube Lines have the most Stations? ✅
  6. What Tube Lines have the least stations? ✅

The MVP got done really quickly and while it was ugly and didn’t do any validations, it still worked. So the next thing was to get real data from the TFL API.

The next post will cover the epic struggles with the API.

Sh*t’s about to get real, y’all!

--

--

artismarti

Software Developer, Product Manager, biryani loving, फ़ेमिनिस्ट किल जॉय. Jill of all trades. Mistress of some.