Skip to content

Commit

Permalink
Tests for create-server and new skill.lambda handler (#20)
Browse files Browse the repository at this point in the history
* Adding tests for the create-server function

* Adding a helper for lambda

* Updating the lambda function

* Updating documentation and examples on the lambda handler

* Update create-server.spec.js
  • Loading branch information
armonge authored and heneryville committed Mar 20, 2017
1 parent e72f53b commit 7e20d9d
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 189 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ skill.onIntent('LaunchIntent', (event) => {
});

// lambda handler
exports.handler = function handler(event, context, callback) {
return skill.execute(event, context)
.then(result => callback(null, result))
.catch(callback);
}
exports.handler = skill.lambda();

```

Expand Down
8 changes: 2 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ Instantiating a StateMachineSkill requires a configuration specifying your :ref:
Responding to alexa events
-----------------------------

Once you have your skill configured responding to events is as simple as calling the :js:func:`skill.execute <Voxa.execute>` method
Once you have your skill configured responding to events is as simple as calling the :js:func:`skill.lambda <Voxa.lambda>` method

.. code-block:: javascript
const skill = require('./MainStateMachine');
exports.handler = function handler(event, context) {
skill.execute(event, context)
.then(context.succeed)
.catch(context.fail);
};
exports.handler = skill.lambda();
Using the development server
-----------------------------
Expand Down
21 changes: 15 additions & 6 deletions docs/statemachine-skill.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ Voxa
const skill = new Voxa({ Model, variables, views, appIds });
.. js:function:: Voxa.lambda()


:returns: A lambda handler that will call your :js:func:`skill.execute <Voxa.execute>` method

.. code-block:: javascript
exports.handler = skill.lambda();
.. js:function:: Voxa.execute(event)

The main entry point for the Skill execution

:param event: The event sent by alexa.
Expand All @@ -24,8 +33,8 @@ Voxa
.. code-block:: javascript
skill.execute(event, context)
.then(context.succeed)
.catch(context.fail);
.then(result => callback(null, result))
.catch(callback);
.. js:function:: Voxa.onState(stateName, handler)

Expand All @@ -50,7 +59,7 @@ Voxa

A shortcut for definining state controllers that map directly to an intent

:param string intentName: The name of the intent
:param string intentName: The name of the intent
:param function/object handler: The controller to handle the state
:returns: An object or a promise that resolves to an object that specifies a transition to another state and/or a view to render

Expand Down Expand Up @@ -136,7 +145,7 @@ Voxa
.. js:function:: Voxa.onSessionEnded(callback, [atLast])

Adds a callback to the ``onSessionEnded`` event, this is called for every ``SessionEndedRequest`` or when the skill returns a transition to a state where ``isTerminal === true``, normally this is a transition to the ``die`` state. You would normally use this to track analytics



.. js:function:: Voxa.onSystem.ExceptionEncountered(callback, [atLast])
Expand Down Expand Up @@ -193,7 +202,7 @@ Playback Controller handlers
Handle events from the `AudioPlayer interface <https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/custom-audioplayer-interface-reference#requests>`_

.. js:function:: audioPlayerCallback(alexaEvent, reply)

All audio player middleware callbacks get a :ref:`alexa event <alexa-event>` and a :ref:`reply <reply>` object

:param AlexaEvent alexaEvent: The :ref:`alexa event <alexa-event>` sent by Alexa
Expand Down
12 changes: 10 additions & 2 deletions lib/AlexaSkill.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const _ = require('lodash');
const debug = require('debug')('voxa');
const UnknownRequestType = require('./Errors').UnkownRequestType;
const capitalize = require('capitalize');
const startServer = require('./start-server');
const createServer = require('./create-server');

class AlexaSkill {
constructor(config) {
Expand All @@ -28,7 +28,9 @@ class AlexaSkill {
}

startServer(port) {
startServer(this, port);
createServer(this).listen(port, () => {
console.log(`Listening on port ${port}`);
});
}

/*
Expand Down Expand Up @@ -74,6 +76,12 @@ class AlexaSkill {
});
}

lambda() {
return (event, context, callback) => this.execute(event, context)
.then(result => callback(null, result))
.catch(callback);
}

execute(event) {
debug('Received new event: %s', JSON.stringify(event));
return Promise.try(() => {
Expand Down
12 changes: 3 additions & 9 deletions lib/start-server.js → lib/create-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

const http = require('http');

function startServer(skill, port) {
port = port || 3000;

const server = http.createServer((req, res) => {
function createServer(skill) {
return http.createServer((req, res) => {
if (req.method !== 'POST') {
res.writeHead(404);
return res.end();
Expand All @@ -27,10 +25,6 @@ function startServer(skill, port) {

return res.writeHead(200, { 'Content-Type': 'application/json' });
});

server.listen(port, () => {
console.log(`Listening on port ${port}`);
});
}

module.exports = startServer;
module.exports = createServer;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"istanbul": "^0.4.5",
"mocha": "3.2.0",
"mocha-jenkins-reporter": "0.3.7",
"portfinder": "^1.0.13",
"simple-mock": "^0.7.3"
},
"bugs": {
Expand Down
4 changes: 2 additions & 2 deletions samples/accountLinking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
"voxa": "^2.0.0"
},
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^3.15.0",
"eslint-config-airbnb-base": "^11.1.0",
"eslint-plugin-import": "^2.2.0",
"chai": "^3.5.0",
"gulp": "^3.9.1",
"gulp-s3-upload": "1.6.4",
"gulp-nodemon": "^2.0.7",
"gulp-s3-upload": "1.6.4",
"mocha": "^3.2.0"
},
"directories": {
Expand Down
1 change: 1 addition & 0 deletions samples/accountLinking/skill/MainStateMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ const states = require('./states');

const skill = new Voxa({ variables, views });
states.register(skill);

module.exports = skill;
7 changes: 1 addition & 6 deletions samples/accountLinking/skill/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
// Include the state machine module, the state machine,
// the responses and variables to be used in this skill
const skill = require('./MainStateMachine');
require('./states');

exports.handler = function handler(event, context, callback) {
skill.execute(event)
.then(response => callback(null, response))
.catch(error => callback(error));
};
exports.handler = skill.lambda();
15 changes: 1 addition & 14 deletions samples/accountLinking/www/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const uuidV4 = require('uuid/v4');
const router = require('../infrastructure/mount.js')(__dirname);
const Storage = require('../../services/userStorage.js');
const MobileDetect = require('mobile-detect');

exports.router = router;

Expand All @@ -14,7 +13,6 @@ router.get('/', (req, res) => {
});

router.post('/', (req, res, next) => {
const md = new MobileDetect(req.headers['user-agent']);
const db = new Storage();
const email = req.body.email;
const code = uuidV4().replace(/-/g, '');
Expand All @@ -27,18 +25,7 @@ router.post('/', (req, res, next) => {
return db.put(params)
.then(() => {
const redirect = `${req.query.redirect_uri}#state=${req.query.state}&access_token=${code}&token_type=Bearer`;

if (md.is('AndroidOS')) {
console.log(`redirecting android to: ${redirect}`);
res.redirect(redirect);
} else {
console.log(`redirecting web to: ${redirect}`);
res.render('auth/success', {
page: 'success',
title: 'Success',
redirectUrl: redirect,
});
}
res.redirect(redirect);
})
.catch(next);
});
16 changes: 0 additions & 16 deletions samples/accountLinking/www/views/auth/success.ejs

This file was deleted.

0 comments on commit 7e20d9d

Please sign in to comment.