All files / lib/geocoder smartystreetsgeocoder.js

65.52% Statements 19/29
50% Branches 5/10
50% Functions 3/6
65.52% Lines 19/29
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 81 82 83 84 85 86 87 88 89 90 912x 2x             2x 5x   4x 1x     3x 3x     2x   2x               2x 1x 1x                 2x                                             2x 1x   1x             1x                               2x  
var util             = require('util'),
    AbstractGeocoder = require('./abstractgeocoder');
 
/**
 * SmartyStreets Constructor
 * @param <object> httpAdapter Http Adapter
 * @param <object> options     Options
 */
var SmartyStreets = function SmartyStreets(httpAdapter, auth_id, auth_token) {
    SmartyStreets.super_.call(this, httpAdapter);
 
    if(!auth_id && !auth_token){
      throw new Error('You must specify an auth-id and auth-token!');
    }
 
    this.auth_id = auth_id;
    this.auth_token = auth_token;
};
 
util.inherits(SmartyStreets, AbstractGeocoder);
 
SmartyStreets.prototype._endpoint = 'https://api.smartystreets.com/street-address';
 
/**
* Reverse geocoding
* @param <integer>  lat      Latittude
* @param <integer>  lng      Longitude
* @param <function> callback Callback method
*/
SmartyStreets.prototype.reverse = function(lat, lng, callback) {
    Eif (typeof this._reverse != 'function') {
        throw new Error(this.constructor.name + ' doesnt support reverse geocoding!');
    }
 
    return this._reverse(lat, lng, callback);
};
 
/**
 * Format Result
 **/
SmartyStreets.prototype._formatResult = function(result) {
  if(result){
      return [{
        'latitude' : result.metadata.latitude,
        'longitude' : result.metadata.longitude,
        'country' : null,
        'city' : result.components.city_name,
        'zipcode' : result.components.zipcode,
        'streetName' : result.components.street_name + ' ' + result.components.street_suffix,
        'streetNumber' : result.components.primary_number,
        'countryCode' : null,
        'type' : result.metadata.record_type,
        'dpv_match' : result.analysis.dpv_match_code,
        'dpv_footnotes' : result.analysis.dpv_footnotes
      }];
  }
};
 
/**
* Geocode
* @param <string>   value    Value to geocode
* @param <function> callback Callback method
*/
SmartyStreets.prototype.geocode = function(value, callback) {
    var _this = this;
 
    var params = {
      'street': value,
      'auth-id': this.auth_id,
      'auth-token': this.auth_token,
      'format': 'json'
    };
 
    this.httpAdapter.get(this._endpoint,params,function(err, result){
      if(err) {
        return callback(err);
      } else {
        var results = [];
 
        result.forEach(function(result) {
          results.push(_this._formatResult(result));
        });
 
        results.raw = result;
        callback(false, results);
      }
    });
};
 
module.exports = SmartyStreets;