Price Estimate and requesting rides example with map.
This demo uses the following libraries
- express for routing and serving static files
- request for simpler async https requests to uber api
- body-parser middleware for parsing POST body requests
- oauth for authorizing users with uber api through OAuth2
- OAuth2 module source because the readme docs are too light
- github example take care to modify url configurations to use uber endpoints
All libraries are imported using cdnjs
- leaflet for maps
- OpenStreetMap as a source for map tiles
- backbone for client side models, views, templates and routing
- handlebars for rendering dynamic templates
- jquery for dom selecting, some manipulation, and async http requests
- underscore mainly used for extending classes and objects
- skeleton lightweight responsive css framework
For presenting this project as a live tutorial, here are the steps along with links and source.
First, you must register as a developer with uber, and create an uber app.
It's simpler to have an uber account, and if you don't have one, download the app on your smartphone, create an account, and you do not have to enter in your credit card info at this time. login to the uber developer panel using the uber account you just created.
- create a
package.json
file - install the
express
module, setup yourserver.js
- setup static middleware in
server.js
, add boilerplateindex.html
- add leaflet
- editing
index.html
- include the leaflet js library from cdnjs
- include the leaflet css file from cdnjs
- include your main
js/app.js
file (doesn't exist yet) - include your main
css/styles.css
file (doesn't exist yet) - add a
h1{Hello Uber}
to the body - add a
div#map
container to the body
- create and edit
js/app.js
- do a sanity check
- create an iife
- use strict
- create an
origin
array of lat/lng of some location - create a new
map
object and set it's view toorigin
, with a zoom level of17
- add a TileLayer using OpenStreetMap
- add a new
marker
object andaddTo
yourmap
- create and edit
css/styles.css
- add
#map
height of600px
- without this, your map will not show
- add
- editing
- add destination marker on map click
- use jquery to
GET
price estimates from your api- include jquery library
- create a
getPriceEstimates()
function inapp.js
- this request should fail
- create an
api
Router and mount it on the/api
path using expressserver.js
- mount
api.js
onto/api
- mount
- create and edit
api.js
- setup an express Router
- create a GET
/estimates
route handler - respond with a "hello world" json
- pass
source
anddestination
from jquery to apipublic/js/app.js
- add GET query params by stringifying
source
anddestination
- add GET query params by stringifying
api.js
- console.log
req.query
- console.log
- get price estimates from the uber api
- install the
request
module api.js
creating an http GET request to uber api- api.js
- note changed the path to
/estimates/price
public/js/app.js
update the endpoint url, parse the data, log it
- install the
- add backbone, underscore, handlbars, models, views, templates and render all the price estimate results to browser.
- public/index.html
- public/js/models/price.js create a Price model, and a Prices collection
- public/js/template_manager.js create a template manager for asynchronously loading external handlebars template with caching
- public/js/views/price.js create a
PricesView
to render the Prices collection - public/templates/prices.hbs create a handlebars template to render the
PricesView
- public/js/app.js create an instance of
Prices
collection, a new instance ofPricesView
, addprices
topricesView
, then render it
- add price estimate detail view, add skeleton css
- public/index.html add skeleton css, restructure divs, add container for
#price-detail
- public/js/views/price.js create a new
PriceView
to render an estimate detail, add a click event toPricesView
that will render a new instance ofpriceView
- public/templates/price.hbs create new
price.hbs
template forPriceView
detail public/templates/prices.hbs
add a data attribute to each li,data-id="{{@index}}"
- public/index.html add skeleton css, restructure divs, add container for
- interact with OAuth2 uber endpoints to authorize the user with your app, add popup window to let user authenticate, get the
access_token
from uber, add/api/get_ride
endpoint that will interact with uber's/request
api- install the
oauth
module - install the 'body-parser' module
server.js
addbodyParser.urlencoded
middlewareapi.js
stub out a POST/get_ride
route handler- public/js/app.js extend the price model to add
source
anddestination
attributes - public/js/views/price.js add a
.get-ride
event handler that will post to the/api/get_ride
endpoint and opens up new browser for authenticating with uber - api.js create an instance of
OAuth2
, generate authorization url to send back to the client. create oauth callback endpoint, handle errors, stub out success. gets theaccess_token
- create a
.env
file for secret uber keys - read
.env
and restartnodemon
- install the
- send the access token back to the client, add backbone router to extract the access_token, store it in
localStorage
, use the token for authorized requests- public/js/routes.js create a router
- include the
/js/routes.js
file inpublic/index.html
public/js/app.js
create an instance ofrouter
var router = new Router();
Backbone.history.start();
- api.js when receiving the access_token tell the popup window to give the token to the main window, and close
- authorized requests post to uber
/requests
to call an uber driver. add status view, model, and template for displaying statusapp.js
use the correct uber pathrequests
app.js
sendsuccess = true
attribute with the response body- public/index.html add
#ride-status
container, and includejs/models/status.js
andjs/views/status.js
- create a bare
public/models/status.js
- create a public/views/status.js view
- create a public/templates/status.hbs template
- public/js/views/price.js on successful
/api/get-ride
request, create a new instance ofstatusView