Skip to content

Commit

Permalink
simplifying function to work asyncronously. track info is passed on i…
Browse files Browse the repository at this point in the history
…nvocation
  • Loading branch information
chrisdevwords authored and chrisdevwords committed Dec 17, 2017
1 parent 9952146 commit 6fc510c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 94 deletions.
84 changes: 21 additions & 63 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,47 @@
const { parseFormString } = require('./util/parse');
const slack = require('./slack');
const {
convertLinkToUri,
extractFromUri
} = require('./spotify');
const { response } = require('./util/lambda');
const radio = require('./spotify/radio');
const track = require('./spotify/track');


function handler(event, context, callback) {

const { body = '' } = event;
const { body = {} } = event;
const {
SPOTIFY_USER_ACCESS_TOKEN,
SPOTIFY_LOCAL_URL,
SLACK_TOKEN,
SPOTIFY_RADIO_PLAYLIST
} = process.env;

const {
text,
token,
response_url
} = parseFormString(body);
const { response_url } = body;
const trackInfo = body.track;

console.log('PROCESSING SLACK COMMAND', text, response_url, SPOTIFY_RADIO_PLAYLIST)
if (!trackInfo) {

if (token !== SLACK_TOKEN) {
return callback(null,
slack.slackResp(
slack.INVALID_TOKEN,
401,
slack.TYPE_PRIVATE
)
);
return callback(null, response({text: 'No track info.'}, 400))
}

const trackUri = convertLinkToUri(text);
track
.getTrackInfo(
extractFromUri(trackUri, 'track'),
SPOTIFY_USER_ACCESS_TOKEN
radio
.playBasedOnTrack(
SPOTIFY_RADIO_PLAYLIST,
trackInfo,
SPOTIFY_USER_ACCESS_TOKEN,
SPOTIFY_LOCAL_URL
)
.then((trackInfo) => {
console.log('Got track info, sending notification');
.then(msg =>
slack.notify(
response_url,
radio.SLACK_PENDING_MESSAGE(trackInfo)
);
radio
.playBasedOnTrack(
SPOTIFY_RADIO_PLAYLIST,
trackInfo,
SPOTIFY_USER_ACCESS_TOKEN,
SPOTIFY_LOCAL_URL
)
.then((msg) => {
console.log('notify success', response_url, msg);
slack.notify(
response_url,
msg
);
})
.catch(({ message }) => {
//console.log('notify error', response_url, message);
slack.notify(
response_url,
`Error creating playlist: ${message}`,
slack.TYPE_PRIVATE
);
});
})
.catch((error) => {
console.log('error creating station. sending notification', error);
msg
)
)
.catch(({ message }) =>
slack.notify(
response_url,
error.message,
`Error creating playlist: ${message}`,
slack.TYPE_PRIVATE
);
});
// eslint-disable-next-line no-param-reassign
//context.callbackWaitsForEmptyEventLoop = false;
console.log('sending callback');
return callback(null, slack.slackResp(''));

)
);
return callback(null, response({}));
}

module.exports = {
Expand Down
40 changes: 9 additions & 31 deletions test/test_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ dotenv.config({

describe('The Index Lambda Handler', () => {

context('with a request event without a slack token', () => {
const event = { };
context('with a request event without a body', () => {
const event = {};

it('sends a response body that can be parsed as JSON ', (done) => {
handler(event, {}, (err, resp) => {
try {
const { text } = JSON.parse(resp.body);
expect(text)
.to.eq(INVALID_TOKEN);
.to.equal('No track info.');
done()
} catch (error) {
done(error);
Expand All @@ -41,7 +41,7 @@ describe('The Index Lambda Handler', () => {
handler(event, {}, (err, resp) => {
try {
expect(resp.statusCode)
.to.eq(401);
.to.eq(400);
done()
} catch (error) {
done(error);
Expand All @@ -50,29 +50,22 @@ describe('The Index Lambda Handler', () => {
});
});

context('with an request event with a valid token', () => {
context('with a request event with a valid token', () => {

const spotifyTrack = 'spotify:track:2771LMNxwf62FTAdpJMQfM';
const notificationUri = encodeURIComponent('https://hooks.slack.com/commands/T4ZLYGVSN/227562856215/B4XvvRukWrmUzSJ0cMC0arpE');
const slackBody = `text=${spotifyTrack}&token=foo_bar_baz&response_url=${notificationUri}`;
const event = { body: slackBody };
const trackInfo = {
name: 'Bodak Yellow',
artist: 'Cardi B',
id: '2771LMNxwf62FTAdpJMQfM'
};
const respMsg = ''; //radio.SLACK_PENDING_MESSAGE(trackInfo);
const event = { body: { track: trackInfo, response_url: notificationUri } };
const respMsg = 'No track info.'; //radio.SLACK_PENDING_MESSAGE(trackInfo);

beforeEach(() => {
sinon
.stub(radio, 'playBasedOnTrack')
.resolves(respMsg);

sinon
.stub(track, 'getTrackInfo')
.resolves(trackInfo);

// todo assert calls
sinon
.stub(slack, 'notify')
.resolves({});
Expand All @@ -81,36 +74,21 @@ describe('The Index Lambda Handler', () => {

afterEach(() => {
radio.playBasedOnTrack.restore();
track.getTrackInfo.restore();
slack.notify.restore();
});

it('sends a response body', (done) => {
handler(event, {}, (err, resp) => {
try {
expect(resp.body)
.to.be.a('String');
expect(JSON.parse(resp.body).statusCode)
.to.eq(200);
done()
} catch (error) {
done(error);
}
});
});

it('sends a response body that can be parsed as JSON ', (done) => {
handler(event, {}, (err, resp) => {
try {
const { text } = JSON.parse(resp.body);
expect(text)
.to.eq(respMsg);
done()
} catch (error) {
done(error); console.log('Send Slack notification that this worked:', msg, response_url);

}
});
});

it('sends a responseCode 200', (done) => {
handler(event, {}, (err, resp) => {
try {
Expand Down

0 comments on commit 6fc510c

Please sign in to comment.