Skip to content

Commit

Permalink
Read flow token and url from command line params
Browse files Browse the repository at this point in the history
  • Loading branch information
cxcorp committed Jun 29, 2017
1 parent 557dfeb commit 108eedd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 48 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"private": true,
"dependencies": {
"bluebird": "3.5.0",
"dotenv": "4.0.0",
"jsdom": "11.0.0",
"node-fetch": "1.7.1"
"node-fetch": "1.7.1",
"yargs": "8.0.2"
}
}
8 changes: 1 addition & 7 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
const tokens = process.env.FLOWDOCK_FLOW_TOKENS
? process.env.FLOWDOCK_FLOW_TOKENS.split(',')
: []

module.exports = {
userAgent: 'Antell Lunch Menu Scraper (github.com/cxcorp/antell-lunchmenu-flowdock)',
antellMenuUrl: process.env.ANTELL_MENU_URL,
flowdockFlowTokens: tokens
userAgent: 'Antell Lunch Menu Scraper (github.com/cxcorp/antell-lunchmenu-flowdock)'
}
25 changes: 8 additions & 17 deletions src/flowdock-submitter.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
const Promise = require('bluebird')
const fetch = require('node-fetch')
const { userAgent, flowdockFlowTokens, antellMenuUrl } = require('./config')
const { userAgent } = require('./config')
const { getTodaysWeekdayInEnglish, formatDateRfc } = require('./util')

/**
* @param {any} menu
* @returns {Promise<string>} flowdock server response
*/
function submitMenuToFlowdock(menu) {
function submitMenuToFlowdock(menu, argv) {
const fields = menuToFields(menu)
const body = getMessageBody(fields)
const payload = {
//flow_token: flowdockFlowTokens, // injected below
flow_token: argv.flow,
event: 'activity',
title: `Today's lunch`,
body,
author: { name: 'Antell' },
external_thread_id: getThreadId(),
external_thread_id: getThreadId(argv.url),
thread: {
title: getThreadTitle(),
fields: fields,
external_url: antellMenuUrl
external_url: argv.url
},
tags: ['#lunch_menu']
tags: argv.tags
}

return Promise.all(flowdockFlowTokens.map((token, i) => {
const payloadWithToken = Object.assign(
{},
payload,
{ flow_token: token }
)
console.log(`Submitting lunch list to flow #${i}`)
return submitBodyToFlow(payloadWithToken)
}))
return submitBodyToFlow(payload)
}

function submitBodyToFlow(payload) {
Expand Down Expand Up @@ -66,7 +57,7 @@ function getMessageBody(fields) {
return `<span style="font-weight:bold">${fields[0].label}</span>${andMore}`
}

function getThreadId() {
function getThreadId(antellMenuUrl) {
// just generate some random ID so that the messages don't get grouped
return `${antellMenuUrl}?id=${new Date().getTime()}`
}
Expand Down
51 changes: 29 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
require('dotenv').config()
#!/usr/bin/env node
const fetch = require('node-fetch')
const { antellMenuUrl, flowdockFlowTokens, userAgent } = require('./config')
const yargs = require('yargs')
const { userAgent } = require('./config')
const { getTodaysWeekdayInFinnish } = require('./util')
const { parseLunchMenu } = require('./lunch-menu-parser')
const { submitMenuToFlowdock } = require('./flowdock-submitter')

exitIfMissingVars()
const yargv = yargs
.usage('$0 [args]')
.option('u', {
alias: 'url',
demandOption: true,
describe: 'url of the lunch list',
type: 'string'
})
.option('f', {
alias: 'flow',
demandOption: true,
describe: 'flow source token of the target flow',
type: 'string'
})
.option('t', {
alias: 'tags',
demandOption: false,
describe: 'tags of the message sent',
type: 'array'
})
.help()
.argv

getLunchMenu().then(weekMenu => {
getLunchMenu(yargv).then(weekMenu => {
const key = getTodaysWeekdayInFinnish()
if (key === 'Lauantai' || key === 'Sunnuntai') {
console.log('Weekend; not posting menu')
Expand All @@ -19,26 +41,11 @@ getLunchMenu().then(weekMenu => {
process.exit(1)
}

return submitMenuToFlowdock(menu)
return submitMenuToFlowdock(menu, yargv)
})

function exitIfMissingVars() {
let exit = false
if (flowdockFlowTokens.length < 1) {
exit = true
console.error('FLOWDOCK_FLOW_TOKENS are not set!')
}
if (!antellMenuUrl) {
exit = true
console.error('ANTELL_MENU_URL is not set!')
}
if (exit) {
process.exit(1)
}
}

function getLunchMenu() {
return fetch(antellMenuUrl, { headers: { 'User-Agent': userAgent } })
function getLunchMenu(argv) {
return fetch(argv.url, { headers: { 'User-Agent': userAgent } })
.then(body => body.text())
.then(parseLunchMenu)
}

0 comments on commit 108eedd

Please sign in to comment.