From 333dc881ea295eea597daeb3f49e66e68bf18091 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Fri, 19 Sep 2014 11:59:11 -0700 Subject: [PATCH] Move Cylon API config to the Cylon.configure block --- examples/api/api.js | 6 +++++- examples/api/api.markdown | 6 +++++- examples/sphero-pebble-sf/sphero-pebble-sf.js | 6 +++++- examples/sphero-pebble-sf/sphero-pebble-sf.markdown | 6 +++++- lib/cylon.js | 12 ++++-------- test/specs/cylon.spec.js | 5 +++-- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/examples/api/api.js b/examples/api/api.js index 1e38655..a09e77e 100644 --- a/examples/api/api.js +++ b/examples/api/api.js @@ -1,6 +1,10 @@ var Cylon = require('../..'); -Cylon.api({ host: '0.0.0.0', port: '8080' }); +Cylon.config({ + api: { host: '0.0.0.0', port: '8080' } +}); + +Cylon.api(); var bots = [ { port: '/dev/rfcomm0', name: 'Thelma' }, diff --git a/examples/api/api.markdown b/examples/api/api.markdown index 5a70482..89f9e33 100644 --- a/examples/api/api.markdown +++ b/examples/api/api.markdown @@ -13,7 +13,11 @@ First, let's import Cylon: Next up, we'll configure the API Cylon will serve, telling it to serve on port `8080`. - Cylon.api({host: '0.0.0.0', port: '8080'}); + Cylon.config({ + api: { host: '0.0.0.0', port: '8080' } + }); + + Cylon.api(); Since we're making two very similar robots (Spheros, in this case), let's put the different parts of each robot in objects so we can initialize them later. diff --git a/examples/sphero-pebble-sf/sphero-pebble-sf.js b/examples/sphero-pebble-sf/sphero-pebble-sf.js index d38bcd5..324ea12 100644 --- a/examples/sphero-pebble-sf/sphero-pebble-sf.js +++ b/examples/sphero-pebble-sf/sphero-pebble-sf.js @@ -4,7 +4,11 @@ var __bind = function(fn, me) { var Cylon = require('../..'); -Cylon.api({ host: '0.0.0.0', port: '8080' }); +Cylon.config({ + api: { host: '0.0.0.0', port: '8080' } +}); + +Cylon.api(); var PebbleRobot = (function() { function PebbleRobot() { this.message = __bind(this.message, this); } diff --git a/examples/sphero-pebble-sf/sphero-pebble-sf.markdown b/examples/sphero-pebble-sf/sphero-pebble-sf.markdown index 9aab7cf..9f92c64 100644 --- a/examples/sphero-pebble-sf/sphero-pebble-sf.markdown +++ b/examples/sphero-pebble-sf/sphero-pebble-sf.markdown @@ -7,7 +7,11 @@ First, let's import Cylon: Next up, we'll configure the API Cylon will serve, telling it to serve on port `8080`. - Cylon.api({ host: '0.0.0.0', port: '8080' }); + Cylon.config({ + api: { host: '0.0.0.0', port: '8080' } + }); + + Cylon.api(); We'll also setup a convenince function for some binding we'll need to do later: diff --git a/lib/cylon.js b/lib/cylon.js index 2551f3f..b70f03c 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -61,17 +61,13 @@ Cylon.robot = function robot(opts) { // Public: Creates a new API based on passed options // -// opts - object containing API options -// // Returns nothing -Cylon.api = function api(opts) { - if (opts == null) { - opts = {}; - } - +Cylon.api = function api() { var API = require('./api'); - this.api_instance = new API(opts); + var config = Utils.fetch(Config, 'api', {}); + + this.api_instance = new API(config); this.api_instance.listen(); }; diff --git a/test/specs/cylon.spec.js b/test/specs/cylon.spec.js index f03e3ef..5a8041d 100644 --- a/test/specs/cylon.spec.js +++ b/test/specs/cylon.spec.js @@ -72,8 +72,9 @@ describe("Cylon", function() { expect(Cylon.api_instance).to.be.an.instanceOf(API); }); - it('passes arguments to the API constructor', function() { - Cylon.api({ port: '1234' }); + it('passes configuration to the API constructor', function() { + Cylon.config({ api: { port: '1234' }}); + Cylon.api(); expect(Cylon.api_instance.port).to.be.eql('1234'); }) });