forked from kaiwood/pnut-butter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kai Wood
committed
Apr 18, 2017
1 parent
2190ef1
commit 8180c37
Showing
3 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); | ||
}); | ||
}); |