Skip to content

Commit

Permalink
Start building the logic of storing commute
Browse files Browse the repository at this point in the history
  • Loading branch information
daemonsy committed Jul 8, 2017
1 parent ae5dad8 commit b316cb1
Show file tree
Hide file tree
Showing 13 changed files with 725 additions and 17 deletions.
494 changes: 494 additions & 0 deletions assets/mta/Stations.csv

Large diffs are not rendered by default.

Binary file added assets/mta/google_transit.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ dependencies:
- yarn run dynamodb:download
- yarn run dynamodb:start:
background: true
- sleep 10

override:
- yarn install


cache_directories:
- ~/.yarn
- ~/.cache/yarn
Expand Down
1 change: 1 addition & 0 deletions data/slots/subway-lines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":"1","name":{"value":"1","synonyms":[]}},{"id":"2","name":{"value":"2","synonyms":[]}},{"id":"3","name":{"value":"3","synonyms":[]}},{"id":"4","name":{"value":"4","synonyms":[]}},{"id":"5","name":{"value":"5","synonyms":[]}},{"id":"6","name":{"value":"6","synonyms":[]}},{"id":"7","name":{"value":"7","synonyms":[]}},{"id":"A","name":{"value":"A","synonyms":["alpha"]}},{"id":"C","name":{"value":"C","synonyms":["charlie"]}},{"id":"E","name":{"value":"E","synonyms":["echo"]}},{"id":"N","name":{"value":"N","synonyms":["november"]}},{"id":"Q","name":{"value":"Q","synonyms":["quebec"]}},{"id":"R","name":{"value":"R","synonyms":["romeo"]}},{"id":"W","name":{"value":"W","synonyms":["whiskey"]}},{"id":"B","name":{"value":"B","synonyms":["bravo"]}},{"id":"D","name":{"value":"D","synonyms":["delta"]}},{"id":"F","name":{"value":"F","synonyms":["foxtrot"]}},{"id":"M","name":{"value":"M","synonyms":["mike"]}},{"id":"L","name":{"value":"L","synonyms":["lima"]}},{"id":"G","name":{"value":"G","synonyms":["golf"]}},{"id":"S","name":{"value":"S","synonyms":["sierra","singapore"]}},{"id":"J","name":{"value":"J","synonyms":["juliet"]}},{"id":"Z","name":{"value":"Z","synonyms":["zulu"]}},{"id":"SIR","name":{"value":"SIR","synonyms":[]}}]
10 changes: 10 additions & 0 deletions handlers/store-commute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function() {
let { subwayStation, subwayDirection, subwayLine } = this.event.request.intent.slots;

if(this.event.request.dialogState === 'COMPLETED') {
this.emit(':tell', 'thank you');
} else {
console.log(JSON.stringify(this.event));
this.emit(':delegate');
}
}
15 changes: 3 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const fetchMTAStatus = require('./services/fetch-mta-status.js');
const storeFavoriteLineHandler = require('./handlers/store-favorite-line.js');
const checkFavoriteLinesHandler = require('./handlers/check-favorite-lines.js');
const checkFavoriteLinesStatusHandler = require('./handlers/check-favorite-lines-status.js');
const storeCommuteHandler = require('./handlers/store-commute.js');

// Utilities
const closestLineMatcher = require('./utilities/closest-line-matcher.js');
Expand Down Expand Up @@ -80,17 +81,7 @@ var handlers = {
checkFavoriteLines: checkFavoriteLinesHandler,
checkFavoriteLinesStatus: checkFavoriteLinesStatusHandler,

storeCommute() {
if(this.event.request.dialogState === 'COMPLETED') {
let { subwayStation, subwayDirection, subwayLine } = this.events.request.intent.slots;
subwayStationVerifier()
this.emit(':tell', 'thank you');
} else {
console.log(JSON.stringify(this.event));
this.emit(':delegate');
}
},

storeCommute: storeCommuteHandler,
Unhandled: fullStatusUpdateHandler
};

Expand Down Expand Up @@ -120,7 +111,7 @@ exports.flashBriefingHandler = (event, context, callback) => {
};

exports.handler = function(event, context, callback){
console.log(event);
if(logRequests) console.log(event);

var alexa = Alexa.handler(event, context);
alexa.appId = applicationId;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"author": "Damon Aw <[email protected]>",
"scripts": {
"data:convert_csv_stops_to_json": "csvtojson ./assets/mta/Stations.csv > ./values/subway-stops.json",
"data:convert_stations_to_slot": "node ./scripts/clean-station-names.js > ./data/slots/station-names.json",
"data:slots:generate_station_names": "node ./scripts/clean-station-names.js > ./data/slots/station-names.json",
"data:slots:generate_subway_lines": "node ./scripts/generate-subway-lines.js > ./data/slots/subway-lines.json",
"dynamodb:download": "mkdir -p downloads && mkdir -p dynamodb_local && wget -N https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz -P ./downloads && tar -xvf ./downloads/dynamodb_local_latest.tar.gz -C ./dynamodb_local",
"dynamodb:start": "java -Djava.library.path=./dynamodb_local/DynamoDBLocal_lib -jar ./dynamodb_local/DynamoDBLocal.jar -sharedDb -inMemory",
"release": "rm -f subway.zip && yarn --production && zip -r subway.zip . -x '*.git*' -x 'tests/*' -x 'downloads/*' -x 'assets/*' -x 'dynamodb_local/*'",
Expand Down
5 changes: 2 additions & 3 deletions scripts/clean-station-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ process.stdout.write(
_(subwayStops)
.map(stopData => stopData["stopName"])
.uniq()
.map(stopName => {
return ({ id: null, name: { value: stopName, synonyms: [] }})
}).value()
.map(stopName => ({ id: null, name: { value: stopName, synonyms: [] }}))
.value()
)
);
37 changes: 37 additions & 0 deletions scripts/generate-subway-lines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const _ = require('lodash');

const subwayLines = [
'1', '2', '3',
'4', '5', '6',
'7',
'A', 'C', 'E',
'N', 'Q', 'R', 'W',
'B', 'D', 'F', 'M',
'L', 'G',
'S',
'J', 'Z',
'SIR'
];

const synonyms = {
'A': ['alpha'],
'C': ['charlie'],
'E': ['echo'],
'N': ['november'],
'Q': ['quebec'],
'R': ['romeo'],
'W': ['whiskey'],
'B': ['bravo'],
'D': ['delta'],
'F': ['foxtrot'],
'M': ['mike'],
'L': ['lima'],
'G': ['golf'],
'S': ['sierra', 'singapore'],
'J': ['juliet'],
'Z': ['zulu']
};

process.stdout.write(
JSON.stringify(subwayLines.map(line => ({ id: line, name: { value: line, synonyms: synonyms[line] || [] }})))
);
59 changes: 59 additions & 0 deletions tests/fixtures/events/store-commute-start.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"version": "1.0",
"session": {
"new": true,
"sessionId": "amzn1.echo-api.session.02c67291-9e9a-4ca5-bef8-c19b642426c4",
"application": {
"applicationId": "amzn1.ask.skill.4fe3808f-fda4-4bfc-8e53-07b9f3b3baae"
},
"user": {
"userId": "amzn1.ask.account.AG7TBVHS7QUDLBU6N65BSUJMZZTQ6CFD4YFQELQRLXB3M4UV64MQ7RJ5CPWXYALQ3QJ665TJ7OIF3ZT4RMC5NQRPH5OWB4TMQTC46REKNEYDBLUN6OJ7OLXYHGEUOLZYRA3CQGGSLOSOFQTEVG4DNKVRN3NIVPK2YYUCAFJ64ODXBDNE5YNAXDX7GQATDLNGZR7G5KMDNTHZR2I"
},
"attributes": {}
},
"context": {
"AudioPlayer": {
"playerActivity": "STOPPED"
},
"System": {
"application": {
"applicationId": "amzn1.ask.skill.4fe3808f-fda4-4bfc-8e53-07b9f3b3baae"
},
"user": {
"userId": "amzn1.ask.account.AG7TBVHS7QUDLBU6N65BSUJMZZTQ6CFD4YFQELQRLXB3M4UV64MQ7RJ5CPWXYALQ3QJ665TJ7OIF3ZT4RMC5NQRPH5OWB4TMQTC46REKNEYDBLUN6OJ7OLXYHGEUOLZYRA3CQGGSLOSOFQTEVG4DNKVRN3NIVPK2YYUCAFJ64ODXBDNE5YNAXDX7GQATDLNGZR7G5KMDNTHZR2I"
},
"device": {
"deviceId": "amzn1.ask.device.AEGQ5USV2TVZTMTG7AXEFTAK5FA4GCTGJWPKJJYMWCFA3E4ADMSX6V7UYVNDBFNUVE2FFWHIZEUBFGWELKJLN243XVK7MURJXUD5O7HFHWAQNFDJAWPSELUAO3NX2RECP6QEZEICXGE2ULYJFUYGSWSNEFRA",
"supportedInterfaces": {
"AudioPlayer": {}
}
},
"apiEndpoint": "https://api.amazonalexa.com"
}
},
"request": {
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.f6c656ec-0bc0-4fc6-bb89-91747b4718c7",
"timestamp": "2017-07-07T21:55:46Z",
"locale": "en-US",
"intent": {
"name": "storeCommute",
"confirmationStatus": "NONE",
"slots": {
"subwayStation": {
"name": "subwayStation",
"confirmationStatus": "NONE"
},
"subwayDirection": {
"name": "subwayDirection",
"confirmationStatus": "NONE"
},
"subwayLine": {
"name": "subwayLine",
"confirmationStatus": "NONE"
}
}
},
"dialogState": "STARTED"
}
}
78 changes: 78 additions & 0 deletions tests/fixtures/events/store-commute-subway-line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"version": "1.0",
"session": {
"new": false,
"sessionId": "amzn1.echo-api.session.02c67291-9e9a-4ca5-bef8-c19b642426c4",
"application": {
"applicationId": "amzn1.ask.skill.4fe3808f-fda4-4bfc-8e53-07b9f3b3baae"
},
"user": {
"userId": "amzn1.ask.account.AG7TBVHS7QUDLBU6N65BSUJMZZTQ6CFD4YFQELQRLXB3M4UV64MQ7RJ5CPWXYALQ3QJ665TJ7OIF3ZT4RMC5NQRPH5OWB4TMQTC46REKNEYDBLUN6OJ7OLXYHGEUOLZYRA3CQGGSLOSOFQTEVG4DNKVRN3NIVPK2YYUCAFJ64ODXBDNE5YNAXDX7GQATDLNGZR7G5KMDNTHZR2I"
},
"attributes": {}
},
"context": {
"AudioPlayer": {
"playerActivity": "STOPPED"
},
"System": {
"application": {
"applicationId": "amzn1.ask.skill.4fe3808f-fda4-4bfc-8e53-07b9f3b3baae"
},
"user": {
"userId": "amzn1.ask.account.AG7TBVHS7QUDLBU6N65BSUJMZZTQ6CFD4YFQELQRLXB3M4UV64MQ7RJ5CPWXYALQ3QJ665TJ7OIF3ZT4RMC5NQRPH5OWB4TMQTC46REKNEYDBLUN6OJ7OLXYHGEUOLZYRA3CQGGSLOSOFQTEVG4DNKVRN3NIVPK2YYUCAFJ64ODXBDNE5YNAXDX7GQATDLNGZR7G5KMDNTHZR2I"
},
"device": {
"deviceId": "amzn1.ask.device.AEGQ5USV2TVZTMTG7AXEFTAK5FA4GCTGJWPKJJYMWCFA3E4ADMSX6V7UYVNDBFNUVE2FFWHIZEUBFGWELKJLN243XVK7MURJXUD5O7HFHWAQNFDJAWPSELUAO3NX2RECP6QEZEICXGE2ULYJFUYGSWSNEFRA",
"supportedInterfaces": {
"AudioPlayer": {}
}
},
"apiEndpoint": "https://api.amazonalexa.com"
}
},
"request": {
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.ff88ca27-f12f-4ab1-9cc2-02b52cea1ec7",
"timestamp": "2017-07-07T21:55:59Z",
"locale": "en-US",
"intent": {
"name": "storeCommute",
"confirmationStatus": "NONE",
"slots": {
"subwayStation": {
"name": "subwayStation",
"confirmationStatus": "NONE"
},
"subwayDirection": {
"name": "subwayDirection",
"confirmationStatus": "NONE"
},
"subwayLine": {
"name": "subwayLine",
"value": "R",
"resolutions": {
"resolutionsPerAuthority": [
{
"authority": "amzn1.er-authority.echo-sdk.amzn1.ask.skill.4fe3808f-fda4-4bfc-8e53-07b9f3b3baae.SUBWAY_LINE",
"status": {
"code": "ER_SUCCESS_MATCH"
},
"values": [
{
"value": {
"name": "R",
"id": "e1e1d3d40573127e9ee0480caf1283d6"
}
}
]
}
]
},
"confirmationStatus": "NONE"
}
}
},
"dialogState": "IN_PROGRESS"
}
}
33 changes: 33 additions & 0 deletions tests/handlers/store-commute-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import test from 'ava';

import LambdaTester from 'lambda-tester';
import getTestEvent from '../helpers/get-test-event.js';

import storeCommute from '../../handlers/store-commute.js';
import createAlexaHandler from '../helpers/create-alexa-handler.js';

test(`Handling the start of the dialog`, async t => {
t.plan(1);

const event = getTestEvent('store-commute-start');
const alexaHandler = createAlexaHandler({ storeCommute });

const result = await LambdaTester(alexaHandler)
.event(event)
.expectSucceed(r => r);

t.is(result.response.directives[0].type, "Dialog.Delegate")
});

test(`collecting subwayLine and a valid input is given`, async t => {
t.plan(1);

const event = getTestEvent('store-commute-subway-line');
const alexaHandler = createAlexaHandler({ storeCommute });

const result = await LambdaTester(alexaHandler)
.event(event)
.expectSucceed(r => r);

t.is(result.response.directives[0].type, "Dialog.Delegate");
});
5 changes: 5 additions & 0 deletions tests/helpers/get-test-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('fs');

module.exports = (eventName) => JSON.parse(
fs.readFileSync(`${process.cwd()}/tests/fixtures/events/${eventName}.json`)
);

0 comments on commit b316cb1

Please sign in to comment.