Skip to content

Latest commit

 

History

History
120 lines (89 loc) · 2.69 KB

README.md

File metadata and controls

120 lines (89 loc) · 2.69 KB

Lyft + Node.js

This is a WIP wrapper written in node for the Lyft API. It's meant to be used alongside your existing application to quickly and easily facilitate calls to the API.

For a sample application using this wrapper, check out our Node Lyft Starter project.

Installation

First, you need to register your app in the Lyft Developer Portal, and take note of the CLIENT_ID and CLIENT_SECRET provided.

Next, copy .env.sample to .env and place in the root of your project, updating it with your information. Alternatively, add environment variables to your startup script. This project uses .env to manage these variables.

./.env

CONFIG_LYFT_API_URI=https://api.lyft.com
CONFIG_LYFT_WWW_URI=https://www.lyft.com
CONFIG_LYFT_CLIENT_ID=YOUR_ID
CONFIG_LYFT_CLIENT_SECRET=YOUR_SECRET
CONFIG_PORT=8080
CONFIG_SESSION_SECRET=secret
CONFIG_USE_SANDBOX=true

After registering your application, install this module in your Node.js project:

$ npm i node-lyft -S

Initialization


In order to use this module, you have to import it in your application first.

var Lyft = require('node-lyft');
lyft = new Lyft();

It's recommended to substantiate without using the var keyword to make your instance available within imported files:

./app.js:

lyft = new Lyft();

var apiLyftController   = require('./controllers/api/lyft');
app.post('/api/lyft/ridetypes', apiLyftController.getRideTypes);

./controllers/api/lyft.js:

const queryObject = (body) => {
  var qs = {};
  Object.keys(body).forEach((key) => {
    qs[key] = body[key]
  });
  return qs;
};

module.exports = {
  getRideTypes(req, res, next) {
    var qs = queryObject(req.body);

    // qs = { lat: 37.774929, lng: -122.419416 };
    lyft.rideTypes.get(qs).then((data) => {
      res.json(data);
      next();
    }).catch((err) => {
      res.json(err);
      next();
    });
  },
};

Usage

This project uses rsvp.js promises to facilitate calls; all calls are Promises. In your controllers, chain onto these promises like so:

./controllers/api/lyft/index.js

module.exports = {
  getRideTypes: function(req, res, next) {
    lyft.rideTypes.get(req.body.lat, req.body.lng)
    .then(function(data) {
      res.json(data);
      next();
    })
    .catch(function(err) {
      res.json(err);
      next();
    });
  }
}

Building

$ gulp build

Tests

$ gulp test

Build, Test, and Watch for changes:

$ gulp