Skip to content

Commit

Permalink
Refactor and extract useful parts for the new handler
Browse files Browse the repository at this point in the history
  • Loading branch information
daemonsy committed Jun 30, 2017
1 parent 54e83a5 commit 74185d7
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 44 deletions.
14 changes: 14 additions & 0 deletions handlers/store-favorite-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fetchMTAStatus = require('../services/fetch-mta-status.js');
const closestLineMatcher = require('../utilities/closest-line-matcher.js');

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

if(!nameGroup) { 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}`);
});
}
42 changes: 14 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@ const uuidv4 = require("uuid/v4");

var env = process.env.NODE_ENV || 'development';
const logRequests = process.env.LOG_REQUESTS === 'true';
const applicationId = process.env.APPLICATION_ID;

if(env !== 'production') {
require('dotenv').load({ path: '.env.' + env });
}

var applicationId = process.env.APPLICATION_ID;
var mtaStatusURL = process.env.MTA_STATUS_URL;
const _ = require('lodash');
const Alexa = require('alexa-sdk');

var _ = require('lodash');
var levenshtein = require('fast-levenshtein');
var Alexa = require('alexa-sdk');
var currentMTAStatus = require('./current-mta-status.js');
var statusToSpeech = require('./status-to-speech.js');
const statusToSpeech = require('./services/status-to-speech.js');
const fetchMTAStatus = require('./services/fetch-mta-status.js');
const closestLineMatcher = require('./utilities/closest-line-matcher.js');

var fetchStatus = function(callback) {
return fetch(mtaStatusURL).then(function(response) {
return response.text()
}).then(function(body) {
currentMTAStatus(body, callback);
});
};
// Handlers
const storeFavoriteLineHandler = require('./handlers/store-favorite-line.js');

const affectedServiceStatusesBuilder = (statuses) => {
let withServiceIssues = status => status.status !== 'GOOD SERVICE';
Expand All @@ -40,7 +34,7 @@ const affectedServiceStatusesBuilder = (statuses) => {
};

var fullStatusUpdateHandler = function() {
fetchStatus(statuses => {
fetchMTAStatus(statuses => {
let affectedServiceStatuses = affectedServiceStatusesBuilder(statuses);

if(affectedServiceStatuses.length === 0) {
Expand All @@ -56,22 +50,13 @@ var handlers = {
statusOfLine: function () {
var self = this;
var nameGroup = self.event.request.intent.slots.subwayLineOrGroup.value;
var closestStatus = null;
var badQueryResponse = function() {
self.emit(':tell', "Sorry, I didn't hear a subway line I understand");
}

if(nameGroup) {
fetchStatus(function(statuses) {
if(nameGroup.length > 1) {
closestStatus = _.minBy(statuses, function(status) {
return levenshtein.get(status.nameGroup, nameGroup.toUpperCase());
});
} else {
closestStatus = _.find(statuses, function(status) {
return status.nameGroup.search(nameGroup.toUpperCase()) !== -1;
});
}
fetchMTAStatus(function(statuses) {
let closestStatus = closestLineMatcher(statuses, 'nameGroup', nameGroup);

if(closestStatus) {
if(closestStatus.description) {
Expand All @@ -90,11 +75,12 @@ var handlers = {
},

fullStatusUpdate: fullStatusUpdateHandler,
Unhandled: fullStatusUpdateHandler
storeFavoriteLine: storeFavoriteLineHandler,
Unhandled: fullStatusUpdateHandler,
};

exports.flashBriefingHandler = (event, context, callback) => {
fetchStatus(statuses => {
fetchMTAStatus(statuses => {
let affectedServiceStatuses = affectedServiceStatusesBuilder(statuses);
let message;

Expand Down
4 changes: 2 additions & 2 deletions current-mta-status.js → parsers/current-mta-status.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var parseXML = require('xml2js').parseString;
var serviceDetailsCleaner = require('./service-details-cleaner');
const parseXML = require('xml2js').parseString;
const serviceDetailsCleaner = require('./service-details-cleaner');

module.exports = function(mtaStatusXML, callback) {
parseXML(mtaStatusXML, { normalize: true, trim: true }, function(error, results) {
Expand Down
12 changes: 12 additions & 0 deletions parsers/service-details-cleaner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const sanitize = require('sanitize-html');
const MATCHERS = {
br: /<br\/?>\s*/g
}

module.exports = function(detailsText) {
let text = detailsText.replace(MATCHERS.br, '\n'); // BRs to newline

return sanitize(text, {
allowedTags: []
});
};
12 changes: 0 additions & 12 deletions service-details-cleaner.js

This file was deleted.

8 changes: 8 additions & 0 deletions services/fetch-mta-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const MTA_STATUS_URL = process.env.MTA_STATUS_URL;
const currentMTAStatus = require('../parsers/current-mta-status.js');

module.exports = function(callback) {
return fetch(MTA_STATUS_URL)
.then(response => response.text())
.then(body => currentMTAStatus(body, callback));
};
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/service-details-cleaner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import fs from 'fs';
import { parseString } from 'xml2js'

import { default as serviceDetailsCleaner } from '../service-details-cleaner.js';
import { default as serviceDetailsCleaner } from '../parsers/service-details-cleaner.js';

test('removes all linebreaks', t => {
t.plan(1);
Expand Down
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 '../status-to-speech.js';
import statusToSpeech from '../services/status-to-speech.js';

test('Given a service status, it builds the right speech"', t => {
t.plan(4);
Expand Down
17 changes: 17 additions & 0 deletions utilities/closest-line-matcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const _ = require('lodash');
const levenshtein = require('fast-levenshtein');

module.exports = function(statuses, key, potentialValue) {
let closetStatus;

if(potentialValue.length > 1) {
closestStatus = _.minBy(statuses, function(status) {
return levenshtein.get(status[key], potentialValue.toUpperCase());
});
} else {
closestStatus = _.find(statuses, function(status) {
return status[key].search(potentialValue.toUpperCase()) !== -1;
});
}
return closestStatus;
}
6 changes: 6 additions & 0 deletions utterances.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ 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}
removeFavoriteLine to forget the {subwayLineOrGroup}
resetFavoriteLine to forget all my tracked lines
checkFavoriteLines to check my favorite lines
checkFavoriteLines what is the status of my favorite lines?
checkFavoriteLines to check my commute

0 comments on commit 74185d7

Please sign in to comment.