Mind the Gap — a CLI app using the TFL API — Part 2
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.
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:
What this needed was a join class:
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:
- What stations are on a Tube Line? ✅
- What lines does a Tube Station belong to? ✅
- What Tube Stations have the most Lines? ✅
- What Tube Stations have have least Lines? ✅
- What Tube Lines have the most Stations? ✅
- 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!