Code coverage report for lib/sapi.js

Statements: 100% (21 / 21)      Branches: 100% (6 / 6)      Functions: 100% (6 / 6)      Lines: 100% (21 / 21)      Ignored: none     

All files » lib/ » sapi.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 711                             1 5   1     2 1   1       5 5 5             1   4   4               26 2 2                 4   3 3   3       1  
var _ = require('lodash'),
  bag = require('bagofrequest'),
  search = require('./endpoint/search'),
  getByListingId = require('./endpoint/getbylistingid'),
  report = require('./endpoint/report'),
  metadata = require('./endpoint/metadata'),
  endpoints = [search, getByListingId, report, metadata];
 
/**
 * class Sapi
 *
 * @param {String} key: Sensis API key, apply for the key here http://developers.sensis.com.au/docs/getting_started/Apply_for_an_API_key
 * @param {Object} opts: optional
 * - url: optional Sensis API base URL, defaults to http://developers.sensis.com.au/docs/using_endpoints
 */
function Sapi(key, opts) {
  opts = opts || {};
 
  function _authRequire(result, cb) {
    // SAPI does not differentiate required authentication and failed authentication
    // since both return status code 403, need to check key param existence to differentiate the two
    if (key) {
      cb(new Error('Authentication failed -  invalid key ' + key));
    } else {
      cb(new Error('Authentication required - set API key in Sapi constructor'));
    }
  }
 
  this.params = { key: key };
  this.url = (opts.url || 'http://api.sensis.com.au/ob-20110511/test').replace(/\/$/, '');
  this.opts = {
    handlers: {
      403: _authRequire
    }
  };
}
 
endpoints.forEach(function (endpoint) {
 
  var params = endpoint.params.required.concat(endpoint.params.optional);
 
  params.forEach(function (param) {
    /**
     * Chainable function for each parameter.
     * Usage pattern: sapi.param1().param2().param3().end();
     * The idea here is to allow the construction of 0...* parameters before calling the endpoint.
     *
     * @param {String} value: parameter value
     */
    Sapi.prototype[param] = function (value) {
      this.params[param] = value;
      return this;
    };
  });
 
  /**
   * End function for each endpoint that makes http request to SAPI.
   *
   * @param {Function} cb: standard cb(err, result) callback
   */
  Sapi.prototype[endpoint.name] = function (cb) {
 
    this.opts.queryStrings = this.params;
    this.opts.handlers = _.extend(this.opts.handlers, endpoint.handlers);
 
    bag.request('get', this.url + '/' + endpoint.path(this.params), this.opts, cb);
  };
});
 
module.exports = Sapi;