├── .gitignore ├── .eslintrc.json ├── .travis.yml ├── tests ├── makeHttpRequest-spec.js ├── commandsExecute-spec.js ├── BublOscClient-spec.js ├── poll-spec.js └── OscClient-spec.js ├── index.js ├── lib ├── OscError.js ├── poll.js ├── makeHttpRequest.js ├── commandsExecute.js ├── Osc2Client.js ├── BublOscClient.js └── OscClient.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@bubltechnology/eslint-config-bubl" 3 | } 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '5' 4 | - '6' 5 | - 'node' 6 | script: 7 | - npm run lint 8 | - npm run test 9 | -------------------------------------------------------------------------------- /tests/makeHttpRequest-spec.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 3 | var makeHttpRequest = require('../lib/makeHttpRequest') 4 | var assert = require('assert') 5 | 6 | describe('makeHttpRequest', function () { 7 | it('should return a promise', function () { 8 | var prom = makeHttpRequest('GET', 'http://localhost/') 9 | assert(typeof prom.then === 'function') 10 | assert(typeof prom.catch === 'function') 11 | }) 12 | }) 13 | -------------------------------------------------------------------------------- /tests/commandsExecute-spec.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 3 | var commandsExecute = require('../lib/commandsExecute') 4 | var assert = require('assert') 5 | 6 | describe('commandsExecute', function () { 7 | it('returns a promise', function () { 8 | var fakeClient = { 9 | serverAddress: 'http://localhost:8000' 10 | } 11 | var prom = commandsExecute.apply(fakeClient, []) 12 | assert(typeof prom.then === 'function') 13 | assert(typeof prom.catch === 'function') 14 | }) 15 | }) 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Bubl Technology Inc. 2 | // 3 | // Licensed under the MIT license 4 | // . 5 | // This file may not be copied, modified, or distributed 6 | // except according to those terms. 7 | 8 | 'use strict'; 9 | 10 | var OscClient = require('./lib/OscClient'); 11 | var BublOscClient = require('./lib/BublOscClient') 12 | var Osc2Client = require('./lib/Osc2Client') 13 | 14 | module.exports = { 15 | OscClient: OscClient, 16 | BublOscClient: BublOscClient, 17 | Osc2Client: Osc2Client 18 | }; 19 | -------------------------------------------------------------------------------- /lib/OscError.js: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Bubl Technology Inc. 2 | // 3 | // Licensed under the MIT license 4 | // . 5 | // This file may not be copied, modified, or distributed 6 | // except according to those terms. 7 | 8 | function OscError (err) { 9 | this._raw = err 10 | this.oscCode = err.error.code 11 | this.oscMessage = err.error.message 12 | 13 | this.name = 'OscError' 14 | this.message = `${this._raw.name} failed with ${this.oscCode}: ${this.oscMessage}` 15 | } 16 | 17 | OscError.prototype = new Error() 18 | 19 | module.exports = OscError 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "osc-client", 3 | "version": "2.0.3", 4 | "description": "Wrapper for making requests to Open Spherical Camera APIs", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git@github.com:BublTechnology/osc-client.git" 9 | }, 10 | "scripts": { 11 | "test": "$(npm bin)/mocha tests", 12 | "lint": "$(npm bin)/eslint ./tests ./lib", 13 | "lint-fix": "$(npm bin)/eslint ./tests ./lib --fix" 14 | }, 15 | "keywords": [ 16 | "OSC", 17 | "Spherical", 18 | "API", 19 | "Open", 20 | "Spherical", 21 | "Camera", 22 | "Node" 23 | ], 24 | "author": "", 25 | "license": "MIT", 26 | "dependencies": { 27 | "q": "^1.4.1", 28 | "superagent": "^1.4.0" 29 | }, 30 | "devDependencies": { 31 | "@bubltechnology/eslint-config-bubl": "^2.0.1", 32 | "eslint": "^3.18.0", 33 | "mocha": "^3.0.0", 34 | "assert": "^1.3.0", 35 | "sinon": "^1.17.2" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## osc-client 2 | 3 | [![Build Status](https://travis-ci.org/BublTechnology/osc-client.svg)](https://travis-ci.org/BublTechnology/osc-client) 4 | 5 | Wrapper for making requests to [Open Spherical Camera API](https://developers.google.com/streetview/open-spherical-camera/?hl=en) 6 | 7 | 8 | ### Install with NPM 9 | ```shell 10 | npm install osc-client --save 11 | ``` 12 | 13 | ### Connect and take a picture. 14 | 15 | ```javascript 16 | 17 | var OscClientClass = require('osc-client').OscClient; 18 | 19 | var domain = '127.0.0.1'; 20 | var port = '8000'; 21 | var client = new OscClientClass(domain, port); 22 | var sessionId; 23 | 24 | client.startSession().then(function(res){ 25 | sessionId = res.body.results.sessionId; 26 | return client.takePicture(sessionId); 27 | }) 28 | .then(function (res) { 29 | var pictureUri = res.body.results.fileUri; 30 | return client.getImage(pictureUri); 31 | }) 32 | .then(function(res){ 33 | var imgData = res.body; // 34 | return client.closeSession(sessionId); 35 | }); 36 | 37 | 38 | ``` 39 | -------------------------------------------------------------------------------- /lib/poll.js: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Bubl Technology Inc. 2 | // 3 | // Licensed under the MIT license 4 | // . 5 | // This file may not be copied, modified, or distributed 6 | // except according to those terms. 7 | 8 | 'use strict' 9 | 10 | var OscError = require('./OscError') 11 | 12 | var poll = { 13 | pollPeriod: 2000, 14 | commandStatus: function (client, commandId, deferred, initialTimeStamp, statusCallback) { 15 | var intervalId = setInterval(function () { 16 | client.commandsStatus(commandId) 17 | .then(function (res) { 18 | if (!res) { 19 | deferred.reject(new Error('CommandStatus wrong response format -- response must have a body')) 20 | } else if (res.state === 'error') { 21 | deferred.reject(new OscError(res)) 22 | } else if (res.state !== 'inProgress') { 23 | deferred.resolve(res) 24 | } else { 25 | if (typeof statusCallback === 'function') { 26 | statusCallback(res) 27 | } 28 | return 29 | } 30 | clearInterval(intervalId) 31 | }, deferred.reject) 32 | }, this.pollPeriod) 33 | } 34 | } 35 | 36 | module.exports = poll 37 | -------------------------------------------------------------------------------- /lib/makeHttpRequest.js: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Bubl Technology Inc. 2 | // 3 | // Licensed under the MIT license 4 | // . 5 | // This file may not be copied, modified, or distributed 6 | // except according to those terms. 7 | 8 | var Q = require('q') 9 | var request = require('superagent') 10 | var OscError = require('./OscError') 11 | 12 | // HTTP REQUEST 13 | var makeHttpRequest = function (method, url, contentType, body) { 14 | 'use strict' 15 | var deferred = Q.defer() 16 | var requestMethod = null 17 | 18 | switch (method) { 19 | case 'POST': 20 | requestMethod = request.post 21 | break 22 | case 'GET': 23 | requestMethod = request.get 24 | break 25 | case 'PUT': 26 | requestMethod = request.put 27 | break 28 | case 'DELETE': 29 | requestMethod = request.delete 30 | break 31 | default: 32 | requestMethod = request.get 33 | } 34 | 35 | requestMethod(url) 36 | .type('json') 37 | .send(body) 38 | .set('Content-Type', 'application/json; charset=utf-8') 39 | .set('X-XSRF-Protected', '1') 40 | .end(function (err, res) { 41 | if (err) { 42 | deferred.reject(new OscError(err.response.body)) 43 | return 44 | } 45 | deferred.resolve(res.body) 46 | }) 47 | 48 | return deferred.promise 49 | } 50 | 51 | module.exports = makeHttpRequest 52 | -------------------------------------------------------------------------------- /lib/commandsExecute.js: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Bubl Technology Inc. 2 | // 3 | // Licensed under the MIT license 4 | // . 5 | // This file may not be copied, modified, or distributed 6 | // except according to those terms. 7 | 8 | 'use strict' 9 | 10 | var Q = require('q') 11 | var request = require('superagent') 12 | var poll = require('./poll') 13 | var OscError = require('./OscError') 14 | 15 | // OSC COMMANDS EXECUTE 16 | var commandsRequest = function (name, params, callback) { 17 | var commandsExecuteUrl = this.serverAddress + '/osc/commands/execute' 18 | request.post(commandsExecuteUrl) 19 | .type('json') 20 | .send({ name: name, parameters: params }) 21 | .set('Content-Type', 'application/json; charset=utf-8') 22 | .set('X-XSRF-Protected', '1') 23 | .end(callback) 24 | } 25 | 26 | var commandsExecute = function (name, params, statusCallback) { 27 | var deferred = Q.defer() 28 | var client = this 29 | commandsRequest.apply(client, [name, params, function (err, res) { 30 | if (err) { 31 | deferred.reject(new OscError(err.response.body)) 32 | return 33 | } 34 | var timeStamp = Date.now() 35 | if (res.headers['content-type'] === 'application/json; charset=utf-8') { 36 | if (res.body.state === 'error') { 37 | deferred.reject(new OscError(res.body)) 38 | } else if (res.body.state !== 'inProgress') { 39 | deferred.resolve(res.body) 40 | } else { 41 | var commandId = res.body.id 42 | poll.commandStatus(client, commandId, deferred, timeStamp, statusCallback) 43 | } 44 | } else { 45 | deferred.resolve(res.body) // only for getImage command 46 | } 47 | }]) 48 | return deferred.promise 49 | } 50 | 51 | module.exports = commandsExecute 52 | -------------------------------------------------------------------------------- /lib/Osc2Client.js: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Bubl Technology Inc. 2 | // 3 | // Licensed under the MIT license 4 | // . 5 | // This file may not be copied, modified, or distributed 6 | // except according to those terms. 7 | 8 | 'use strict' 9 | 10 | var OscClient = require('./OscClient') 11 | var commandsExecute = require('./commandsExecute') 12 | 13 | var Osc2Client = function () { 14 | this.constructor.apply(this, Array.prototype.slice.call(arguments)) 15 | } 16 | 17 | Osc2Client.prototype = new OscClient() 18 | 19 | /*********************************************************/ 20 | /* OSC Level 2 Commands Endpoints */ 21 | /*********************************************************/ 22 | 23 | // OSC2 COMMAND RESET 24 | Osc2Client.prototype.reset = function () { 25 | return commandsExecute.apply(this, ['camera.reset']) 26 | } 27 | 28 | // OSC2 COMMAND LISTFILES 29 | Osc2Client.prototype.listFiles = function (fileType, entryCount, maxThumbSize, startPosition) { 30 | return commandsExecute.apply(this, ['camera.listFiles', { 31 | fileType: fileType, 32 | entryCount: entryCount, 33 | maxThumbSize: maxThumbSize, 34 | startPosition: startPosition 35 | }]) 36 | } 37 | 38 | // OSC2 COMMAND STARTCAPTURE 39 | Osc2Client.prototype.startCapture = function (statusCallback) { 40 | return commandsExecute.apply(this, ['camera.startCapture', {}, statusCallback]) 41 | } 42 | 43 | // OSC2 COMMAND STOPCAPTURE 44 | Osc2Client.prototype.stopCapture = function () { 45 | return commandsExecute.apply(this, ['camera.stopCapture']) 46 | } 47 | 48 | // OSC2 COMMAND DELETE2 49 | Osc2Client.prototype.delete2 = function (fileUrls) { 50 | return commandsExecute.apply(this, ['camera.delete', { fileUrls: fileUrls }]) 51 | } 52 | 53 | module.exports = Osc2Client 54 | -------------------------------------------------------------------------------- /lib/BublOscClient.js: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Bubl Technology Inc. 2 | // 3 | // Licensed under the MIT license 4 | // . 5 | // This file may not be copied, modified, or distributed 6 | // except according to those terms. 7 | 8 | 'use strict' 9 | 10 | var OscClient = require('./Osc2Client') 11 | var makeHttpRequest = require('./makeHttpRequest') 12 | var commandsExecute = require('./commandsExecute') 13 | 14 | var BublOscClient = function () { 15 | this.constructor.apply(this, Array.prototype.slice.call(arguments)) 16 | } 17 | 18 | BublOscClient.prototype = new OscClient() 19 | 20 | /*********************************************************/ 21 | /* Bubl's Vendor Specific Endpoints and Commands */ 22 | /*********************************************************/ 23 | 24 | // OSC BUBL UPDATE 25 | BublOscClient.prototype.bublUpdate = function (updateFileBin) { 26 | var bublUpdateUrl = this.serverAddress + '/osc/_bublUpdate' 27 | return makeHttpRequest('POST', bublUpdateUrl, this.applicationOctetType, updateFileBin) 28 | } 29 | 30 | // OSC BUBL GET IMAGE 31 | BublOscClient.prototype.bublGetImage = function (fileUri) { 32 | var bublGetImageUrl = this.serverAddress + '/osc/_bublGetImage/' 33 | return makeHttpRequest('GET', bublGetImageUrl + encodeURIComponent(fileUri), this.applicationJsonType) 34 | } 35 | 36 | // BUBL STOP 37 | BublOscClient.prototype.bublStop = function (commandId) { 38 | var bublStopUrl = this.serverAddress + '/osc/commands/_bublStop' 39 | return makeHttpRequest('POST', bublStopUrl, this.applicationJsonType, { id: commandId }) 40 | } 41 | 42 | // BUBL POLL 43 | BublOscClient.prototype.bublPoll = function (commandId, fingerprint, waitTimeout) { 44 | var bublPollUrl = this.serverAddress + '/osc/commands/_bublPoll' 45 | return makeHttpRequest('POST', bublPollUrl, this.applicationJsonType, { 46 | id: commandId, 47 | fingerprint: fingerprint, 48 | waitTimeout: waitTimeout 49 | }) 50 | } 51 | 52 | // BUBL COMMANDS CAPTURE VIDEO 53 | BublOscClient.prototype.bublCaptureVideo = function (sessionId, statusCallback) { 54 | return commandsExecute.apply(this, ['camera._bublCaptureVideo', { sessionId: sessionId }, statusCallback]) 55 | } 56 | 57 | // OSC COMMANDS BUBL TIMELAPSE 58 | BublOscClient.prototype.bublTimelapse = function (sessionId, statusCallback) { 59 | return commandsExecute.apply(this, ['camera._bublTimelapse', { sessionId: sessionId }, statusCallback]) 60 | } 61 | 62 | // OSC COMMANDS BUBL STREAM 63 | BublOscClient.prototype.bublStream = function (sessionId, statusCallback) { 64 | return commandsExecute.apply(this, ['camera._bublStream', { sessionId: sessionId }, statusCallback]) 65 | } 66 | 67 | // OSC COMMANDS BUBL SHUTDOWN 68 | BublOscClient.prototype.bublShutdown = function (sessionId, shutdownDelay) { 69 | return commandsExecute.apply(this, ['camera._bublShutdown', { sessionId: sessionId, shutdownDelay: shutdownDelay }]) 70 | } 71 | 72 | module.exports = BublOscClient 73 | -------------------------------------------------------------------------------- /tests/BublOscClient-spec.js: -------------------------------------------------------------------------------- 1 | /* global describe, it, beforeEach */ 2 | 3 | var BublOscClient = require('../lib/BublOscClient') 4 | var OscClient = require('../lib/OscClient') 5 | var assert = require('assert') 6 | 7 | describe('BublOscClient', function () { 8 | describe('instantiation', function () { 9 | var bublClient 10 | var client 11 | beforeEach(function () { 12 | bublClient = new BublOscClient() 13 | client = new OscClient() 14 | }) 15 | 16 | it('should extend OscClient', function () { 17 | for (var clientProp in client) { 18 | if (typeof client[clientProp] === 'function') { 19 | assert.equal(typeof bublClient[clientProp], 'function') 20 | } 21 | } 22 | }) 23 | }) 24 | 25 | describe('bubl Methods', function () { 26 | var bublClient 27 | 28 | beforeEach(function () { 29 | bublClient = new BublOscClient() 30 | }) 31 | 32 | describe('bublUpdate', function () { 33 | it('returns a promise', function () { 34 | var prom = bublClient.bublUpdate() 35 | assert.equal(typeof prom.then, 'function') 36 | assert.equal(typeof prom.catch, 'function') 37 | }) 38 | }) 39 | 40 | describe('bublGetImage', function () { 41 | it('returns a promise', function () { 42 | var prom = bublClient.bublGetImage() 43 | assert.equal(typeof prom.then, 'function') 44 | assert.equal(typeof prom.catch, 'function') 45 | }) 46 | }) 47 | 48 | describe('bublStop', function () { 49 | it('returns a promise', function () { 50 | var prom = bublClient.bublStop() 51 | assert.equal(typeof prom.then, 'function') 52 | assert.equal(typeof prom.catch, 'function') 53 | }) 54 | }) 55 | 56 | describe('bublPoll', function () { 57 | it('returns a promise', function () { 58 | var prom = bublClient.bublPoll() 59 | assert.equal(typeof prom.then, 'function') 60 | assert.equal(typeof prom.catch, 'function') 61 | }) 62 | }) 63 | 64 | describe('bublCaptureVideo', function () { 65 | it('returns a promise', function () { 66 | var prom = bublClient.bublCaptureVideo() 67 | assert.equal(typeof prom.then, 'function') 68 | assert.equal(typeof prom.catch, 'function') 69 | }) 70 | }) 71 | 72 | describe('bublTimelapse', function () { 73 | it('returns a promise', function () { 74 | var prom = bublClient.bublTimelapse() 75 | assert.equal(typeof prom.then, 'function') 76 | assert.equal(typeof prom.catch, 'function') 77 | }) 78 | }) 79 | 80 | describe('bublStream', function () { 81 | it('returns a promise', function () { 82 | var prom = bublClient.bublStream() 83 | assert.equal(typeof prom.then, 'function') 84 | assert.equal(typeof prom.catch, 'function') 85 | }) 86 | }) 87 | 88 | describe('bublShutdown', function () { 89 | it('returns a promise', function () { 90 | var prom = bublClient.bublShutdown() 91 | assert.equal(typeof prom.then, 'function') 92 | assert.equal(typeof prom.catch, 'function') 93 | }) 94 | }) 95 | }) 96 | }) 97 | -------------------------------------------------------------------------------- /lib/OscClient.js: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Bubl Technology Inc. 2 | // 3 | // Licensed under the MIT license 4 | // . 5 | // This file may not be copied, modified, or distributed 6 | // except according to those terms. 7 | 8 | var makeHttpRequest = require('./makeHttpRequest') 9 | var commandsExecute = require('./commandsExecute') 10 | 11 | var OscClient = function (domainArg, portArg) { 12 | 'use strict' 13 | var domain = domainArg || 'localhost' 14 | var port = portArg || 8000 15 | 16 | this.serverAddress = 'http://' + domain + ':' + port 17 | 18 | // HTTP CONTENT TYPES 19 | this.applicationJsonType = 'application/json; charset=utf-8' 20 | this.applicationOctetType = 'application/octet-stream' 21 | 22 | // OSC INFO 23 | var infoUrl = this.serverAddress + '/osc/info' 24 | 25 | this.getInfo = function () { 26 | return makeHttpRequest('GET', infoUrl, this.applicationJsonType) 27 | } 28 | 29 | // OSC STATE 30 | var stateUrl = this.serverAddress + '/osc/state' 31 | 32 | this.getState = function () { 33 | return makeHttpRequest('POST', stateUrl, this.applicationJsonType) 34 | } 35 | 36 | // OSC CHECK FOR UPDATES 37 | var checkForUpdatesUrl = this.serverAddress + '/osc/checkForUpdates' 38 | 39 | this.checkForUpdates = function (stateFingerprint, waitTimeout) { 40 | return makeHttpRequest('POST', checkForUpdatesUrl, this.applicationJsonType, { 41 | stateFingerprint: stateFingerprint, 42 | waitTimeout: waitTimeout 43 | }) 44 | } 45 | 46 | // OSC COMMANDS START SESSION 47 | this.startSession = function (timeout) { 48 | return commandsExecute.apply(this, ['camera.startSession', { timeout: timeout }]) 49 | } 50 | 51 | // OSC COMMANDS UPDATE SESSION 52 | this.updateSession = function (sessionId, timeout) { 53 | return commandsExecute.apply(this, ['camera.updateSession', { sessionId: sessionId, timeout: timeout }]) 54 | } 55 | 56 | // OSC COMMANDS CLOSE SESSION 57 | this.closeSession = function (sessionId) { 58 | return commandsExecute.apply(this, ['camera.closeSession', { sessionId: sessionId }]) 59 | } 60 | 61 | // OSC COMMANDS TAKE PICTURE 62 | this.takePicture = function (sessionId, statusCallback) { 63 | return commandsExecute.apply(this, ['camera.takePicture', { sessionId: sessionId }, statusCallback]) 64 | } 65 | 66 | // OSC COMMANDS LIST IMAGES 67 | this.listImages = function (entryCount, includeThumb, maxSize, continuationToken) { 68 | return commandsExecute.apply(this, ['camera.listImages', { 69 | entryCount: entryCount, 70 | includeThumb: includeThumb, 71 | maxSize: maxSize, 72 | continuationToken: continuationToken 73 | }]) 74 | } 75 | 76 | // OSC COMMANDS DELETE 77 | this.delete = function (fileUri) { 78 | return commandsExecute.apply(this, ['camera.delete', { fileUri: fileUri }]) 79 | } 80 | 81 | // OSC COMMANDS GET IMAGE 82 | this.getImage = function (fileUri, maxSize) { 83 | return commandsExecute.apply(this, ['camera.getImage', { fileUri: fileUri, maxSize: maxSize }]) 84 | } 85 | 86 | // OSC COMMANDS GET METADATA 87 | this.getMetadata = function (fileUri) { 88 | return commandsExecute.apply(this, ['camera.getMetadata', { fileUri: fileUri }]) 89 | } 90 | 91 | // OSC COMMANDS SET OPTIONS 92 | this.setOptions = function (sessionId, options) { 93 | return commandsExecute.apply(this, ['camera.setOptions', { sessionId: sessionId, options: options }]) 94 | } 95 | 96 | // OSC COMMANDS GET OPTIONS 97 | this.getOptions = function (sessionId, optionNames) { 98 | return commandsExecute.apply(this, ['camera.getOptions', { sessionId: sessionId, optionNames: optionNames }]) 99 | } 100 | 101 | // OSC COMMANDS STATUS 102 | var commandsStatusUrl = this.serverAddress + '/osc/commands/status' 103 | 104 | this.commandsStatus = function (commandId) { 105 | return makeHttpRequest('POST', commandsStatusUrl, this.applicationJsonType, { id: commandId }) 106 | } 107 | } 108 | 109 | module.exports = OscClient 110 | -------------------------------------------------------------------------------- /tests/poll-spec.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 3 | var poll = require('../lib/poll') 4 | var assert = require('assert') 5 | var sinon = require('sinon') 6 | var Q = require('q') 7 | 8 | describe('poll', function () { 9 | describe('defaults', function () { 10 | it('has a default pollPeriod of 2000 milliseconds', function () { 11 | assert.equal(poll.pollPeriod, 2000) 12 | }) 13 | }) 14 | 15 | describe('commandStatus', function () { 16 | it('calls commandsStatus on client passed in', function (done) { 17 | var fakeClient = { 18 | commandsStatus: sinon.stub().returns({ then: function () {} }) 19 | } 20 | var promise = { 21 | then: function () {}, 22 | reject: function () {}, 23 | resolve: function () {} 24 | } 25 | 26 | poll.pollPeriod = 1 27 | poll.commandStatus(fakeClient, 12, promise, Date.now(), null) 28 | setTimeout(function () { 29 | assert.equal(fakeClient.commandsStatus.called, true) 30 | done() 31 | }, 100) 32 | }) 33 | 34 | it('sends the command id to commandStatus', function (done) { 35 | var fakeClient = { 36 | commandsStatus: sinon.stub().returns({ then: function () {} }) 37 | } 38 | var promise = { 39 | then: function () {}, 40 | reject: function () {}, 41 | resolve: function () {} 42 | } 43 | 44 | poll.pollPeriod = 1 45 | poll.commandStatus(fakeClient, 15, promise, Date.now(), null) 46 | setTimeout(function () { 47 | assert(fakeClient.commandsStatus.calledWith(15)) 48 | done() 49 | }, 100) 50 | }) 51 | 52 | it('resolves promise if result.state is not in progress', function (done) { 53 | var spyOne = sinon.spy() 54 | var spyTwo = sinon.spy() 55 | var fakePromiseOne = { 56 | then: function (cb) { 57 | cb({ body: { state: 'finished' } }) 58 | } 59 | } 60 | var fakeClient = { 61 | commandsStatus: sinon.stub().returns(fakePromiseOne) 62 | } 63 | var fakePromiseTwo = { 64 | then: function () {}, 65 | reject: function () {}, 66 | resolve: function () { 67 | spyOne() 68 | } 69 | } 70 | 71 | poll.pollPeriod = 1 72 | poll.commandStatus(fakeClient, 15, fakePromiseTwo, Date.now(), spyTwo) 73 | setTimeout(function () { 74 | assert.equal(spyOne.called, true) 75 | assert.equal(spyTwo.called, false) 76 | done() 77 | }, 100) 78 | }) 79 | 80 | it('rejects promise if there is an error object on the response body', function (done) { 81 | var rejectSpy = sinon.spy() 82 | var resolveSpy = sinon.spy() 83 | var fakePromiseOne = Q.reject({ error: { message: 'bad error!' } }) 84 | var fakePromise = { 85 | then: function () {}, 86 | reject: function () { 87 | rejectSpy() 88 | }, 89 | resolve: function () { 90 | resolveSpy() 91 | } 92 | } 93 | var fakeClient = { 94 | commandsStatus: sinon.stub().returns(fakePromiseOne) 95 | } 96 | poll.pollPeriod = 1 97 | poll.commandStatus(fakeClient, 12, fakePromise, Date.now(), null) 98 | setTimeout(function () { 99 | assert.equal(rejectSpy.called, true) 100 | assert.equal(resolveSpy.called, false) 101 | done() 102 | }, 100) 103 | }) 104 | 105 | it('calls statusCallback if state is inProgress', function (done) { 106 | var spyOne = sinon.spy() 107 | var spyTwo = sinon.spy() 108 | var fakePromiseOne = Q({ state: 'inProgress' }) 109 | var fakeClient = { 110 | commandsStatus: sinon.stub().returns(fakePromiseOne) 111 | } 112 | var fakePromiseTwo = { 113 | then: function () {}, 114 | reject: function () {}, 115 | resolve: function () { 116 | spyOne() 117 | } 118 | } 119 | 120 | poll.pollPeriod = 10 121 | poll.commandStatus(fakeClient, 15, fakePromiseTwo, Date.now(), spyTwo) 122 | setTimeout(function () { 123 | assert.equal(spyOne.called, false) 124 | assert.equal(spyTwo.called, true) 125 | done() 126 | }, 100) 127 | }) 128 | }) 129 | }) 130 | -------------------------------------------------------------------------------- /tests/OscClient-spec.js: -------------------------------------------------------------------------------- 1 | /* global describe, it, beforeEach */ 2 | 3 | var assert = require('assert') 4 | var OscClient = require('../lib/OscClient') 5 | 6 | describe('OscClient', function () { 7 | var client 8 | 9 | describe('instantiation', function () { 10 | describe('defaults', function () { 11 | beforeEach(function () { 12 | client = new OscClient() 13 | }) 14 | 15 | it('should have an applicationOctetType property', function () { 16 | assert.equal(client.applicationOctetType, 'application/octet-stream') 17 | }) 18 | 19 | it('should have an applicationJsonType property', function () { 20 | assert.equal(client.applicationJsonType, 'application/json; charset=utf-8') 21 | }) 22 | 23 | it('should form a proper serverAddress with the default host and port', function () { 24 | assert.equal(client.serverAddress, 'http://localhost:8000') 25 | }) 26 | }) 27 | 28 | describe('custom port', function () { 29 | beforeEach(function () { 30 | client = new OscClient(null, '9999') 31 | }) 32 | 33 | it('should form a proper serverAddress with the host and custom port', function () { 34 | assert.equal(client.serverAddress, 'http://localhost:9999') 35 | }) 36 | }) 37 | describe('custom host', function () { 38 | beforeEach(function () { 39 | client = new OscClient('192.168.1.100', '1234') 40 | }) 41 | 42 | it('should form a proper serverAddress with the custom host', function () { 43 | assert.equal(client.serverAddress, 'http://192.168.1.100:1234') 44 | }) 45 | }) 46 | }) 47 | 48 | describe('instance methods', function () { 49 | beforeEach(function () { 50 | client = new OscClient() 51 | }) 52 | 53 | describe('getInfo', function () { 54 | it('returns a promise', function () { 55 | var prom = client.getInfo() 56 | assert.equal(typeof prom.then, 'function') 57 | assert.equal(typeof prom.catch, 'function') 58 | }) 59 | }) 60 | 61 | describe('getState', function () { 62 | it('returns a promise', function () { 63 | var prom = client.getState() 64 | assert.equal(typeof prom.then, 'function') 65 | assert.equal(typeof prom.catch, 'function') 66 | }) 67 | }) 68 | 69 | describe('checkForUpdates', function () { 70 | it('returns a promise', function () { 71 | var prom = client.checkForUpdates() 72 | assert.equal(typeof prom.then, 'function') 73 | assert.equal(typeof prom.catch, 'function') 74 | }) 75 | }) 76 | 77 | describe('startSession', function () { 78 | it('returns a promise', function () { 79 | var prom = client.startSession() 80 | assert.equal(typeof prom.then, 'function') 81 | assert.equal(typeof prom.catch, 'function') 82 | }) 83 | }) 84 | 85 | describe('updateSession', function () { 86 | it('returns a promise', function () { 87 | var prom = client.updateSession() 88 | assert.equal(typeof prom.then, 'function') 89 | assert.equal(typeof prom.catch, 'function') 90 | }) 91 | }) 92 | 93 | describe('closeSession', function () { 94 | it('returns a promise', function () { 95 | var prom = client.closeSession() 96 | assert.equal(typeof prom.then, 'function') 97 | assert.equal(typeof prom.catch, 'function') 98 | }) 99 | }) 100 | 101 | describe('takePicture', function () { 102 | it('returns a promise', function () { 103 | var prom = client.takePicture() 104 | assert.equal(typeof prom.then, 'function') 105 | assert.equal(typeof prom.catch, 'function') 106 | }) 107 | }) 108 | 109 | describe('listImages', function () { 110 | it('returns a promise', function () { 111 | var prom = client.listImages() 112 | assert.equal(typeof prom.then, 'function') 113 | assert.equal(typeof prom.catch, 'function') 114 | }) 115 | }) 116 | 117 | describe('delete', function () { 118 | it('returns a promise', function () { 119 | var prom = client.delete() 120 | assert.equal(typeof prom.then, 'function') 121 | assert.equal(typeof prom.catch, 'function') 122 | }) 123 | }) 124 | 125 | describe('getImage', function () { 126 | it('returns a promise', function () { 127 | var prom = client.getImage() 128 | assert.equal(typeof prom.then, 'function') 129 | assert.equal(typeof prom.catch, 'function') 130 | }) 131 | }) 132 | 133 | describe('getMetadata', function () { 134 | it('returns a promise', function () { 135 | var prom = client.getMetadata() 136 | assert.equal(typeof prom.then, 'function') 137 | assert.equal(typeof prom.catch, 'function') 138 | }) 139 | }) 140 | 141 | describe('setOptions', function () { 142 | it('returns a promise', function () { 143 | var prom = client.setOptions() 144 | assert.equal(typeof prom.then, 'function') 145 | assert.equal(typeof prom.catch, 'function') 146 | }) 147 | }) 148 | 149 | describe('getOptions', function () { 150 | it('returns a promise', function () { 151 | var prom = client.getOptions() 152 | assert.equal(typeof prom.then, 'function') 153 | assert.equal(typeof prom.catch, 'function') 154 | }) 155 | }) 156 | }) 157 | }) 158 | --------------------------------------------------------------------------------