Skip to content
This repository has been archived by the owner on Jan 2, 2018. It is now read-only.

Commit

Permalink
Moved all calls requiring an API key to server side
Browse files Browse the repository at this point in the history
  • Loading branch information
shuwen committed Apr 19, 2015
1 parent d9f623f commit 9053c95
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
53 changes: 14 additions & 39 deletions public/javascripts/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,33 @@
success: function(loc) {
// Write geolocation location to view
$('#location').html(loc.coords.latitude + 'N, ' + loc.coords.longitude + 'E');

// Populate list of buses
$.ajax({
'method': 'GET',
'dataType': 'json',
'headers': {
'X-Mashape-Key': '***REMOVED***',
},
'url': 'https://transloc-api-1-2.p.mashape.com/stops.json?agencies=100&geo_area='+(loc.coords.latitude + 0.003)+','+(loc.coords.longitude - 0.003)+'|'+(loc.coords.latitude - 0.003)+','+(loc.coords.longitude + 0.003),

'success': function(res) {
// Add each of the buses we get as an option in our select
$.each(res.data, function(index,value) {
$('#buses').append('<option value="'+value.stop_id+'">'+value.name+'</option>');
});
$('#submit').prop('disabled',false);
} // end of ajax success function
}); // end of ajax call
}, // success
socket.emit('locate', {lat: loc.coords.latitude, lon: loc.coords.longitude});
},
error: function(e) {
console.log(e);
}
});
});

// Submit bus info
$('#submit').click(function() {
socket.emit('watch', {
bus: $('#buses').val(),
t: parseInt($('#time').val())
t: parseInt($('#time').val()),
user: $('#handle').val(),
});
});

// Get the server to emit a test event back to us
$('#test').click(function() {
var user = $('#handle').val();
yo(user);
});

// Server will respond when there is a bus
socket.on('bus', function() {
flashTitle();
var user = $('#handle').val();
yo(user);
});

socket.on('busList', function(d) {
$.each(d.buses, function(index,value) {
$('#buses').append('<option value="'+value.stop_id+'">'+value.name+'</option>');
});
$('#submit').prop('disabled',false);
});

// Flash the page title
Expand All @@ -64,15 +50,4 @@
title.text(oldTitle);
}, 900);
}

function yo(user) {
$.ajax({
type: 'POST',
url: 'http://api.justyo.co/yo/',
data: {
'api_token': '***REMOVED***',
'username': user
}
});
}
})();
40 changes: 32 additions & 8 deletions sockets/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,46 @@ var unirest = require('unirest'),
module.exports = function(io) {
io.on('connection', function(socket) {

// For testing only
socket.on('test', function() {
console.log('test: it works');
socket.emit('bus', {});
socket.on('locate', function(d) {
busList(d.lat,d.lon,socket);
});

socket.on('watch', function(d) {
var f = false;
// Pass the bus id, time, and socket
findBus(d.bus, d.t, socket);
findBus(d.bus,d.t,d.user,socket);
});

});
}

// Helper function that "Yo"s the specific user
function yo(user) {
var request = unirest.post("http://api.justyo.co/yo/")
.send({
'api_token': '***REMOVED***',
'username': user
})
.end(function(res) {
console.log(res.body);
});
}

/**
* Helper function that takes in a lat/long position and
* returns a list of bus stops within that cirlce
**/
function busList(lat,lon,soc) {
var reqPath = 'https://transloc-api-1-2.p.mashape.com/stops.json?agencies=100&geo_area='+(lat + 0.003)+','+(lon - 0.003)+'|'+(lat - 0.003)+','+(lon + 0.003);

var request = unirest.get(reqPath)
.header("X-Mashape-Key", "***REMOVED***")
.header("Accept", "application/json")
.end(function (res) {
soc.emit('busList', {buses: res.body.data});
});
}

// Helper function that queries API until true
function findBus(id, t, soc) {
function findBus(id,t,user,soc) {
var busFound = false;
var reqPath = "https://transloc-api-1-2.p.mashape.com/arrival-estimates.json?agencies=100&stops="+id;

Expand All @@ -30,6 +53,7 @@ function findBus(id, t, soc) {
.header("Accept", "application/json")
.end(function (res) {
if ( (Date.parse(res.body.data[0].arrivals[0].arrival_at) - Date.now() ) < t*60000 ) {
yo(user);
soc.emit('bus', {});
} else {
// Wait 1 minute and run this function again
Expand Down

0 comments on commit 9053c95

Please sign in to comment.