From b4ef6439e9e7e655e9d72bfd76a8e071f6e68aa4 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 24 Apr 2014 15:59:31 -0400 Subject: [PATCH] JsonDataApi=>JsonResponse trait This cuts down on the code significantly and makes more sense, since we would not want to veil the API of any classes that want to use this. --- .../{JsonDataApi.js => JsonResponse.js} | 33 +++--------- ...JsonDataApiTest.js => JsonResponseTest.js} | 52 +++++++------------ 2 files changed, 24 insertions(+), 61 deletions(-) rename src/dapi/format/{JsonDataApi.js => JsonResponse.js} (70%) rename test/dapi/format/{JsonDataApiTest.js => JsonResponseTest.js} (68%) diff --git a/src/dapi/format/JsonDataApi.js b/src/dapi/format/JsonResponse.js similarity index 70% rename from src/dapi/format/JsonDataApi.js rename to src/dapi/format/JsonResponse.js index 97763b5..a9fc714 100644 --- a/src/dapi/format/JsonDataApi.js +++ b/src/dapi/format/JsonResponse.js @@ -19,51 +19,30 @@ * along with this program. If not, see . */ -var Class = require( 'easejs' ).Class, +var Trait = require( 'easejs' ).Trait, DataApi = require( '../DataApi' ); /** * Processes DataApi return data as JSON */ -module.exports = Class( 'JsonDataApi' ) +module.exports = Trait( 'JsonResponse' ) .implement( DataApi ) .extend( { /** - * DataAPI to decorate - * @type {DataApi} - */ - 'private _dapi': null, - - - /** - * Decorate provided DataAPI to parse response data as JSON + * Processes response as JSON * - * @param {DataApi} dapi DataApi to decorate - */ - __construct: function( dapi ) - { - if ( !( Class.isA( DataApi, dapi ) ) ) - { - throw TypeError( "Expecting DataApi" ); - } - - this._dapi = dapi; - }, - - - /** - * Proxies request to encapsulated DataApi and parses the result as JSON + * Will return an error if the response is not valid JSON. * * @param {string} data binary data to transmit * @param {function(?Error,*)} callback continuation upon reply * * @return {DataApi} self */ - 'public request': function( data, callback ) + 'abstract override public request': function( data, callback ) { - this._dapi.request( data, function( err, resp ) + this.__super( data, function( err, resp ) { if ( err !== null ) { diff --git a/test/dapi/format/JsonDataApiTest.js b/test/dapi/format/JsonResponseTest.js similarity index 68% rename from test/dapi/format/JsonDataApiTest.js rename to test/dapi/format/JsonResponseTest.js index 4ac363a..ede3046 100644 --- a/test/dapi/format/JsonDataApiTest.js +++ b/test/dapi/format/JsonResponseTest.js @@ -23,37 +23,19 @@ var dapi = require( '../../../' ).dapi, expect = require( 'chai' ).expect, Class = require( 'easejs' ).Class, DataApi = dapi.DataApi, - Sut = dapi.format.JsonDataApi; + Sut = dapi.format.JsonResponse; -describe( 'JsonDataApi', function() +describe( 'dapi.format.JsonRepsonse trait', function() { - it( 'errors if not provided a DataApi', function() - { - expect( function() - { - Sut( {} ); - } ).to.throw( TypeError, 'DataApi' ); - } ); - - - it( 'accepts a DataApi', function() - { - expect( function() - { - Sut( _createStubDapi( null, '' ) ); - } ).to.not.throw( TypeError ); - } ); - - describe( '.request', function() { it( 'passes data to encapsulated DataApi', function() { - var stubs = _createStubDapi( null, '0' ), + var stubs = _createStubbedDapi( null, '0' ), expected = {}; - Sut( stubs ).request( expected, function() {} ); + stubs.request( expected, function() {} ); expect( stubs.given ).to.equal( expected ); } ); @@ -61,19 +43,21 @@ describe( 'JsonDataApi', function() it( 'converts response to JSON', function( done ) { var raw = '{"foo": "bar"}'; - Sut( _createStubDapi( null, raw ) ).request( '', function( err, data ) - { - // should have been converted to JSON - expect( data ).to.deep.equal( { foo: 'bar' } ); - expect( err ).to.equal( null ); - done(); - } ); + + _createStubbedDapi( null, raw ) + .request( '', function( err, data ) + { + // should have been converted to JSON + expect( data ).to.deep.equal( { foo: 'bar' } ); + expect( err ).to.equal( null ); + done(); + } ); } ); it( 'returns error if JSON parsing fails', function( done ) { - Sut( _createStubDapi( null, 'ERR' ) ) + _createStubbedDapi( null, 'ERR' ) .request( '', function( err, data ) { expect( err ).to.be.instanceOf( Error ); @@ -87,7 +71,7 @@ describe( 'JsonDataApi', function() { var e = Error( 'foo' ); - Sut( _createStubDapi( e, '0' ) ) + _createStubbedDapi( e, '0' ) .request( '', function( err, data ) { // data should also be cleared out @@ -100,17 +84,17 @@ describe( 'JsonDataApi', function() } ); -function _createStubDapi( err, resp ) +function _createStubbedDapi( err, resp ) { return Class.implement( DataApi ).extend( { given: null, - request: function( data, callback ) + 'virtual public request': function( data, callback ) { this.given = data; callback( err, resp ); } - } )(); + } ).use( Sut )(); }