From e67b0c02f25d44c848f5d9cd033c63ceff2b5718 Mon Sep 17 00:00:00 2001 From: Thomas Shafer Date: Mon, 8 Sep 2014 15:46:52 -0700 Subject: [PATCH] added ability to query stream index by name --- index.js | 13 +++++--- src/index.coffee | 8 +++-- test/index_test.coffee | 51 ++++++++++++++++++++---------- test/nock/get_streams_with_name.js | 21 ++++++++++++ 4 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 test/nock/get_streams_with_name.js diff --git a/index.js b/index.js index f9df603..32585ed 100644 --- a/index.js +++ b/index.js @@ -147,11 +147,14 @@ return request.put(requestOptions(url), responseCallback(callback)); }; - StreamsHandler.prototype.index = function(callback) { - var params, url; - params = serialize({ - secretKey: this.config.secretKey - }); + StreamsHandler.prototype.index = function(params, callback) { + var url; + if (typeof params === 'function') { + callback = params; + params = {}; + } + params.secretKey = this.config.secretKey; + params = serialize(params); url = "" + BASE_URL + "/streams?" + params; return request.get(requestOptions(url), responseCallback(callback)); }; diff --git a/src/index.coffee b/src/index.coffee index 7608abb..5b73aaa 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -89,8 +89,12 @@ class StreamsHandler request.put requestOptions(url), responseCallback(callback) # callback(err, streams) - index: (callback)-> - params = serialize(secretKey: @config.secretKey) + index: (params, callback)-> + if typeof params == 'function' + callback = params + params = {} + params.secretKey = @config.secretKey + params = serialize(params) url = "#{BASE_URL}/streams?#{params}" request.get requestOptions(url), responseCallback(callback) diff --git a/test/index_test.coffee b/test/index_test.coffee index 80cb9ef..d88633f 100644 --- a/test/index_test.coffee +++ b/test/index_test.coffee @@ -93,22 +93,41 @@ describe 'CineIO', -> @cine = CineIO.init {publicKey: 'my key', secretKey: 'MY SECRET'} describe 'index', -> - beforeEach -> - @stub = requireFixture('get_streams') - - it 'gets the streams', (done)-> - @cine.streams.index (err, streams)=> - expect(streams).to.have.length(1) - stream = streams[0] - expect(stream.id).to.equal("the stream id") - expect(stream.streamName).to.equal("the stream name") - expect(stream.expiration).to.equal("2034-05-17T00:00:00.000Z") - expect(stream.password).to.equal("the stream password") - expect(stream.play).to.deep.equal({hls: "the hls url", rtmp: "the rtmp url"}) - expect(stream.publish).to.deep.equal({url: "the publish url", stream: "the publish stream"}) - - expect(@stub.isDone()).to.be.true - done(err) + describe 'without params', -> + beforeEach -> + @stub = requireFixture('get_streams') + + it 'gets the streams', (done)-> + @cine.streams.index (err, streams)=> + expect(streams).to.have.length(1) + stream = streams[0] + expect(stream.id).to.equal("the stream id") + expect(stream.streamName).to.equal("the stream name") + expect(stream.expiration).to.equal("2034-05-17T00:00:00.000Z") + expect(stream.password).to.equal("the stream password") + expect(stream.play).to.deep.equal({hls: "the hls url", rtmp: "the rtmp url"}) + expect(stream.publish).to.deep.equal({url: "the publish url", stream: "the publish stream"}) + + expect(@stub.isDone()).to.be.true + done(err) + + describe 'with name', -> + beforeEach -> + @stub = requireFixture('get_streams_with_name') + + it 'gets the streams', (done)-> + @cine.streams.index name: 'my name', (err, streams)=> + expect(streams).to.have.length(1) + stream = streams[0] + expect(stream.id).to.equal("the stream id") + expect(stream.streamName).to.equal("the stream name") + expect(stream.expiration).to.equal("2034-05-17T00:00:00.000Z") + expect(stream.password).to.equal("the stream password") + expect(stream.play).to.deep.equal({hls: "the hls url", rtmp: "the rtmp url"}) + expect(stream.publish).to.deep.equal({url: "the publish url", stream: "the publish stream"}) + + expect(@stub.isDone()).to.be.true + done(err) describe 'get', -> beforeEach -> diff --git a/test/nock/get_streams_with_name.js b/test/nock/get_streams_with_name.js new file mode 100644 index 0000000..38f09cb --- /dev/null +++ b/test/nock/get_streams_with_name.js @@ -0,0 +1,21 @@ +var response; + +response = [{ + id: "the stream id", + streamName: "the stream name", + name: 'my name', + play: { + hls: "the hls url", + rtmp: "the rtmp url" + }, + publish: { + url: "the publish url", + stream: "the publish stream" + }, + expiration: "2034-05-17T00:00:00.000Z", + password: "the stream password" +}]; + +module.exports = function() { + return nock('https://www.cine.io:443').get('/api/1/-/streams?name=my%20name&secretKey=MY%20SECRET').reply(200, JSON.stringify(response)); +};