All files / src/services/translators RideEstimatesTranslator.js

87.27% Statements 48/55
78.95% Branches 30/38
100% Functions 1/1
87.27% Lines 48/55
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    1x   1x 1x 1x 1x 1x 1x       5x 1x     4x   4x 1x     3x 8x           18x 1x     17x 1x       16x 1x     15x 1x     14x 1x     13x 1x     12x 1x     11x 1x     10x   10x 1x     9x   9x       9x   9x       9x   9x       9x   9x       9x   9x       9x   9x       9x   9x       9x                                        
'use es6';
 
import {List} from 'immutable';
 
import Distance from '../../data/Distance';
import DistanceUnit from '../../data/DistanceUnit';
import Duration from '../../data/Duration';
import TimeUnit from '../../data/TimeUnit';
import PriceRange from '../../data/PriceRange';
import RideEstimate from '../../data/RideEstimate';
 
export default class RideEstimatesTranslator {
  static translate(response) {
    if (!('cost_estimates' in response)) {
      throw new ReferenceError('expected cost_estimates field');
    }
 
    const costEstimates = response.cost_estimates;
 
    if (!Array.isArray(costEstimates)) {
      throw new TypeError('expected cost_estimates to be an array');
    }
 
    return List(costEstimates.map((costEstimate) => {
      return RideEstimatesTranslator.translateCostEstimate(costEstimate);
    }));
  }
 
  static translateCostEstimate(costEstimate) {
 
    if (!('estimated_duration_seconds' in costEstimate)) {
      throw new ReferenceError('expected estimated_duration_seconds field');
    }
 
    if (!('estimated_distance_miles' in costEstimate)) {
      throw new ReferenceError('expected estimated_distance_miles field');
    }
 
    
    if (!('estimated_cost_cents_max' in costEstimate)) {
      throw new ReferenceError('expected estimated_cost_cents_max field');
    }
 
    if (!('primetime_percentage' in costEstimate)) {
      throw new ReferenceError('expected primetime_percentage field');
    }
 
    if (!('is_valid_estimate' in costEstimate)) {
      throw new ReferenceError('expected is_valid_estimate field');
    }
 
    if (!('currency' in costEstimate)) {
      throw new ReferenceError('expected currency field');
    }
 
    if (!('estimated_cost_cents_min' in costEstimate)) {
      throw new ReferenceError('expected estimated_cost_cents_min field');
    }
 
    if (!('display_name' in costEstimate)) {
      throw new ReferenceError('expected display_name field');
    }
 
    const estimatedDurationSeconds = costEstimate.estimated_duration_seconds;
 
    if (!Number.isInteger(estimatedDurationSeconds)) {
      throw new TypeError('expected estimated_duration_seconds to be an integer');
    }
 
    const estimatedDistanceMiles = costEstimate.estimated_distance_miles;
 
    Iif (typeof(estimatedDistanceMiles) !== 'number') {
      throw new TypeError('expected estimated_distance_miles to be a number');
    }
 
    const estimatedCostCentsMax = costEstimate.estimated_cost_cents_max;
 
    Iif (!Number.isInteger(estimatedCostCentsMax)) {
      throw new TypeError('expected estimated_cost_cents_max to be an integer');
    }
 
    const primetimePercentage = costEstimate.primetime_percentage;
 
    Iif (typeof(primetimePercentage) !== 'string') {
      throw new TypeError('expected primetime_percentage to be a string');
    }
 
    const isValidEstimate = costEstimate.is_valid_estimate;
 
    Iif (!typeof(isValidEstimate) === 'boolean') {
      throw new TypeError('expected is_valid_estimate to be an boolean');
    }
 
    const currency = costEstimate.currency;
 
    Iif (typeof(currency) !== 'string') {
      throw new TypeError('expected currency to be a string');
    }
 
    const estimatedCostCentsMin = costEstimate.estimated_cost_cents_min;
 
    Iif (!Number.isInteger(estimatedCostCentsMin)) {
      throw new TypeError('expected estimated_cost_cents_min to be an integer');
    }
 
    const displayName = costEstimate.display_name;
 
    Iif (typeof(displayName) !== 'string') {
      throw new TypeError('expected display_name to be a string');
    }
 
    return new RideEstimate({
      displayName: displayName,
      estimatedDuration: new Duration({
        length: estimatedDurationSeconds,
        unit: TimeUnit.SECOND,
      }),
      estimatedDistance: new Distance({
        value: estimatedDistanceMiles,
        unit: DistanceUnit.MILE,
      }),
      isValidEstimate: isValidEstimate,
      primetimePercentage: parseInt(primetimePercentage),
      priceRange: new PriceRange({
        min: estimatedCostCentsMin,
        max: estimatedCostCentsMax,
        currencyCode: currency,
      }),
    });
  }
}