All files / src/services/translators GeocodeTranslator.js

97.3% Statements 36/37
95.83% Branches 23/24
100% Functions 1/1
97.22% Lines 35/36
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    1x       1x 1x 1x       15x 1x     14x 1x     13x 1x     12x 12x 1x     11x       20x 1x     19x   19x 1x     18x   18x 1x     17x   17x 1x     16x 1x     15x 15x   15x 1x     14x 2x     12x       12x                  
'use es6';
 
import {
  List,
} from 'immutable';
 
import Coordinate from '../../data/Coordinate';
import Location from '../../data/Location';
import Utilities from '../../Utilities';
 
export default class GeocodeTranslator {
  static translate(json) {
    if (!('status' in json)) {
      throw new ReferenceError('expected status field');
    }
 
    if (json['status'] !== 'OK') {
      throw new TypeError('unexpected status value');
    }
 
    if (!('results' in json)) {
      throw new ReferenceError('expected results field');
    }
 
    let results = json['results'];
    if (!Array.isArray(results)) {
      throw new TypeError('expected array of results');
    }
 
    return List(results.map(result => GeocodeTranslator.translateLocation(result)));
  }
 
  static translateLocation(json) {
    if (!('formatted_address' in json)) {
      throw new ReferenceError('expected formatted address field');
    }
 
    let formattedAddress = json['formatted_address'];
 
    if (!('geometry' in json)) {
      throw new ReferenceError('expected geometry field');
    }
 
    let geometry = json['geometry'];
 
    if (!('location' in geometry)) {
      throw new ReferenceError('expected location field in geometry field');
    }
 
    let location = geometry['location'];
 
    if (!('lat' in location)) {
      throw new ReferenceError('expected lat field in location field');
    }
 
    if (!('lng' in location)) {
      throw new ReferenceError('expected lng field in location field');
    }
 
    let latitude = location['lat'];
    let longitude = location['lng'];
 
    if (typeof formattedAddress !== 'string') {
      throw new TypeError('expected address to be a string');
    }
 
    if (!Utilities.isFloat(latitude)) {
      throw new TypeError('expected latitude to be a float');
    }
 
    Iif (!Utilities.isFloat(longitude)) {
      throw new TypeError('expected longitude to be a float');
    }
 
    return new Location({
      name: formattedAddress,
      coordinate: new Coordinate({
        latitude: latitude,
        longitude: longitude,
      })
    });
  }
}