ebay-lookup
Advanced tools
Comparing version 0.2.1 to 0.3.0
83
index.js
@@ -5,2 +5,3 @@ /** | ||
var request = require('superagent') | ||
, util = require('util') | ||
, _ = require('underscore') | ||
@@ -22,2 +23,4 @@ , path = require('JSONPath').eval | ||
var Ebay = function (itemId) { | ||
this._filters = {}; | ||
if ('object' === typeof itemId) { | ||
@@ -100,21 +103,36 @@ if (itemId.keywords) this.keywords = itemId.keywords, this.mode = 'search'; | ||
]; | ||
var defaultListing = 'All' | ||
Ebay.prototype.type = function (type) { | ||
var results = _.where(listing, {name: type.toLowerCase()}); | ||
// Set listing type | ||
this.listingType = results.length ? results[0].id : null; | ||
// Choke on invalid listing type | ||
if (!results.length) throw new Error('Invalid listing type'); | ||
// Return this for chaining | ||
// Add to filters | ||
this._filters['ListingType'] = [results[0].id]; | ||
// Chain | ||
return this; | ||
}; | ||
var defaultCondition = 'Unspecified'; | ||
Ebay.prototype.condition = function (cond) { | ||
return this._condition = capitalize(cond), this; | ||
return this._filters['Condition'] = [capitalize(cond)], this; | ||
}; | ||
Ebay.prototype.price = function (range) { | ||
// Convert to array if necessary | ||
range = _.isString(range) ? range.split('..') : range; | ||
// Add limits to filters | ||
if (range[0]) this._filters['MinPrice'] = [range[0]]; | ||
if (range[1]) this._filters['MaxPrice'] = [range[1]]; | ||
// Chain | ||
return this; | ||
}; | ||
Ebay.prototype.done = function (cb) { | ||
return request | ||
.get(endpoint) | ||
var r = request.get(endpoint); | ||
// Add easy fields | ||
r | ||
.query({'OPERATION-NAME': 'findItemsAdvanced'}) | ||
@@ -129,21 +147,40 @@ .query({'SERVICE-VERSION': '1.0.0'}) | ||
.query({'affiliate.trackingId': this.trackingId}) | ||
.query({'affiliate.networkId': this.networkId || defaultNetwork}) | ||
.query({'itemFilter(0).name': 'ListingType'}) | ||
.query({'itemFilter(0).value(0)': this.listingType || defaultListing}) | ||
.query({'itemFilter(1).name': 'Condition'}) | ||
.query({'itemFilter(1).value(0)': this._condition || defaultCondition}) | ||
.end(function (err, res) { | ||
.query({'affiliate.networkId': this.networkId || defaultNetwork}); | ||
// Add filters | ||
_ | ||
.chain(this._filters) | ||
.pairs() | ||
.map(map) | ||
.flatten() | ||
.each(r.query, r); | ||
function map (x, i) { | ||
var nameTemplate = 'itemFilter(%d).name=%s' | ||
, valueTemplate = 'itemFilter(%d).value(%d)=%s'; | ||
// Holy shit what is that | ||
return [ | ||
util.format(nameTemplate, i, x[0]), | ||
_.map(x[1], function (y, j) { | ||
return util.format(valueTemplate, i, j, y); | ||
}) | ||
]; | ||
} | ||
// Run it | ||
r.end(function (err, res) { | ||
if (err) return cb(err); | ||
return parse(res.text, function (err, p) { | ||
// Parsing errors | ||
if (err) return cb(err); | ||
return parse(res.text, function (err, p) { | ||
// Parsing errors | ||
if (err) return cb(err); | ||
// API errors | ||
if (err = parseErr(p)) return cb(err); | ||
// API errors | ||
if (err = parseErr(p)) return cb(err); | ||
// Return result | ||
return cb(null, parseResults(p, extractions)); | ||
}); | ||
// Return result | ||
return cb(null, parseResults(p, extractions)); | ||
}); | ||
}); | ||
}; | ||
@@ -150,0 +187,0 @@ |
{ | ||
"name": "ebay-lookup", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
1
t.js
@@ -10,2 +10,3 @@ var ebay = require('./') | ||
.condition('new') | ||
.price('1000000..10000') | ||
.done(function (err, res) { | ||
@@ -12,0 +13,0 @@ console.dir(err); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25204444
229