/**
 * Module dependencies
 */
var request = require('superagent')
  , _ = require('underscore');

// Factory
module.exports = function (opts) {
  return new Affilinet(opts);
};

// Api endpoint
var endpoint = 'http://product-api.affili.net/V3/productservice.svc/JSON/SearchProducts';

var Affilinet = function Affilinet (opts) {
  opts = ('string' === typeof opts) ? { keywords: opts } : opts;

  if (opts.keywords) {
    this._keywords = opts.keywords;
    this.mode = 'search';
  }
};

// Publisher identifier
Affilinet.prototype.id = function (id) {
  return this._id = id, this;
};

// Affilinet api password, or key I guess
Affilinet.prototype.password = Affilinet.prototype.key = function (password) {
  return this._password = password, this;
};

Affilinet.prototype.done = function (cb) {
  request
    .get(endpoint)
    .query({publisherId: this._id})
    .query({Password: this._password})
    .done(function (err, result) {
      if (err) return cb(err);
      return cb(null, result.body);
    });
};

@@ -1004,12 +1004,389 @@
 %0A    %7D);%0A%7D;%0A
+%0A// Take care of affilinet byte ordering foolishness%0Arequest.parse%5B'application/json'%5D = function (res, fn) %7B%0A  res.text = '';%0A  res.setEncoding('utf8');%0A  res.on('data', function (chunk) %7B res.text += chunk; %7D);%0A  res.on('end', function () %7B%0A    try %7B%0A      res.text = res.text.slice(1);%0A      fn(null, JSON.parse(res.text));%0A    %7D catch (err) %7B%0A      fn(err);%0A    %7D%0A  %7D);%0A%7D;%0A

Remove byte order indicator before attempting parse