All files / src/services/translators DriverEtasTranslator.js

92.59% Statements 25/27
83.33% Branches 15/18
100% Functions 1/1
92.59% Lines 25/27
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    1x   1x 1x 1x       5x 1x     4x   4x 1x     3x 8x         13x 1x     12x 1x     11x 1x     10x   10x 1x     9x   9x       9x   9x       9x                    
'use es6';
 
import {List} from 'immutable';
 
import Duration from '../../data/Duration';
import TimeUnit from '../../data/TimeUnit';
import DriverEta from '../../data/DriverEta';
 
export default class DriverEtasTranslator {
  static translate(response) {
    if (!('eta_estimates' in response)) {
      throw new ReferenceError('expected eta_estimates field');
    }
 
    const etaEstimates = response.eta_estimates;
 
    if (!Array.isArray(etaEstimates)) {
      throw new TypeError('expected eta_estimates to be an array');
    }
 
    return List(etaEstimates.map((etaEstimate) => {
      return DriverEtasTranslator.translateEtaEstimate(etaEstimate);
    }));
  }
 
  static translateEtaEstimate(etaEstimate) {
    if (!('display_name' in etaEstimate)) {
      throw new ReferenceError('expected display_name field');
    }
 
    if (!('eta_seconds' in etaEstimate)) {
      throw new ReferenceError('expected eta_seconds field');
    }
 
    if (!('is_valid_estimate' in etaEstimate)) {
      throw new ReferenceError('expected is_valid_estimate field');
    }
 
    const displayName = etaEstimate.display_name;
 
    if (typeof displayName !== 'string') {
      throw new TypeError('expected display_name to be a string');
    }
 
    const etaSeconds = etaEstimate.eta_seconds;
 
    Iif (!Number.isInteger(etaSeconds)) {
      throw new TypeError('expected eta_seconds to be an integer');
    }
 
    const isValidEstimate = etaEstimate.is_valid_estimate;
 
    Iif (!typeof(isValidEstimate) === 'boolean') {
      throw new TypeError('expected is_valid_estimate to be an boolean');
    }
 
    return new DriverEta({
      displayName: displayName,
      etaSeconds: new Duration({
        length: etaSeconds,
        unit: TimeUnit.SECOND,
      }),
      isValidEstimate: isValidEstimate,
    });
  }
}