I was curious what the smallest margins were by which candidates had won, and one thing lead to the next...
Data from https://rtr.elections.on.ca/RealTimeResults/en/electoral-district. Thanks for the open endpoints!
I was curious what the smallest margins were by which candidates had won, and one thing lead to the next...
Data from https://rtr.elections.on.ca/RealTimeResults/en/electoral-district. Thanks for the open endpoints!
# frozen_string_literal: true | |
require 'faraday' | |
require 'faraday/net_http' | |
require 'json' | |
require 'pry' | |
Faraday.default_adapter = :net_http | |
conn = Faraday.new( | |
url: 'https://rtr.elections.on.ca/RealTimeResults/api', | |
headers: { | |
'Content-Type' => 'application/json', | |
'Accept' => 'application/json', | |
}, | |
) do |f| | |
f.request :json | |
f.response :json | |
end | |
class ElectoralDistrict | |
attr_accessor :results | |
attr_reader :body | |
def initialize(body) | |
@body = body | |
end | |
def id = body.fetch("i") | |
def name = body.fetch("n") | |
def leading_candidate = @leading_candidate ||= candidates.find { _1.winning? } | |
def candidates = @candidates ||= results.fetch("cs").map { Candidate.new(_1) } | |
end | |
class Candidate | |
attr_reader :body | |
def initialize(body) | |
@body = body | |
end | |
def name = body.fetch("n") | |
def party_name = body.fetch("pn") | |
def party_name_acronym = body.fetch("pna") | |
def winning? = !margin.nil? | |
def margin = body.fetch("m")&.delete(',')&.to_i | |
def votes = body.fetch("v") | |
end | |
# Fetch list of districts | |
districts_by_id = conn.get('refdata/geteds/513/en').body.map { ed = ElectoralDistrict.new(_1); [ed.id, ed] }.to_h | |
# Fetch and populate results for all districts | |
conn.post('edresults/getedresults') do |req| | |
req.body = {"languageCode":"en","electoralDistricts": districts_by_id.values.map do |district| | |
{"eventId":513,"electoralDistrictId": district.id} # Ottawa Centre is 78 | |
end} | |
end.body.each do |results_for_district| | |
districts_by_id.fetch(results_for_district.fetch("edi")).results = results_for_district | |
end | |
# For each district, show the leading candidate and their margin | |
column_widths = { | |
district_name: 0, | |
party_name_acronym: 0, | |
margin: 0, | |
candidate_name: 0, | |
} | |
# Collect data to print | |
rows = districts_by_id.values.sort_by { _1.leading_candidate.margin }.map do |district| | |
leader = district.leading_candidate | |
total_votes = district.candidates.sum(&:votes) | |
# Forgive me, dear reader... | |
[ | |
district.name, | |
leader.name, | |
leader.party_name_acronym, | |
leader.margin.to_s, | |
total_votes.to_s, | |
"%0.2f" % [(100.0 * leader.margin / total_votes)], # % | |
] | |
end | |
# Calculate table column widths | |
lengths = rows[0] | |
.zip(*rows[1..-1]) # [[A1,A2,A3],[B1,B2,B3]... | |
.map { _1.map(&:length).max } | |
# Format and print each row | |
rows.sort_by { _1[5].to_f }.each do |row| | |
# ...but we're going to rely on matching indices | |
puts "#{ | |
row[0].ljust(lengths[0], ?_) | |
}_#{ | |
row[1].rjust(lengths[1], ?_) | |
} (#{ | |
row[2].ljust(lengths[2]) | |
}) by #{ | |
row[3].rjust(lengths[3]) | |
} / #{ | |
row[4].rjust(lengths[4]) | |
} votes (#{ | |
row[5].rjust(lengths[5]) | |
}%)" | |
end |
Barrie—Springwater—Oro-Medonte________________________________DOWNEY, DOUG (PCP) by 609 / 37959 votes ( 1.60%) | |
Etobicoke—Lakeshore_____________________________________HOGARTH, CHRISTINE (PCP) by 803 / 47980 votes ( 1.67%) | |
Eglinton—Lawrence____________________________________________MARTIN, ROBIN (PCP) by 722 / 39459 votes ( 1.83%) | |
Oshawa____________________________________________________FRENCH, JENNIFER (NDP) by 757 / 40799 votes ( 1.86%) | |
Niagara Centre_________________________________________________BURCH, JEFF (NDP) by 850 / 41226 votes ( 2.06%) | |
Ottawa West—Nepean__________________________________________PASMA, CHANDRA (NDP) by 908 / 41906 votes ( 2.17%) | |
Beaches—East York___________________________________MCMAHON, MARY-MARGARET (LIB) by 898 / 40648 votes ( 2.21%) | |
Toronto—St. Paul's____________________________________________ANDREW, JILL (NDP) by 1092 / 42175 votes ( 2.59%) | |
York South—Weston____________________________________________FORD, MICHAEL (PCP) by 796 / 30426 votes ( 2.62%) | |
Glengarry—Prescott—Russell______________________________SARRAZIN, STÉPHANE (PCP) by 1283 / 41011 votes ( 3.13%) | |
Thunder Bay—Superior North__________________________________VAUGEOIS, LISE (NDP) by 800 / 24634 votes ( 3.25%) | |
Thunder Bay—Atikokan________________________________________HOLLAND, KEVIN (PCP) by 898 / 26598 votes ( 3.38%) | |
Mississauga East—Cooksville________________________________RASHEED, KALEED (PCP) by 1249 / 33869 votes ( 3.69%) | |
Humber River—Black Creek____________________________________RAKOCEVIC, TOM (NDP) by 884 / 22931 votes ( 3.86%) | |
Milton__________________________________________________________GILL, PARM (PCP) by 1680 / 38927 votes ( 4.32%) | |
Haldimand—Norfolk_________________________________________BRADY, BOBBI ANN (IND) by 2070 / 45427 votes ( 4.56%) | |
Mississauga—Erin Mills______________________________________SABAWY, SHEREF (PCP) by 1676 / 36370 votes ( 4.61%) | |
Ajax_______________________________________________________BARNES, PATRICE (PCP) by 1775 / 37688 votes ( 4.71%) | |
Parry Sound—Muskoka_________________________________________SMITH, GRAYDON (PCP) by 2114 / 44522 votes ( 4.75%) | |
Don Valley West__________________________________________BOWMAN, STEPHANIE (LIB) by 1864 / 36099 votes ( 5.16%) | |
St. Catharines_____________________________________________STEVENS, JENNIE (NDP) by 2277 / 43172 votes ( 5.27%) | |
Oakville_________________________________________________CRAWFORD, STEPHEN (PCP) by 2530 / 46504 votes ( 5.44%) | |
Scarborough Centre____________________________________________SMITH, DAVID (PCP) by 1784 / 31876 votes ( 5.60%) | |
Nepean_______________________________________________________MACLEOD, LISA (PCP) by 2271 / 39105 votes ( 5.81%) | |
Kingston and the Islands__________________________________________HSU, TED (LIB) by 3170 / 48738 votes ( 6.50%) | |
Willowdale_______________________________________________________CHO, STAN (PCP) by 2115 / 31585 votes ( 6.70%) | |
Windsor West_________________________________________________GRETZKY, LISA (NDP) by 2184 / 31802 votes ( 6.87%) | |
Toronto Centre___________________________________________WONG-TAM, KRISTYN (NDP) by 2461 / 34909 votes ( 7.05%) | |
Hamilton East—Stoney Creek___________________________________LUMSDEN, NEIL (PCP) by 2552 / 35166 votes ( 7.26%) | |
Mississauga Centre______________________________________KUSENDOVA, NATALIA (PCP) by 2459 / 33758 votes ( 7.28%) | |
Timiskaming—Cochrane_________________________________________VANTHOF, JOHN (NDP) by 1601 / 21574 votes ( 7.42%) | |
Hamilton West—Ancaster—Dundas__________________________________SHAW, SANDY (NDP) by 3522 / 44600 votes ( 7.90%) | |
Peterborough—Kawartha__________________________________________SMITH, DAVE (PCP) by 4167 / 52384 votes ( 7.95%) | |
Mississauga—Lakeshore_______________________________________CUZZETTO, RUDY (PCP) by 3522 / 42805 votes ( 8.23%) | |
London North Centre_____________________________________KERNAGHAN, TERENCE (NDP) by 3909 / 42951 votes ( 9.10%) | |
Sault Ste. Marie______________________________________________ROMANO, ROSS (PCP) by 2577 / 26884 votes ( 9.59%) | |
Mississauga—Streetsville______________________________________TANGRI, NINA (PCP) by 3844 / 37848 votes (10.16%) | |
University—Rosedale__________________________________________BELL, JESSICA (NDP) by 4036 / 38214 votes (10.56%) | |
Algoma—Manitoulin__________________________________________MANTHA, MICHAEL (NDP) by 2576 / 24277 votes (10.61%) | |
Don Valley North_______________________________________________KE, VINCENT (PCP) by 3418 / 31806 votes (10.75%) | |
Kitchener—Conestoga___________________________________________HARRIS, MIKE (PCP) by 4194 / 37589 votes (11.16%) | |
London West_________________________________________________SATTLER, PEGGY (NDP) by 5624 / 49865 votes (11.28%) | |
Markham—Thornhill_________________________________________KANAPATHI, LOGAN (PCP) by 3248 / 28701 votes (11.32%) | |
Mushkegowuk—James Bay_______________________________________BOURGOUIN, GUY (NDP) by 824 / 7248 votes (11.37%) | |
Niagara Falls_________________________________________________GATES, WAYNE (NDP) by 5946 / 51183 votes (11.62%) | |
Scarborough—Agincourt_______________________________________BABIKIAN, ARIS (PCP) by 3368 / 28606 votes (11.77%) | |
Don Valley East_______________________________________________SHAMJI, ADIL (LIB) by 3315 / 28036 votes (11.82%) | |
Oakville North—Burlington_________________________TRIANTAFILOPOULOS, EFFIE (PCP) by 5590 / 47095 votes (11.87%) | |
Sudbury________________________________________________________WEST, JAMIE (NDP) by 3494 / 29411 votes (11.88%) | |
Brampton East______________________________________________GREWAL, HARDEEP (PCP) by 3612 / 28986 votes (12.46%) | |
Markham—Stouffville_________________________________________CALANDRA, PAUL (PCP) by 5656 / 43715 votes (12.94%) | |
Burlington_________________________________________________PIERRE, NATALIE (PCP) by 6891 / 52536 votes (13.12%) | |
Newmarket—Aurora____________________________________GALLAGHER MURPHY, DAWN (PCP) by 5601 / 41539 votes (13.48%) | |
Kitchener South—Hespeler_______________________________________DIXON, JESS (PCP) by 4723 / 34915 votes (13.53%) | |
Orléans_____________________________________________________BLAIS, STEPHEN (LIB) by 7056 / 51739 votes (13.64%) | |
York Centre_______________________________________________KERZNER, MICHAEL (PCP) by 3891 / 28116 votes (13.84%) | |
Kitchener Centre__________________________________________LINDO, LAURA MAE (NDP) by 5411 / 38931 votes (13.90%) | |
London—Fanshawe__________________________________________ARMSTRONG, TERESA (NDP) by 4899 / 34257 votes (14.30%) | |
Etobicoke Centre______________________________________________SURMA, KINGA (PCP) by 6596 / 45252 votes (14.58%) | |
Mississauga—Malton___________________________________________ANAND, DEEPAK (PCP) by 4143 / 28215 votes (14.68%) | |
Brampton Centre________________________________________WILLIAMS, CHARMAINE (PCP) by 3596 / 24466 votes (14.70%) | |
Scarborough—Guildwood_______________________________________HUNTER, MITZIE (LIB) by 4280 / 28949 votes (14.78%) | |
Cambridge___________________________________________________RIDDELL, BRIAN (PCP) by 5845 / 39383 votes (14.84%) | |
Hamilton Mountain__________________________________________TAYLOR, MONIQUE (NDP) by 5041 / 33334 votes (15.12%) | |
Ottawa—Vanier_____________________________________________COLLARD, LUCILLE (LIB) by 5750 / 37227 votes (15.45%) | |
Pickering—Uxbridge_____________________________________BETHLENFALVY, PETER (PCP) by 6863 / 43232 votes (15.87%) | |
Brantford—Brant________________________________________________BOUMA, WILL (PCP) by 7455 / 46953 votes (15.88%) | |
Windsor—Tecumseh_____________________________________________DOWIE, ANDREW (PCP) by 6141 / 38485 votes (15.96%) | |
Brampton North____________________________________________MCGREGOR, GRAHAM (PCP) by 4872 / 30055 votes (16.21%) | |
Waterloo___________________________________________________FIFE, CATHERINE (NDP) by 7439 / 44913 votes (16.56%) | |
Chatham-Kent—Leamington______________________________________JONES, TREVOR (PCP) by 6365 / 36503 votes (17.44%) | |
Brampton South____________________________________SARKARIA, PRABMEET SINGH (PCP) by 5023 / 28602 votes (17.56%) | |
Scarborough—Rouge Park_________________________________THANIGASALAM, VIJAY (PCP) by 6210 / 34602 votes (17.95%) | |
Spadina—Fort York____________________________________________GLOVER, CHRIS (NDP) by 6132 / 33849 votes (18.12%) | |
Vaughan—Woodbridge________________________________________TIBOLLO, MICHAEL (PCP) by 6725 / 35958 votes (18.70%) | |
Scarborough North_____________________________________________CHO, RAYMOND (PCP) by 4896 / 26097 votes (18.76%) | |
Kanata—Carleton________________________________________FULLERTON, MERRILEE (PCP) by 8425 / 43885 votes (19.20%) | |
Scarborough Southwest__________________________________________BEGUM, DOLY (NDP) by 6518 / 33878 votes (19.24%) | |
Nickel Belt________________________________________________GÉLINAS, FRANCE (NDP) by 6431 / 30738 votes (20.92%) | |
Durham______________________________________________________MCCARTHY, TODD (PCP) by 10337 / 49321 votes (20.96%) | |
Richmond Hill___________________________________________________WAI, DAISY (PCP) by 6252 / 29709 votes (21.04%) | |
Carleton___________________________________________________GHAMARI, GOLDIE (PCP) by 9843 / 46310 votes (21.25%) | |
Ottawa South__________________________________________________FRASER, JOHN (LIB) by 8663 / 40499 votes (21.39%) | |
Brampton West______________________________________________SANDHU, AMARJOT (PCP) by 6640 / 30316 votes (21.90%) | |
Nipissing______________________________________________________FEDELI, VIC (PCP) by 6727 / 30659 votes (21.94%) | |
Aurora—Oak Ridges—Richmond Hill_____________________________PARSA, MICHAEL (PCP) by 7340 / 32559 votes (22.54%) | |
Essex______________________________________________________LEARDI, ANTHONY (PCP) by 10983 / 48343 votes (22.72%) | |
Flamborough—Glanbrook________________________________________SKELLY, DONNA (PCP) by 10311 / 43954 votes (23.46%) | |
Thornhill_____________________________________________________SMITH, LAURA (PCP) by 8147 / 34527 votes (23.60%) | |
Niagara West_______________________________________________OOSTERHOFF, SAM (PCP) by 10121 / 41795 votes (24.22%) | |
Kiiwetinoong__________________________________________________MAMAKWA, SOL (NDP) by 1111 / 4534 votes (24.50%) | |
Perth—Wellington______________________________________________RAE, MATTHEW (PCP) by 10289 / 41534 votes (24.77%) | |
Markham—Unionville_____________________________________________PANG, BILLY (PCP) by 9211 / 35772 votes (25.75%) | |
Northumberland—Peterborough South___________________________PICCINI, DAVID (PCP) by 13348 / 51437 votes (25.95%) | |
Whitby__________________________________________________________COE, LORNE (PCP) by 13368 / 49870 votes (26.81%) | |
Bruce—Grey—Owen Sound__________________________________________BYERS, RICK (PCP) by 11814 / 41659 votes (28.36%) | |
Bay of Quinte__________________________________________________SMITH, TODD (PCP) by 12293 / 43315 votes (28.38%) | |
Hastings—Lennox and Addington__________________________________BRESEE, RIC (PCP) by 10871 / 38126 votes (28.51%) | |
Oxford_____________________________________________________HARDEMAN, ERNIE (PCP) by 12662 / 44322 votes (28.57%) | |
Simcoe—Grey______________________________________________SAUNDERSON, BRIAN (PCP) by 15380 / 52852 votes (29.10%) | |
Sarnia—Lambton_________________________________________________BAILEY, BOB (PCP) by 11691 / 40174 votes (29.10%) | |
King—Vaughan________________________________________________LECCE, STEPHEN (PCP) by 11951 / 40727 votes (29.34%) | |
Lanark—Frontenac—Kingston_____________________________________JORDAN, JOHN (PCP) by 12193 / 41159 votes (29.62%) | |
Dufferin—Caledon_____________________________________________JONES, SYLVIA (PCP) by 14222 / 46088 votes (30.86%) | |
Barrie—Innisfil____________________________________________KHANJIN, ANDREA (PCP) by 11283 / 36272 votes (31.11%) | |
Parkdale—High Park_______________________________________KARPOCHE, BHUTILA (NDP) by 13477 / 42664 votes (31.59%) | |
Etobicoke North_________________________________________________FORD, DOUG (PCP) by 7960 / 25015 votes (31.82%) | |
Ottawa Centre_________________________________________________HARDEN, JOEL (NDP) by 17576 / 55199 votes (31.84%) | |
Simcoe North__________________________________________________DUNLOP, JILL (PCP) by 14833 / 46310 votes (32.03%) | |
Elgin—Middlesex—London__________________________________________FLACK, ROB (PCP) by 14393 / 43778 votes (32.88%) | |
Toronto—Danforth_____________________________________________TABUNS, PETER (NDP) by 13650 / 41325 votes (33.03%) | |
Huron—Bruce_________________________________________________THOMPSON, LISA (PCP) by 15594 / 46894 votes (33.25%) | |
Guelph_____________________________________________________SCHREINER, MIKE (GPO) by 18610 / 54667 votes (34.04%) | |
Wellington—Halton Hills________________________________________ARNOTT, TED (PCP) by 17325 / 49493 votes (35.00%) | |
Timmins______________________________________________________PIRIE, GEORGE (PCP) by 5155 / 14554 votes (35.42%) | |
Haliburton—Kawartha Lakes—Brock______________________________SCOTT, LAURIE (PCP) by 17979 / 48636 votes (36.97%) | |
Davenport____________________________________________________STILES, MARIT (NDP) by 13427 / 35473 votes (37.85%) | |
Kenora—Rainy River__________________________________________RICKFORD, GREG (PCP) by 6134 / 15530 votes (39.50%) | |
York—Simcoe_____________________________________________MULRONEY, CAROLINE (PCP) by 14470 / 36628 votes (39.51%) | |
Leeds—Grenville—Thousand Islands and Rideau Lakes_____________CLARK, STEVE (PCP) by 16871 / 42657 votes (39.55%) | |
Stormont—Dundas—South Glengarry_______________________________QUINN, NOLAN (PCP) by 14310 / 36112 votes (39.63%) | |
Lambton—Kent—Middlesex___________________________________MCNAUGHTON, MONTE (PCP) by 17508 / 43516 votes (40.23%) | |
Hamilton Centre____________________________________________HORWATH, ANDREA (NDP) by 11890 / 29147 votes (40.79%) | |
Renfrew—Nipissing—Pembroke_________________________________YAKABUSKI, JOHN (PCP) by 17691 / 40188 votes (44.02%) |