All files / lib/geocoder abstractgeocoder.js

93.75% Statements 30/32
91.43% Branches 32/35
100% Functions 5/5
93.75% Lines 30/32
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 71 72 73 74 75 76 77 78 79 80    18x 18x     182x               18x 182x       182x   182x 17x   165x   165x 105x     165x 112x 488x 388x         165x               18x 27x 2x     25x               18x 82x     82x 13x     69x 13x     56x 1x     55x     18x  
'use strict';
 
var net = require('net');
var ValueError = require('../error/valueerror.js');
 
function formatGeocoderName(name) {
  return name.toLowerCase().replace(/geocoder$/, '');
}
 
/**
 * AbstractGeocoder Constructor
 * @param <object> httpAdapter Http Adapter
 * @param <object> options     Options
 */
var AbstractGeocoder = function(httpAdapter, options) {
  Iif (!this.constructor.name) {
    throw new Error('The Constructor must be named');
  }
 
  this.name = formatGeocoderName(this.constructor.name);
 
  if (!httpAdapter || httpAdapter == 'undefined') {
    throw new Error(this.constructor.name + ' need an httpAdapter');
  }
  this.httpAdapter = httpAdapter;
 
  if (!options || options == 'undefined') {
    options = {};
  }
 
  if (this.options) {
    this.options.forEach(function(option) {
      if (!options[option] || options[option] == 'undefined') {
        options[option] = null;
      }
    });
  }
 
  this.options = options;
};
 
/**
* Reverse geocoding
* @param {lat:<number>,lon:<number>}  lat: Latitude, lon: Longitude
* @param <function> callback Callback method
*/
AbstractGeocoder.prototype.reverse = function(query, callback) {
  if (typeof this._reverse != 'function') {
    throw new Error(this.constructor.name + ' no support reverse geocoding');
  }
 
  return this._reverse(query, callback);
};
 
/**
* Geocode
* @param <string>   value    Value to geocode
* @param <function> callback Callback method
*/
AbstractGeocoder.prototype.geocode = function(value, callback) {
  Iif (typeof this._geocode != 'function') {
    throw new ValueError(this.constructor.name + ' does not support geocoding');
  }
  if (net.isIPv4(value) && (!this.supportIPv4 || this.supportIPv4 == 'undefined')) {
    throw new ValueError(this.constructor.name + ' does not support geocoding IPv4');
  }
 
  if (net.isIPv6(value) && (!this.supportIPv6 || this.supportIPv6 == 'undefined')) {
    throw new ValueError(this.constructor.name + ' does not support geocoding IPv6');
  }
 
  if (this.supportAddress === false && (!net.isIPv4(value) && !net.isIPv6(value))) {
    throw new ValueError(this.constructor.name + ' does not support geocoding address');
  }
 
  return this._geocode(value, callback);
};
 
module.exports = AbstractGeocoder;