ebay-api
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -12,2 +12,3 @@ var | ||
defaultParser = require('./parser').parseResponse, | ||
deepToArray = require('./deep-to-array').deepToArray, | ||
@@ -20,45 +21,2 @@ _errors = require('./errors'), | ||
/** | ||
* handle quirk of 'xml' module: | ||
* need arrays all the way down, | ||
* and objects can only have a single key:value, | ||
* e.g. {k1:v1,k2:v2} need to become [{k1:[v1]}, {k2:[v2]}]. | ||
* @see tests in xml-request.test.js. | ||
*/ | ||
exports._deepToArray = function _deepToArray(obj) { | ||
var key, value, arr = []; | ||
if (_.isArray(obj)) { | ||
// not sure about this: arrays within objects are handled below; | ||
// top level should never be an array; | ||
// but seems ok. change/fix if a scenario comes up that this breaks. | ||
return obj.map(function(value) { | ||
return _deepToArray(value); | ||
}); | ||
} | ||
else if (_.isObject(obj)) { | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
value = obj[key]; | ||
// `{foo: [a,b,c]}` => `[{foo:a},{foo:b},{foo:c}]` | ||
if (_.isArray(value)) { | ||
value.forEach(function(subValue) { | ||
arr.push(_.set({}, key, _deepToArray(subValue))); | ||
}); | ||
} | ||
else { | ||
arr.push(_.set({}, key, _deepToArray(value))); | ||
} | ||
} | ||
} | ||
} | ||
else { | ||
arr = [obj]; | ||
} | ||
return arr; | ||
}; | ||
/* | ||
@@ -89,3 +47,3 @@ build XML input for XML requests (POST). | ||
// concat | ||
root.push.apply(root, exports._deepToArray(params)); | ||
root.push.apply(root, deepToArray(params)); | ||
@@ -148,4 +106,8 @@ debug('Converting to XML', data); | ||
debug('response', {statusCode: response.statusCode, body: response.body}); | ||
debug('response', error ? {error: error} : {statusCode: response.statusCode, body: response.body}); | ||
if (error) { | ||
return callback(error); | ||
} | ||
// this is tricky -- API should return 200 with body in every valid scenario, | ||
@@ -152,0 +114,0 @@ // so a non-200 probably means something's wrong on the client side with the request. |
{ | ||
"name": "ebay-api", | ||
"description": "eBay API Client", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"homepage": "https://github.com/newleafdigital/nodejs-ebay-api", | ||
@@ -6,0 +6,0 @@ "author": "Ben Buckman <ben@newleafdigital.com> (http://newleafdigital.com)", |
@@ -394,100 +394,1 @@ var | ||
describe('`_deepToArray`', function() { | ||
var _deepToArray = require('../lib/xml-request')._deepToArray; | ||
it('converts plain value to array', function() { | ||
expect(_deepToArray(5)).to.deep.equal([5]); | ||
}); | ||
it('converts object to array', function() { | ||
expect(_deepToArray({ | ||
'a': '1', | ||
'b': '2' | ||
})).to.deep.equal( | ||
[ | ||
{'a': ['1']}, | ||
{'b': ['2']} | ||
] | ||
); | ||
}); | ||
it('splits out array elements into objects', function() { | ||
expect(_deepToArray( | ||
{ | ||
thing: ['a', 'b', 'c'], | ||
itemFilter: [ | ||
{name: 'FreeShippingOnly', value: 'true'}, | ||
{name: 'MaxPrice', value: '150'}, | ||
] | ||
} | ||
)).to.deep.equal( | ||
[ | ||
{thing: ['a']}, | ||
{thing: ['b']}, | ||
{thing: ['c']}, | ||
{ | ||
itemFilter: [ | ||
{name: ['FreeShippingOnly']}, | ||
{value: ['true']}, | ||
] | ||
}, | ||
{ | ||
itemFilter: [ | ||
{name: ['MaxPrice']}, | ||
{value: ['150']}, | ||
] | ||
} | ||
] | ||
); | ||
}); | ||
it('converts everything recursively to arrays', function() { | ||
expect(_deepToArray({ | ||
'Thing': { | ||
'Has': { | ||
'Weird': 'Structure', | ||
'Strange': 'Data' | ||
} | ||
}, | ||
'Things': [ | ||
{'foo': 'bar'} | ||
], | ||
'More': 'Things', | ||
'Finally': ['Thing1', 'Thing2'], | ||
'Already': [{'An': 'Array'}] | ||
})) | ||
.to.deep.equal( | ||
[ | ||
{ | ||
'Thing': [{ | ||
'Has': [ | ||
{'Weird': ['Structure']}, | ||
{'Strange': ['Data']} | ||
] | ||
}] | ||
}, | ||
{ | ||
'Things': [ | ||
{'foo': ['bar']} | ||
] | ||
}, | ||
{ | ||
'More': ['Things'] | ||
}, | ||
{ | ||
'Finally': ['Thing1'] | ||
}, | ||
{ | ||
'Finally': ['Thing2'] | ||
}, | ||
{ | ||
'Already': [ | ||
{'An': ['Array']} | ||
] | ||
} | ||
] | ||
); | ||
}); | ||
}); |
48228
23
1166