Large diffs are not rendered by default.

@@ -4,7 +4,7 @@
"private": true,
"dependencies": {
"alexa-sdk": "^1.0.17",
"nodeunit": "^0.11.2",
"snoowrap": "^1.14.2"
"snoowrap": "^1.14.2",
"bluebird": "latest"
}
}

This file was deleted.

@@ -0,0 +1,47 @@
exports.isTextPost = function isTextPost(post) {
return Boolean(post.selftext);
};

exports.isNotStickiedPost = function isNotStickiedPost(post) {
return !post.stickied;
};

exports.isSFW = function isSFW(post) {
return !post.over_18;
};

exports.isStoryPost = function (post) {
return exports.isSFW(post) && exports.isTextPost(post) && exports.isNotStickiedPost(post);
};

exports.getStoryPosts = function (subreddit, reddit) {
return reddit
.getSubreddit(subreddit)
.getHot()
.filter(exports.isStoryPost);
};

exports.emitStoryPost = function emitStoryPost(story, alexa) {
// Create speech output
const title = story.title;
const author = story.author.name;
const body = story.selftext;
const url = story.permalink;

const cardTitle = "\"" + title + "\", by " + author;
const cardContent = body + "\n\n" + url;
const speechOutput = "By " + author + ".\n\n" + body;

alexa.emit(':tellWithCard', speechOutput, cardTitle, cardContent);
};

exports.getRandomElementFrom = function (array) {
if(array.length === 0){
throw new Error("Pulling from an empty array");
}
return array[Math.floor(Math.random() * array.length)];
};

exports.emitRandomStoryPostFrom = function emitRandomStoryPost(stories, alexa) {
exports.emitStoryPost(exports.getRandomElementFrom(stories), alexa);
};
@@ -0,0 +1,11 @@
const packageInfo = require('../package.json');
const credentials = require('../credentials');
const snoowrap = require("snoowrap");

exports.client = new snoowrap({
userAgent: "alexa:client-tales:v" + packageInfo.version,
clientId: credentials.clientID,
clientSecret: credentials.clientSecret,
username: credentials.username,
password: credentials.password
});
@@ -1,35 +1,54 @@
require('nodeunit');
'use strict';

const index = require('../index');
const postTools = require('../postTools');
const postTools = require('../src/postTools');

exports.test_emittingRandomStoryFromListEmitsOne = function (test){
test.expect(1);
const BAREBONES_TITLE = 'TITLE';
const BAREBONES_AUTHOR = 'AUTHOR';
const BAREBONES_SELF_TEXT = 'SELFTEXT';
const BAREBONES_PERMALINK = 'PERMALINK';

exports.test_emittingStoryHitsCallback = function (test){
test.expect(5);
const testAlexaObject = {
emit: function(type){
emit: function(type, speechOutput, cardTitle, cardContent){
test.equals(type, ':tellWithCard');
test.ok(speechOutput.includes(BAREBONES_SELF_TEXT));
test.ok(cardTitle.includes(BAREBONES_AUTHOR));
test.ok(cardTitle.includes(BAREBONES_TITLE));
test.ok(cardContent.includes(BAREBONES_SELF_TEXT));
}
};
postTools.emitRandomStoryPost([makeBarebonesPost()], testAlexaObject);
postTools.emitStoryPost(makeBarebonesPost(), testAlexaObject);
test.done();
};

exports.test_emittingStoryHitsCallback = function (test){
exports.test_gettingRandomElementWorks = function(test){
test.expect(1);
const test_obj = {'test':'a'};
test.equals(postTools.getRandomElementFrom([test_obj]), test_obj);
test.done();
};

exports.test_emittingRandomStoryFromListEmitsOne = function (test){
test.expect(5);
const testAlexaObject = {
emit: function(type){
emit: function(type, speechOutput, cardTitle, cardContent){
test.equals(type, ':tellWithCard');
test.ok(speechOutput.includes(BAREBONES_SELF_TEXT));
test.ok(cardTitle.includes(BAREBONES_AUTHOR));
test.ok(cardTitle.includes(BAREBONES_TITLE));
test.ok(cardContent.includes(BAREBONES_SELF_TEXT));
}
};
postTools.emitStoryPost(makeBarebonesPost(), testAlexaObject);
postTools.emitRandomStoryPostFrom([makeBarebonesPost()], testAlexaObject);
test.done();
};

function makeBarebonesPost() {
return {
'title': 'TITLE',
'author': 'AUTHOR',
'selftext': 'SELFTEXT',
'permalink': 'PERMALINK'
'title': BAREBONES_TITLE,
'author': BAREBONES_AUTHOR,
'selftext': BAREBONES_SELF_TEXT,
'permalink': BAREBONES_PERMALINK
}
}