UNPKG

tender

Version:

Tender API client

228 lines (206 loc) 6.28 kB
// Generated by CoffeeScript 1.4.0 var TenderRequest, async, request, tenderRequest, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; request = require('request'); async = require('async'); TenderRequest = (function() { function TenderRequest(client, options, callback) { var _this = this; this.client = client; this.options = options; this.callback = callback; this.finalize = __bind(this.finalize, this); this.query = __bind(this.query, this); this.getSinglePage = __bind(this.getSinglePage, this); this.getAdditionalPages = __bind(this.getAdditionalPages, this); this.getFirstPage = __bind(this.getFirstPage, this); this.options.max = this.options.max || 1000; this.options.qs = this.options.qs || {}; if (!(this.client.subdomain && typeof this.client.subdomain === "string")) { return this.callback(new Error("No subdomain specified")); } if (this.client.token) { this.options.qs.auth = this.client.token; } else if (this.client.username || this.client.password) { if (!this.client.username) { return this.callback(new Error("No username specified")); } if (!this.client.password) { return this.callback(new Error("No password specified")); } } else { return this.callback(new Error("No authentication specified")); } this.run(function(err, data) { return _this.callback(err, data); }); } TenderRequest.prototype.run = function(callback) { var _this = this; async.waterfall([this.getFirstPage, this.getAdditionalPages], this.finalize); return function(err, result) { return callback(err, result); }; }; TenderRequest.prototype.getFirstPage = function(callback) { var _this = this; return this.query(function(err, response, body) { var data, key, pages, total, value; if (err) { return callback(err); } pages = 0; try { data = JSON.parse(body); } catch (error) { return callback(new Error("(Tender) " + body)); } total = data.total != null ? Math.min(_this.options.max, data.total) : _this.options.max; if ((data.per_page != null) && total > data.per_page) { pages = Math.ceil((total - data.per_page) / data.per_page); } for (key in data) { value = data[key]; if (Array.isArray(value)) { _this.arrayKey = key; } } return callback(null, pages, data); }); }; TenderRequest.prototype.getAdditionalPages = function(pages, data, callback) { var queue, _results, _this = this; if (!pages) { return callback(null, data); } queue = async.queue(this.getSinglePage, 5); queue.drain = function() { return callback(null, data); }; _results = []; while (pages) { _results.push(queue.push(pages--, function(error, qData) { return data[_this.arrayKey] = data[_this.arrayKey].concat(qData[_this.arrayKey]); })); } return _results; }; TenderRequest.prototype.getSinglePage = function(page, callback) { this.options.qs.page = page + 1; return this.query(function(err, response, body) { var data; if (err) { return callback(err); } try { data = JSON.parse(body); } catch (error) { return callback(new Error("(Tender) " + body)); } return callback(null, data); }); }; TenderRequest.prototype.query = function(callback) { var options, _this = this; options = { uri: this.options.uri, qs: this.options.qs, encoding: 'utf8', headers: { accept: "application/vnd.tender-v1+json" } }; if (!this.client.token) { options.auth = "" + this.client.username + ":" + this.client.password; } return request(options, function(err, response, body) { if (err) { console.log(err); } return callback(err, response, body); }); }; TenderRequest.prototype.finalize = function(err, result) { var i, index, output, _i, _len, _ref; if (err) { return this.callback(err); } if ((result[this.arrayKey] != null) && !this.options.id) { output = result[this.arrayKey]; } else { output = [result]; } if ((_ref = output[0]) != null ? _ref["href"] : void 0) { for (_i = 0, _len = output.length; _i < _len; _i++) { i = output[_i]; index = i.href.lastIndexOf('/'); i.id = i.href.substring(index + 1); } } output = this.filter(output); return this.callback(err, output); }; TenderRequest.prototype.filter = function(data) { var i; if (!data.length) { return data; } if (this.options.name || this.options.pattern) { data = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = data.length; _i < _len; _i++) { i = data[_i]; if (this.matchName(i)) { _results.push(i); } } return _results; }).call(this); } if (this.options.id && data[0].id) { data = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = data.length; _i < _len; _i++) { i = data[_i]; if (i.id === this.options.id) { _results.push(i); } } return _results; }).call(this); } if (data.length > this.options.max) { data = data.slice(0, this.options.max); } return data; }; TenderRequest.prototype.matchName = function(item) { var matches, name, re; name = item.name || item.title; if (!name) { return false; } if (this.options.pattern) { re = new RegExp("" + this.options.pattern, "gi"); matches = name.match(re); if (matches) { return true; } } else { if (name.toLowerCase() === this.options.name.toLowerCase()) { return true; } } return false; }; return TenderRequest; })(); tenderRequest = function(client, options, callback) { var tr; return tr = new TenderRequest(client, options, callback); }; module.exports = tenderRequest;