Skip to content

Commit

Permalink
Implement managing app streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Wood committed Apr 18, 2017
1 parent 2190ef1 commit 8180c37
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 1 deletion.
101 changes: 101 additions & 0 deletions lib/app_streams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"use strict";

/**
* App Streams
* @module app_streams
*/
module.exports = api => {
return {
/**
* Get a specific app stream by its key.
* @param {Object} [params] - Additional URI parameters
* @returns {Promise}
*/
streams(params = {}) {
return api.request("/streams", { params: params });
},

/**
* Get a specific app stream by its key.
* @param {string} streamKey - A stream key
* @param {Object} [params] - Additional URI parameters
* @returns {Promise}
*/
stream(streamKey, params = {}) {
return api.request(`/streams/${streamKey}`, { params: params });
},

/**
* Create an app stream for the authenticated app.
*
* The options object must at least include "objectType",
* which is an array of up to 5(!) valid apps streams to subscribe to.
*
* Allowed values are: post, bookmark, follow, mute, block, message,
* channel, channel_subscription, token and user
*
* You can optionally add "key: 'myfancykeyname'" to name your stream.
* If you do not supply your own key, you get back a generated one from the API
* which you need to keep track of.
*
* @param {Object} options - Option object.
* @returns {Promise}
*/
createStream(options) {
let sanitizedOptions = {
type: "long_poll",
object_types: options.objectTypes
};

if (options.key) {
sanitizedOptions.key = options.key;
}

return api.request("/streams", {
httpMethod: "POST",
data: sanitizedOptions
});
},

/**
* Update an app stream for the authorized app.
*
* Same rules as in createStream apply, but "key"
* is no longer optional and must be supplied to identify
* the stream you want to update.
* @param {Object} options - Options object
* @returns {Promise}
*/
updateStream(options) {
let sanitizedOptions = {
object_types: options.objectTypes
};

if (options.key) {
sanitizedOptions.key = options.key;
}

return api.request("/streams", {
httpMethod: "PUT",
data: sanitizedOptions
});
},

/**
* Delete all app streams for the authorized app.
* @returns {Promise}
*/
deleteStreams() {
return api.request("/streams", { httpMethod: "DELETE" });
},

/**
* Delete a specific app stream by its key.
* @param {string} streamKey - A stream key
* @returns {Promise}
*/
deleteStream(streamKey) {
return api.request(`/streams/${streamKey}`, { httpMethod: "DELETE" });
}
};
};
4 changes: 3 additions & 1 deletion lib/pnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const posts = require("./posts");
const channels = require("./channels");
const messages = require("./messages");
const system = require("./system");
const appStreams = require("./app_streams");

/**
* pnut-butter.js
Expand Down Expand Up @@ -114,7 +115,8 @@ const pnut = () => {
posts(api),
channels(api),
messages(api),
system(api)
system(api),
appStreams(api)
);
};

Expand Down
71 changes: 71 additions & 0 deletions test/app_streams_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const nock = require("nock");
chai.use(chaiAsPromised);
chai.should();

const pnut = require("../lib/pnut");

before(function() {
let base = "https://api.pnut.io/v0";

nock(base).get("/streams").reply(200, {});
nock(base).get("/streams/post").reply(200, {});
nock(base)
.post("/streams", {
type: "long_poll",
object_types: ["post", "message", "channel_subscription", "bookmark"],
key: "jelly"
})
.reply(200, {});

nock(base)
.put("/streams", {
object_types: ["post", "message"],
key: "jelly"
})
.reply(200, {});

nock(base).delete("/streams").reply(200, {});
nock(base).delete("/streams/jelly").reply(200, {});
});

after(function() {
nock.cleanAll();
});

describe("App Streams", () => {
it("should be able to get all app streams for the authenticated app", () => {
return pnut.streams().should.become({});
});

it("should be able to get a specific app stream by its key", () => {
return pnut.stream("post").should.become({});
});

it("should be able to create an app stream for the authenticated app", () => {
return pnut
.createStream({
objectTypes: ["post", "message", "channel_subscription", "bookmark"],
key: "jelly"
})
.should.become({});
});

it("should be able to update an app stream", () => {
return pnut
.updateStream({
objectTypes: ["post", "message"],
key: "jelly"
})
.should.become({});
});

it("should be able to delete all app streams for the authorized app", () => {
return pnut.deleteStreams().should.become({});
});

it("should be able to delete a specific app stream by its key", () => {
return pnut.deleteStream("jelly").should.become({});
});
});

0 comments on commit 8180c37

Please sign in to comment.