Skip to content

Commit

Permalink
moving slack response stuff into one module formatting now playing re…
Browse files Browse the repository at this point in the history
…sponse
  • Loading branch information
chrisdevwords authored and chrisdevwords committed Mar 12, 2017
1 parent 808fdfb commit e01b8b5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
12 changes: 8 additions & 4 deletions src/commands.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

import request from 'request-promise-native';

export const CMD_NOT_SUPPORTED = 'Command not supported.';
import {
CMD_NOT_SUPPORTED,
NOW_PLAYING
} from './slack-resp';

let _apiRoot;

Expand Down Expand Up @@ -77,14 +79,16 @@ export function skipTrack() {
}

export function getPlaying() {

const uri = `${_apiRoot}/api/spotify/playing/`;

return request
.get({
uri,
json: true
})
.then((resp) => {
return resp;
.then(({ track }) => {
return NOW_PLAYING(track)
});
}

Expand Down
21 changes: 6 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@

import { response } from './util/lambda';
import { parseFormString } from './util/parse';
import { exec, setAPIRoot } from './commands';

const TYPE_PRIVATE = 'ephemeral';
const TYPE_PUBLIC = 'in_channel';

const INVALID_TOKEN = 'Token is invalid.';

function slackResp(text, code = 200, type = TYPE_PUBLIC) {
return response({
// eslint-disable-next-line camelcase
response_type: type,
text
}, code);
}

import {
slackResp,
INVALID_TOKEN,
TYPE_PRIVATE,
TYPE_PUBLIC
} from './slack-resp';

function handler(event, context, callback) {

Expand Down
26 changes: 26 additions & 0 deletions src/slack-resp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import { response } from './util/lambda';

// response types
export const TYPE_PRIVATE = 'ephemeral';
export const TYPE_PUBLIC = 'in_channel';

// error messages
const INVALID_TOKEN = 'Token is invalid.';

// message templates
export const CMD_NOT_SUPPORTED = 'Command not supported.';
export const NOW_PLAYING = ({ name, artist, requestedBy }) =>
`Now playing "${name}" by ${artist} requested by ${requestedBy}.`;



export function slackResp(text, code = 200, type = TYPE_PUBLIC) {
return response({
// eslint-disable-next-line camelcase
response_type: type,
text
}, code);
}

export default {}
4 changes: 3 additions & 1 deletion test/test_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ describe('The Spotify Slack Lambda index.handler', () => {
handler(event, {}, (err, resp) => {
try {
expect(resp.statusCode)

.to.eq(200);
done()
} catch (err) {
done(err);

done(resp);
}
});
});
Expand Down

0 comments on commit e01b8b5

Please sign in to comment.