Skip to content

Instantly share code, notes, and snippets.

@sprice
Created June 28, 2017 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sprice/62464049507d2690c0ffcaa7ed1333f6 to your computer and use it in GitHub Desktop.
Save sprice/62464049507d2690c0ffcaa7ed1333f6 to your computer and use it in GitHub Desktop.
foo V5Y0A5
bar L5G4L3
const fs = require('fs')
const parse = require('csv-parse')
const represent = require('represent')
const parser = parse({delimiter: ','}, (err, data) => {
data.forEach(item => {
const code = item[1]
represent.postalCode(code, (err, data) => {
const MP = data.representatives_centroid.filter(rep => rep.elected_office === 'MP')[0]
console.log(`The MP for the postal code ${code} is ${MP.first_name} ${MP.last_name}`)
})
})
})
fs.createReadStream(__dirname+'/codes.csv').pipe(parser)
{
"name": "represent-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Shawn Price",
"license": "MIT",
"dependencies": {
"csv-parse": "^1.2.0",
"represent": "^0.2.2"
}
}
@mgifford
Copy link

Ok, this almost worked.. Well, it did work, but ran into a bunch of data glitches I could fix and some coding issues I couldn't.

The biggest problem is that I'd put in 25 names & postal codes and I'd get back 25 MP names (in a different order). The whole point of the exercise was to be able to avoid cutting & pasting from http://represent.opennorth.ca/demo/ - because they were returned in a random order I ended up doing just as much cutting & pasting.

I ran into some interesting errors I wasn't expecting. These could be overcome in code, but mostly it is stuff that should at least be documented as the error reports aren't very clear.

  1. Postal codes need to be in all caps, no spaces and ensure that no 0's are written as O's. Exactly 6 characters.
  2. The names should be pretty simple. A-Z & 0-9 seem to be best. There might be a string limit to the name, so short is good.
  3. Better error reporting would be useful. Would be great to just do a dump of 200 name/postalcode pairs and know that it got through the first 59 before it ran into a problem. Right now it isn't clear what line caused the script to fail.

I'll want to do this in the future, so just trying to document this further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment