# All files / lib geocoderfactory.js

83.33% Statements 85/102
69.12% Branches 47/68
100% Functions 4/4
83.33% Lines 85/102

    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
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199

    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    22x
    15x

    7x
    7x

    22x
    8x

    14x
    8x

    6x

    6x

    6x
    2x

    4x
    1x

    3x
    1x

    2x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    4x
    2x

    2x

    2x
    2x

    2x

    22x
    2x
    2x
    2x

    22x
    4x

    22x
    1x

    22x
    2x

    22x
    2x

    22x

    22x
    22x

    22x
    22x

    21x

    21x
    4x

    21x

    1x
     'use strict';

    var Helper = require('./helper.js');
    var Geocoder = require('./geocoder.js');

    var HttpAdapter = require('./httpadapter/httpadapter.js');
    var HttpsAdapter = require('./httpadapter/httpsadapter.js');
    var RequestAdapter = require('./httpadapter/requestadapter.js');

    var GoogleGeocoder = require('./geocoder/googlegeocoder.js');
    var HereGeocoder = require('./geocoder/heregeocoder.js');
    var AGOLGeocoder = require('./geocoder/agolgeocoder.js');
    var FreegeoipGeocoder = require('./geocoder/freegeoipgeocoder.js');
    var DataScienceToolkitGeocoder = require('./geocoder/datasciencetoolkitgeocoder.js');
    var OpenStreetMapGeocoder = require('./geocoder/openstreetmapgeocoder.js');
    var PickPointGeocoder = require('./geocoder/pickpointgeocoder.js');
    var LocationIQGeocoder = require('./geocoder/locationiqgeocoder.js');
    var MapQuestGeocoder = require('./geocoder/mapquestgeocoder.js');
    var MapzenGeocoder = require('./geocoder/mapzengeocoder.js');
    var OpenMapQuestGeocoder = require('./geocoder/openmapquestgeocoder.js');
    var YandexGeocoder = require('./geocoder/yandexgeocoder.js');
    var GeocodioGeocoder = require('./geocoder/geocodiogeocoder.js');
    var OpenCageGeocoder = require('./geocoder/opencagegeocoder.js');
    var NominatimMapquestGeocoder = require('./geocoder/nominatimmapquestgeocoder.js');
    var TomTomGeocoder = require('./geocoder/tomtomgeocoder.js');
    var VirtualEarthGeocoder = require('./geocoder/virtualearth.js');
    var SmartyStreets = require('./geocoder/smartystreetsgeocoder.js');
    var TeleportGeocoder = require('./geocoder/teleportgeocoder.js');
    var OpendataFranceGeocoder = require('./geocoder/opendatafrancegeocoder.js');

    /**
    * Geocoder Facotry
    */
    var GeocoderFactory = {

      /**
      * Return an http adapter by name
      * @param  <string> adapterName adapter name
      * @return <object>
      */
      _getHttpAdapter: function(adapterName, options) {
        if (adapterName === 'http') {
          return new HttpAdapter(null, options);
        }
        Eif (adapterName === 'https') {
          return new HttpsAdapter(null, options);
        }
        if (adapterName === 'request') {
          return new RequestAdapter(null, options);
        }
      },
      /**
      * Return a geocoder adapter by name
      * @param  <string> adapterName adapter name
      * @return <object>
      */
      _getGeocoder: function(geocoderName, adapter, extra) {
        if (geocoderName === 'google') {
          return new GoogleGeocoder(adapter, {clientId: extra.clientId, apiKey: extra.apiKey, language: extra.language, region: extra.region, excludePartialMatches: extra.excludePartialMatches, channel: extra.channel});
        }
        if (geocoderName === 'here') {
          return new HereGeocoder(adapter, {appId: extra.appId, appCode: extra.appCode, language: extra.language, politicalView: extra.politicalView, country: extra.country, state: extra.state});
        }
        Iif (geocoderName === 'agol') {
          return new AGOLGeocoder(adapter, {client_id: extra.client_id, client_secret: extra.client_secret});
        }
        Iif (geocoderName === 'freegeoip') {
          return new FreegeoipGeocoder(adapter);
        }
        if (geocoderName === 'datasciencetoolkit') {
          return new DataScienceToolkitGeocoder(adapter, {host: extra.host});
        }
        if (geocoderName === 'openstreetmap') {
          return new OpenStreetMapGeocoder(adapter, {language: extra.language});
        }
        if (geocoderName === 'pickpoint') {
          return new PickPointGeocoder(adapter, {language: extra.language, apiKey: extra.apiKey});
        }
        if (geocoderName === 'locationiq') {
          return new LocationIQGeocoder(adapter, extra.apiKey);
        }
        Iif (geocoderName === 'mapquest') {
          return new MapQuestGeocoder(adapter, extra.apiKey);
        }
        Iif (geocoderName === 'mapzen') {
          return new MapzenGeocoder(adapter, extra.apiKey);
        }
        Iif (geocoderName === 'openmapquest') {
          return new OpenMapQuestGeocoder(adapter, extra.apiKey);
        }
        Iif (geocoderName === 'yandex') {
          return new YandexGeocoder(adapter, {
            language: extra.language,
            results: extra.results,
            skip:  extra.skip,
            kind:  extra.kind,
            bbox:  extra.bbox,
            rspn:  extra.rspn
          });
        }
        Iif (geocoderName === 'geocodio') {
          return new GeocodioGeocoder(adapter, extra.apiKey);
        }
        Iif (geocoderName === 'opencage') {
          return new OpenCageGeocoder(adapter, extra.apiKey, extra);
        }
        Iif (geocoderName === 'nominatimmapquest') {
          return new NominatimMapquestGeocoder(adapter, {language: extra.language, apiKey: extra.apiKey});
        }
        Iif (geocoderName === 'tomtom') {
          return new TomTomGeocoder(adapter, {apiKey: extra.apiKey});
        }
        Iif (geocoderName === 'virtualearth') {
          return new VirtualEarthGeocoder(adapter, {apiKey: extra.apiKey});
        }
        Iif (geocoderName === 'smartystreets') {
          return new SmartyStreets(adapter, extra.auth_id, extra.auth_token);
        }
        Iif (geocoderName === 'teleport') {
          return new TeleportGeocoder(adapter, extra.apiKey, extra);
        }
        Iif (geocoderName === 'opendatafrance') {
          return new OpendataFranceGeocoder(adapter);
        }

        throw new Error('No geocoder provider find for : ' + geocoderName);
      },
      /**
      * Return an formatter adapter by name
      * @param  <string> adapterName adapter name
      * @return <object>
      */
      _getFormatter: function(formatterName, extra) {
        if (formatterName === 'gpx') {
          var GpxFormatter = require('./formatter/gpxformatter.js');

          return new GpxFormatter();
        }

        Eif (formatterName === 'string') {
          var StringFormatter = require('./formatter/stringformatter.js');

          return new StringFormatter(extra.formatterPattern);
        }
      },
      /**
      * Return a geocoder
      * @param  <string|object> geocoderAdapter Geocoder adapter name or adapter object
      * @param  <string|object> httpAdapter     Http adapter name or adapter object
      * @param  <array>         extra           Extra parameters array
      * @return <object>
      */
      getGeocoder: function(geocoderAdapter, httpAdapter, extra) {
        if (typeof geocoderAdapter === 'object') {
          extra = geocoderAdapter;
          geocoderAdapter = null;
          httpAdapter = null;
        }

        if (!extra) {
          extra = {};
        }

        if (extra.httpAdapter) {
          httpAdapter = extra.httpAdapter;
        }

        if (extra.provider) {
          geocoderAdapter = extra.provider;
        }

        if (!httpAdapter) {
          httpAdapter = 'https';
        }

        Iif (!geocoderAdapter) {
          geocoderAdapter = 'google';
        }

        Eif (Helper.isString(httpAdapter)) {
          httpAdapter = this._getHttpAdapter(httpAdapter, extra);
        }

        Eif (Helper.isString(geocoderAdapter)) {
          geocoderAdapter = this._getGeocoder(geocoderAdapter, httpAdapter, extra);
        }

        var formatter = extra.formatter;

        if (Helper.isString(formatter)) {
          formatter = this._getFormatter(formatter, extra);
        }

        return new Geocoder(geocoderAdapter, formatter);
      }
    };

    module.exports = GeocoderFactory;