Skip to content

Commit 95c928f

Browse files
committed
Moved all Plex stuff to its own module object
Tests back up and running
1 parent 0415ba0 commit 95c928f

16 files changed

+362
-222
lines changed

Gruntfile.js

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ module.exports = function(grunt) {
5252
command: 'deploy.bat'
5353
}
5454
},
55+
jsdoc : {
56+
dist : {
57+
src: ['*.js', 'lib/**/*.js', 'test/*.js'],
58+
options: {
59+
destination: 'jsdoc',
60+
template : "node_modules/ink-docstrap/template",
61+
configure : "node_modules/ink-docstrap/template/jsdoc.conf.json",
62+
readme: "README.md"
63+
}
64+
}
65+
}
5566
});
5667

5768
// Coveralls support
@@ -68,6 +79,7 @@ module.exports = function(grunt) {
6879
grunt.loadNpmTasks('grunt-shell');
6980
grunt.loadNpmTasks('grunt-mocha-test');
7081
grunt.loadNpmTasks('grunt-mocha-istanbul');
82+
grunt.loadNpmTasks('grunt-jsdoc');
7183

7284
// Default task(s).
7385
grunt.registerTask('default', ['mocha_istanbul:coverage']);

index.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1+
/**
2+
* @module
3+
*/
4+
15
require('dotenv').load();
2-
var common = require('./lib/common.js');
3-
var db = require('./lib/db.js');
4-
var stateMachine = require('./lib/statemachine.js');
6+
var app = require('./lib/app');
7+
var db = require('./lib/db');
8+
var stateMachine = require('./lib/statemachine');
59
var User = require('./lib/user');
610
var Alexa = require('alexa-app');
7-
8-
//console.error = function(err) {
9-
// console.log(err);
10-
// console.log(err.stack);
11-
//};
12-
13-
// HACK this is pretty terrible, but it's the only way I could get it to work with proxyquire in the unit tests :(
14-
// There is a better way I'm sure. TODO fix it.
15-
common.PlexAPI = require('plex-api');
16-
common.PlexPinAuth = require('plex-api-pinauth');
11+
var Plex = require('./lib/plex');
1712

1813
/**
1914
* The main AWS Lambda handler.
@@ -29,19 +24,22 @@ exports.handler = function(event, context) {
2924
}
3025
}
3126

32-
common.app = new Alexa.app('plex');
27+
app.skill = new Alexa.app('plex');
28+
app.plex = new Plex.Plex();
3329

3430
db.initializeUserRecord(event.session.user.userId).then(function(dbuser) {
35-
common.user = new User(dbuser);
31+
app.user = new User(dbuser);
32+
app.plex.pinAuth.token = app.user.authtoken;
3633

37-
if(!common.user.authtoken) {
34+
if(!app.user.authtoken) {
3835
return stateMachine.initApp('not-authed');
3936
} else {
4037
return stateMachine.initApp('authed');
4138
}
4239
}).then(function() {
43-
common.app.lambda()(event, context);
40+
app.skill.lambda()(event, context);
4441
}).catch(function(err) {
4542
console.error(err);
43+
console.error(err.stack);
4644
});
4745
};

lib/app.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @module app
3+
*/
4+
5+
/**
6+
* @name module:app
7+
*/
8+
app = module.exports = {
9+
/** @type {module:Plex~Plex} */
10+
plex: null,
11+
12+
// TODO figure out how to properly jsdoc this so that it is recognized as an alexa-app.app object
13+
/** @type {Object} */
14+
skill: null,
15+
16+
/**
17+
* How confident we need to be when trying to figure out which show a user is talkijng about
18+
* @const
19+
*/
20+
CONFIDICE_CONFIRM_THRESHOLD: 0.4,
21+
22+
/**
23+
* The invocation name used for this app. Used in many responses so put here in case it changes.
24+
* @const
25+
* @type {string}
26+
*/
27+
INVOCATION_NAME: "the home theater"
28+
};

lib/common.js

-29
This file was deleted.

lib/db.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function updateUserPlayer(user, player) {
171171
}
172172

173173
if(typeof user === 'object') {
174-
user.dbobject = data.Attributes; // TODO this is broken
174+
user.dbobject = data.Attributes;
175175
}
176176

177177
deferred.fulfill(data);

lib/plex.js

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @module Plex
3+
*/
4+
5+
"use strict";
6+
var PlexAPI = require('plex-api');
7+
var PlexPinAuth = require('plex-api-pinauth');
8+
var app = require('./app');
9+
10+
require('dotenv').load('../');
11+
12+
/**
13+
* The app config used in all Plex API requests for the X-Plex headers describing this app
14+
* @const
15+
* @type {{product: {String}, version: {String}, device: {String}, deviceName: {String}, identifier: {String}}}
16+
*/
17+
var PLEX_APP_CONFIG = {
18+
product: process.env.APP_PRODUCT,
19+
version: process.env.APP_VERSION,
20+
device: process.env.APP_DEVICE,
21+
deviceName: process.env.APP_DEVICE_NAME,
22+
identifier: process.env.APP_IDENTIFIER
23+
};
24+
25+
/**
26+
* Creates a new Plex object, which handles the stateful objects from the plex-api library
27+
* @constructor Plex
28+
* @classdesc A container for the multiple stateful plex-api objects needed for the app
29+
*/
30+
var Plex = function() {
31+
var context = this;
32+
33+
/**
34+
* The stateful PlexPinAuth object that provides handy PIN auth functions
35+
* @member {PlexPinAuth}
36+
*/
37+
this.pinAuth = new PlexPinAuth();
38+
39+
/**
40+
* @private
41+
* @member {?PlexAPI}
42+
*/
43+
this._pms = null;
44+
/**
45+
* @private
46+
* @member {?PlexAPI}
47+
*/
48+
this._web = null;
49+
this.initializeWebApi();
50+
51+
/**
52+
* @type {PlexAPI}
53+
* @name module:Plex~Plex#pms
54+
* @memberof module:Plex~Plex
55+
*/
56+
Object.defineProperty(this, 'pms', {
57+
get: function() {
58+
if(!context._pms) {
59+
if(!context.initializeServerApi()) {
60+
throw new ReferenceError("Tried to access Plex.pms before it has been initialized");
61+
}
62+
}
63+
return context._pms;
64+
}
65+
});
66+
67+
/**
68+
* @type {PlexAPI}
69+
* @name module:Plex~Plex#web
70+
* @memberof module:Plex~Plex
71+
*/
72+
Object.defineProperty(this, 'web', {
73+
get: function() {
74+
if(!context._pms) {
75+
if(!context.initializeWebApi()) {
76+
throw new ReferenceError("Tried to access Plex.web before it has been initialized");
77+
}
78+
}
79+
return context._web;
80+
}
81+
});
82+
};
83+
84+
/**
85+
* Initializes the "web" instance of the Plex API
86+
* @returns {Boolean} true if creation of the API object was successful
87+
*/
88+
Plex.prototype.initializeWebApi = function() {
89+
this._web = new PlexAPI({
90+
hostname: 'plex.tv',
91+
port: 443,
92+
https: true,
93+
authenticator: this.pinAuth,
94+
options: PLEX_APP_CONFIG
95+
});
96+
97+
return true;
98+
};
99+
100+
/**
101+
* Initializes the "server" instance of the Plex API
102+
* The user must have a server configured or the creation will fail
103+
* @returns {Boolean} true if creation of the API object was successful
104+
*/
105+
Plex.prototype.initializeServerApi = function() {
106+
if(!app.user.server) {
107+
return false;
108+
}
109+
110+
this._pms = new PlexAPI({
111+
hostname: app.user.hostname,
112+
port: app.user.port,
113+
https: app.user.https,
114+
authenticator: this.pinAuth,
115+
options: PLEX_APP_CONFIG
116+
});
117+
118+
return true;
119+
};
120+
121+
/**
122+
* Sets the auth token to be used on all future API calls
123+
* @param {String} authtoken - The auth token
124+
*/
125+
Plex.prototype.setAuthToken = function(authtoken) {
126+
this.pinAuth.token = authtoken;
127+
};
128+
129+
module.exports = {
130+
Plex: Plex
131+
};

0 commit comments

Comments
 (0)