All files / src Lyft.js

92.86% Statements 26/28
100% Branches 1/1
60% Functions 3/5
92.86% Lines 26/28
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    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                                             4x                                                
'use es6';
 
import rp from 'request-promise';
 
import Subpath from './data/Subpath';
 
import CoordinateBuilder from './data/builders/CoordinateBuilder';
 
import RideTypesSearchBuilder from './search/builders/RideTypesSearchBuilder';
import DriverEtaSearchBuilder from './search/builders/DriverEtaSearchBuilder';
import RideEstimatesSearchBuilder from './search/builders/RideEstimatesSearchBuilder';
import NearbyDriversSearchBuilder from './search/builders/NearbyDriversSearchBuilder';
 
export default class Lyft {
  constructor(clientId, clientSecret) {
    this.clientId = clientId;
    this.clientSecret = clientSecret;
 
    this.tokenType;
    this.accessToken;
    this.expiresIn;
    this.scope;
  }
 
  getAccessToken() {
    return rp({
      method: 'POST',
      uri: 'https://api.lyft.com/oauth/token',
      auth: {
        username: this.clientId,
        password: this.clientSecret,
      },
      headers: {
        'Content-Type': 'application/json',
      },
      body: {
        grant_type: 'client_credentials',
      },
      json: true,
      timeout: 5000,
    })
      .then(result => {
        this.tokenType = result.token_type;
        this.accessToken = result.access_token;
        this.expiresIn = result.expires_in;
        this.scope = result.scope;
        return result;
      })
      .catch(function(err) {
        throw new Error(err);
      });
  }
 
  getRideTypes(search) {
    return this.execute(
      Subpath.RIDE_TYPES,
      RideTypesSearchBuilder.build(search).toParameters().toJS()
    );
  }
 
  getDriverEta(search) {
    return this.execute(
      Subpath.DRIVER_ETA,
      DriverEtaSearchBuilder.build(search).toParameters().toJS()
    );
  }
 
  getRideEstimates(search) {
    return this.execute(
      Subpath.RIDE_ESTIMATES,
      RideEstimatesSearchBuilder.build(search).toParameters().toJS()
    );
  }
 
  getNearbyDrivers(search) {
    return this.execute(
      Subpath.NEARBY_DRIVERS,
      NearbyDriversSearchBuilder.build(search).toParameters().toJS()
    );
  }
 
  // getPriceEstimates(search) {
  //   return this.execute(
  //     Subpath.PRICE_ESTIMATES,
  //     PriceEstimatesSearchBuilder.build(search).toParameters().toJS()
  //   );
  // }
 
  // getTimeEstimates(search) {
  //   return this.execute(Subpath.TIME_ESTIMATES,
  //                       TimeEstimatesSearchBuilder.build(search)
  //                                                 .toParameters()
  //                                                 .toJS());
  // }
 
  async execute(subpath, parameters) {
    const options = await this.buildOptions(subpath, parameters);
    return rp(options)
      .then(result => result)
      .catch(function(err) {
        throw new Error(err);
      });
  }
 
  async buildOptions(subpath, parameters) {
 
    // Also check for time here in the future
    if (!this.tokenType || !this.accessToken) {
      await this.getAccessToken();
    }
 
    return {
      uri: `https://api.lyft.com/v1/${subpath.value}`,
      qs: parameters,
      headers: {
        Authorization: `${this.tokenType} ${this.accessToken}`,
      },
      json: true,
      timeout: 5000,
    };
  }
}