Created
August 9, 2016 05:15
-
-
Save drzax/0f87f8d7c8ec93c553e6dd733d96e8d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ [*] who is (@candidate) [*] | |
- <call>candidate-info:who <star></call> | |
+ [*] who (@candidate) is [*] | |
@ who is <star> | |
+ [*] what [*] about [*] (@candidate) [*] | |
@ who is <star> | |
+ [*] what [*] (@candidate) [*] (electorate|seat) [*] | |
@ who is <star> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const id = 'candidate-info'; | |
const merge = require('merge'); | |
const qs = require('querystring'); | |
const fetch = require('../../fetch'); | |
const utils = require('../../utils'); | |
const mu = require('mustache'); | |
const path = require('path'); | |
const log = require(path.join(__dirname,'..','..','logger')).child({component: id}); | |
const senate = ['sqld','snsw','svic','stas','ssa','swa','sact','snt']; | |
module.exports = constructor; | |
function constructor(_opts) { | |
var opts, candidatesPromise, templates; | |
// Get options from the environment by default | |
opts = merge({ | |
morphKey: process.env.MORPHIO_KEY | |
}, _opts); | |
candidatesPromise = fetchCandidatesData().catch((err)=> { | |
log.warn({err:err}, 'Error fetching candidates'); | |
}); | |
// Load the templates into memory. | |
templates = fetch.templates(path.join(__dirname,'templates.txt')).then((templates) => { | |
log.info({templates: templates}, 'Templates loaded'); | |
return templates; | |
}).catch((err) => { | |
log.error({err:err}, 'Error loading templates'); | |
}); | |
// `opts.brain` is a promise | |
opts.brain.then((brain)=>{ | |
// Define the list of functions this capability can perform. | |
var subroutines = { | |
who: who | |
}; | |
// Initialise the capability by attaching all the sub-routines here. | |
for (var name in subroutines) { | |
brain.setSubroutine(id+':'+name, subroutines[name]); | |
} | |
}); | |
function who(rive, args) { | |
var data = this; | |
return new rive.Promise((resolve, reject)=>{ | |
candidatesPromise.then((candidates) => { | |
var candidate; | |
candidate = candidates.find((candidate) => { | |
return candidate.name.toLowerCase() === args[0].toLowerCase(); | |
}); | |
if (!candidate) { | |
return reject(new Error('Candidate not found')); | |
} | |
utils.templateByKey(templates, 'who-is').then((template)=>{ | |
var party; | |
party = candidate.partyName.replace('Independent', 'independent').replace('The Greens', 'Greens'); | |
if (senate.indexOf(candidate.electorateCode) > -1) { | |
party = (party === 'independent') ? `an ${party} senate` : `a ${party} senate`; | |
} else { | |
party = (party === 'independent') ? `an ${party}` : `the ${party}`; | |
} | |
data.response = { | |
text: mu.render(template, { | |
electorateName: candidate.electorateName, | |
candidateName: candidate.name, | |
guideUrl: `http://www.abc.net.au/news/federal-election-2016/guide/${candidate.electorateCode}/`, | |
partyName: party | |
}), | |
fn:`${id}:who` | |
}; | |
// In this implementation we often want to return an empty string | |
// It's not used as the actual response to the user. Set `data.response` | |
// for that. | |
resolve(''); | |
}); | |
}); | |
}); | |
} | |
function fetchCandidatesData() { | |
return fetch.json('https://api.morph.io/drzax/morph-australian-federal-election-candidates-2016/data.json?'+qs.stringify({ | |
key: opts.morphKey, | |
query: "SELECT * FROM data" | |
})).then((data) => { | |
log.info('Candidates data fetched'); | |
return data; | |
}).catch((err) => { | |
log.error({err:err}); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
who-is {{&candidateName}} is {{&partyName}} candidate for {{&electorateName}} See more: {{&guideUrl}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment