Skip to content

Commit

Permalink
[WIP] Store the favorite line uttered by the user
Browse files Browse the repository at this point in the history
  • Loading branch information
daemonsy committed Jun 30, 2017
1 parent 3b57f6a commit 1c600f8
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 18 deletions.
2 changes: 2 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies:
echo "The correct version of Yarn is already installed."
fi
- yarn run dynamodb:download
- yarn run dynamodb:start:
background: true

override:
- yarn install
Expand Down
8 changes: 6 additions & 2 deletions handlers/store-favorite-line.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const fetchMTAStatus = require('../services/fetch-mta-status.js');

const closestLineMatcher = require('../utilities/closest-line-matcher.js');
const literalize = require('../speech-helpers/literalize.js');

module.exports = function() {
module.exports = function(alexa) {
let heardNameGroup = this.event.request.intent.slots.subwayLineOrGroup.value;

if(!heardNameGroup) { this.emit(':tell', "Sorry, I didn't hear a subway line or group I recognized") };

fetchMTAStatus(statuses => {
let closestLine = closestLineMatcher(statuses, 'nameGroup', heardNameGroup);

this.emit(':tell', `Your favorite line is ${closestLine.nameGroup}`);
this.attributes["trackedTrainLines"] = (this.attributes["trackedTrainLines"] || []).concat([closestLine]);

this.emit(':tell', `Your favorite line is ${literalize(closestLine.nameGroup)}`);
});
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ const applicationId = process.env.APPLICATION_ID;
const _ = require('lodash');
const Alexa = require('alexa-sdk');

const statusToSpeech = require('./services/status-to-speech.js');
const statusToSpeech = require('./speech-helpers/status-to-speech.js');
const fetchMTAStatus = require('./services/fetch-mta-status.js');
const closestLineMatcher = require('./utilities/closest-line-matcher.js');

// Handlers
const storeFavoriteLineHandler = require('./handlers/store-favorite-line.js');

if(env === 'test') { require('dotenv').config(); };

const affectedServiceStatusesBuilder = (statuses) => {
let withServiceIssues = status => status.status !== 'GOOD SERVICE';

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"ava": {
"files": [
"tests/**/*-test.js",
"tests/*-test.js"
],
"timeout": "6000",
Expand All @@ -30,6 +31,7 @@
"license": "MIT",
"devDependencies": {
"ava": "^0.17.0",
"dotenv": "^4.0.0",
"fetch-mock": "^5.8.0",
"lambda-tester": "^2.6.1"
}
Expand Down
1 change: 1 addition & 0 deletions speech-helpers/literalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = (nameGroup) => nameGroup.split("").join("-")
File renamed without changes.
29 changes: 29 additions & 0 deletions tests/fixtures/events/store-favorite-line-ace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"session": {
"sessionId": "SessionId.fea02aa4-0e49-4f22-ab37-d818ee1c8f32",
"application": {
"applicationId": "amzn1.ask.skill.4fe3808f-fda4-4bfc-8e53-07b9f3b3baae"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.AFAKUB5FUTW5AXKNQ7ZVBGQWW7EKCO2V5XIW447VAUBZM2YYKTKPXYNOSCR4DT2VDIBJ6SG2LSY3SESF56RGJNAF77EG547QF3TDYRLBFXJFPZC4SZ5PMWQRZ64B3Q22LYNN7R6COCX6WWALHWF6SHT234QXMD23SWTLZXKDAV4NILWHGRMDOJJQ2VQLO6RL4UTX6NCYQUH5PHY"
},
"new": true
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.648ced7d-3e26-4372-9b4f-f89d23a24c6a",
"locale": "en-US",
"timestamp": "2017-06-30T02:46:44Z",
"intent": {
"name": "storeFavoriteLine",
"slots": {
"subwayLineOrGroup": {
"name": "subwayLineOrGroup",
"value": "ace"
}
}
}
},
"version": "1.0"
}
29 changes: 29 additions & 0 deletions tests/fixtures/events/store-favorite-line-seven.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"session": {
"sessionId": "SessionId.fea02aa4-0e49-4f22-ab37-d818ee1c8f32",
"application": {
"applicationId": "amzn1.ask.skill.4fe3808f-fda4-4bfc-8e53-07b9f3b3baae"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.AFAKUB5FUTW5AXKNQ7ZVBGQWW7EKCO2V5XIW447VAUBZM2YYKTKPXYNOSCR4DT2VDIBJ6SG2LSY3SESF56RGJNAF77EG547QF3TDYRLBFXJFPZC4SZ5PMWQRZ64B3Q22LYNN7R6COCX6WWALHWF6SHT234QXMD23SWTLZXKDAV4NILWHGRMDOJJQ2VQLO6RL4UTX6NCYQUH5PHY"
},
"new": false
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.cb4f5ce6-d838-4898-9caf-7789aedda93e",
"locale": "en-US",
"timestamp": "2017-06-30T02:47:32Z",
"intent": {
"name": "storeFavoriteLine",
"slots": {
"subwayLineOrGroup": {
"name": "subwayLineOrGroup",
"value": "7"
}
}
}
},
"version": "1.0"
}
26 changes: 26 additions & 0 deletions tests/handlers/store-favorite-line-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'fs';
import test from 'ava';

import LambdaTester from 'lambda-tester';
import fetchMock from 'fetch-mock';

import storeFavoriteLine from '../../handlers/store-favorite-line.js';
import createAlexaHandler from '../helpers/create-alexa-handler.js';

import dynamoDBClient from "../helpers/dynamo-db-client.js";

test.cb('handling "tell NYC subway to track the ACE train"', t => {
t.plan(0);

const event = JSON.parse(fs.readFileSync(process.cwd() + '/tests/fixtures/events/store-favorite-line-ace.json'));
fetchMock.once(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status.xml', 'utf-8'));

const alexaHandler = createAlexaHandler({ storeFavoriteLine })

LambdaTester(alexaHandler)
.event(event)
.expectSucceed(r => {
t.end();
fetchMock.restore();
});
});
15 changes: 15 additions & 0 deletions tests/helpers/create-alexa-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Alexa = require('alexa-sdk');
const dynamoDBClient = require('./dynamo-db-client.js');

module.exports = function(handlers) {
return (event, context, callback) => {
var alexa = Alexa.handler(event, context);
alexa.appId = process.env.APPLICATION_ID;

alexa.dynamoDBClient = dynamoDBClient;
alexa.dynamoDBTableName = 'nyc-subway-test';

alexa.registerHandlers(handlers);
alexa.execute();
};
}
7 changes: 7 additions & 0 deletions tests/helpers/dynamo-db-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const AWS = require('aws-sdk');

module.exports = new AWS.DynamoDB({
apiVersion: '2012-08-10',
endpoint: "http://localhost:8000",
region: "us-east-1"
});
14 changes: 1 addition & 13 deletions tests/index-test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import fs from 'fs';
import { spawn } from 'child_process';
import dynamoDBClient from "./helpers/dynamo-db-client.js";

import test from 'ava';
import LambdaTester from 'lambda-tester';
import fetchMock from 'fetch-mock';

import { handler, flashBriefingHandler } from '../index.js';

var dynamo;

test.cb.before(t => {
dynamo = spawn('yarn run dynamodb:start', { shell: true, timeout: 3000 });
setTimeout(t.end, 3000);
})

test.cb.after.always(t => {
dynamo.on('exit', () => t.end());
setTimeout(t.end, 3000);
dynamo.kill('SIGINT');
})

test.serial('flashBriefingHandler', async t => {
t.plan(3);

Expand Down
7 changes: 7 additions & 0 deletions tests/literalize-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import test from 'ava';

import literalize from "../speech-helpers/literalize.js";

test('literalize adds dashes across a speech token', t => {
t.is(literalize("ACE"), "A-C-E");
});
2 changes: 1 addition & 1 deletion tests/status-to-speech-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import statusToSpeech from '../services/status-to-speech.js';
import statusToSpeech from '../speech-helpers/status-to-speech.js';

test('Given a service status, it builds the right speech"', t => {
t.plan(4);
Expand Down
5 changes: 4 additions & 1 deletion utterances.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ statusOfLine what is the status of {subwayLineOrGroup}
fullStatusUpdate for a service update
fullStatusUpdate for a full service update
fullStatusUpdate for an update
storeFavoriteLine to track the {subwayLineOrGroup}
storeFavoriteLine to track the {subwayLineOrGroup} line
storeFavoriteLine my favorite line is {subwayLineOrGroup}
storeFavoriteLine to add {subwayLineOrGroup} to my favorite line

removeFavoriteLine to forget the {subwayLineOrGroup}
resetFavoriteLine to forget all my tracked lines
checkFavoriteLines to check my favorite lines
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,10 @@ dotenv@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-2.0.0.tgz#bd759c357aaa70365e01c96b7b0bec08a6e0d949"

dotenv@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-4.0.0.tgz#864ef1379aced55ce6f95debecdce179f7a0cd1d"

duplexer2@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.1.4.tgz#8b12dab878c0d69e3e7891051662a32fc6bddcc1"
Expand Down

0 comments on commit 1c600f8

Please sign in to comment.