All files / src/services LyftService.js

80% Statements 40/50
100% Branches 0/0
52.38% Functions 11/21
80% Lines 40/50
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    1x     1x 1x   1x   1x 1x   1x 1x   1x 1x   1x 1x   1x     1x 1x       1x     2x 2x   2x             2x   2x                           2x 2x   2x             2x   2x                           2x 2x   2x           2x   2x           2x   2x                     2x   2x                             2x 2x   2x             2x   2x                            
'use es6';
 
import {
  List,
} from 'immutable';
import Lyft from 'lyft-node';
import dotenv from 'dotenv';
 
import GeocodeService from './GeocodeService';
 
import RideTypes from '../data/RideTypes';
import RideTypesTranslator from './translators/RideTypesTranslator';
 
import NearbyDrivers from '../data/NearbyDrivers';
import NearbyDriversTranslator from './translators/NearbyDriversTranslator';
 
import DriverEtas from '../data/DriverEtas';
import DriverEtasTranslator from './translators/DriverEtasTranslator';
 
import RideEstimates from '../data/RideEstimates';
import RideEstimatesTranslator from './translators/RideEstimatesTranslator';
 
dotenv.load();
 
export default class LyftService {
  constructor() {
    this.lyftApi = new Lyft(
      process.env.LYFT_CLIENT_ID,
      process.env.LYFT_CLIENT_SECRET
    );
    this.geocodeService = new GeocodeService();
  }
 
  getRideTypes(address) {
    return this.geocodeService.getLocation(address)
      .then((location) => {
        const query = {
          start: {
            latitude: location.coordinate.latitude,
            longitude: location.coordinate.longitude,
          },
        };
 
        return this.lyftApi.getRideTypes(query)
          .then((response) => {
            return new RideTypes({
              location: location,
              rideTypes: RideTypesTranslator.translate(response),
            });
          })
          .catch((error) => {
            throw error;
          });
      })
      .catch((error) => {
        throw error;
      });
  }
 
  getDriverEtas(address) {
    return this.geocodeService.getLocation(address)
      .then((location) => {
        const query = {
          start: {
            latitude: location.coordinate.latitude,
            longitude: location.coordinate.longitude,
          },
        };
 
        return this.lyftApi.getDriverEta(query)
          .then((response) => {
            return new DriverEtas({
              location: location,
              driverEtas: DriverEtasTranslator.translate(response),
            });
          })
          .catch((error) => {
            throw error;
          });
      })
      .catch((error) => {
        throw error;
      });
  }
 
  getRideEstimates(addresses) {
    let startLocation = this.geocodeService.getLocation(addresses.startAddress)
      .then((location) => {
        return location;
      })
      .catch((error) => {
        throw error;
      });
 
    let endLocation = this.geocodeService.getLocation(addresses.endAddress)
      .then((location) => {
        return location;
      })
      .catch((error) => {
        throw error;
      });
 
    return Promise.all([startLocation, endLocation])
      .then((locations) => {
        const query = {
          start: {
            latitude: locations[0].coordinate.latitude,
            longitude: locations[0].coordinate.longitude,
          },
          end: {
            latitude: locations[1].coordinate.latitude,
            longitude: locations[1].coordinate.longitude,
          },
        };
 
        return this.lyftApi.getRideEstimates(query)
          .then((response) => {
            return new RideEstimates({
              start: locations[0],
              end: locations[1],
              costEstimates: RideEstimatesTranslator.translate(response),
            });
          })
          .catch((error) => {
            throw error;
          });
      })
      .catch((error) => {
        throw error;
      });
  }
 
  getNearbyDrivers(address) {
    return this.geocodeService.getLocation(address)
      .then((location) => {
        const query = {
          start: {
            latitude: location.coordinate.latitude,
            longitude: location.coordinate.longitude,
          },
        };
 
        return this.lyftApi.getNearbyDrivers(query)
          .then((response) => {
            return new NearbyDrivers({
              location: location,
              nearbyDrivers: NearbyDriversTranslator.translate(response, location),
            });
          })
          .catch((error) => {
            throw error;
          });
      })
      .catch((error) => {
        throw error;
      });
  }
}