Skip to content

Commit cdbb9d1

Browse files
committed
API code cleanup
1 parent 01e7bd5 commit cdbb9d1

File tree

1 file changed

+23
-45
lines changed

1 file changed

+23
-45
lines changed

controllers/api.js

+23-45
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ exports.getApi = function(req, res) {
3737

3838
exports.getFoursquare = function(req, res, next) {
3939
var token = _.find(req.user.tokens, { kind: 'foursquare' });
40-
console.log(token);
4140
async.parallel({
4241
trendingVenues: function(callback) {
4342
foursquare.Venues.getTrending('40.7222756', '-74.0022724', { limit: 50 }, token.accessToken, function(err, results) {
@@ -71,7 +70,7 @@ exports.getFoursquare = function(req, res, next) {
7170
* Tumblr API example.
7271
*/
7372

74-
exports.getTumblr = function(req, res) {
73+
exports.getTumblr = function(req, res, next) {
7574
var token = _.find(req.user.tokens, { kind: 'tumblr' });
7675
var client = tumblr.createClient({
7776
consumer_key: secrets.tumblr.consumerKey,
@@ -80,6 +79,7 @@ exports.getTumblr = function(req, res) {
8079
token_secret: token.tokenSecret
8180
});
8281
client.posts('withinthisnightmare.tumblr.com', { type: 'photo' }, function(err, data) {
82+
if (err) return next(err);
8383
res.render('api/tumblr', {
8484
title: 'Tumblr API',
8585
blog: data.blog,
@@ -128,7 +128,7 @@ exports.getScraping = function(req, res, next) {
128128
if (err) return next(err);
129129
var $ = cheerio.load(body);
130130
var links = [];
131-
$(".title a[href^='http'], a[href^='https']").each(function() {
131+
$('.title a[href^="http"], a[href^="https"]').each(function() {
132132
links.push($(this));
133133
});
134134
res.render('api/scraping', {
@@ -142,11 +142,13 @@ exports.getScraping = function(req, res, next) {
142142
* GET /api/github
143143
* GitHub API Example.
144144
*/
145-
exports.getGithub = function(req, res) {
145+
146+
exports.getGithub = function(req, res, next) {
146147
var token = _.find(req.user.tokens, { kind: 'github' });
147148
var github = new Github({ token: token.accessToken });
148149
var repo = github.getRepo('sahat', 'requirejs-library');
149150
repo.show(function(err, repo) {
151+
if (err) return next(err);
150152
res.render('api/github', {
151153
title: 'GitHub API',
152154
repo: repo
@@ -174,7 +176,8 @@ exports.getAviary = function(req, res) {
174176
exports.getNewYorkTimes = function(req, res, next) {
175177
var query = querystring.stringify({ 'api-key': secrets.nyt.key, 'list-name': 'young-adult' });
176178
var url = 'http://api.nytimes.com/svc/books/v2/lists?' + query;
177-
request.get(url, function(error, request, body) {
179+
request.get(url, function(err, request, body) {
180+
if (err) return next(err);
178181
if (request.statusCode === 403) return next(Error('Missing or Invalid New York Times API Key'));
179182
var bestsellers = JSON.parse(body);
180183
res.render('api/nyt', {
@@ -194,7 +197,7 @@ exports.getLastfm = function(req, res, next) {
194197
async.parallel({
195198
artistInfo: function(done) {
196199
lastfm.request('artist.getInfo', {
197-
artist: 'Sirenia',
200+
artist: 'The Pierces',
198201
handlers: {
199202
success: function(data) {
200203
done(null, data);
@@ -207,7 +210,7 @@ exports.getLastfm = function(req, res, next) {
207210
},
208211
artistTopTracks: function(done) {
209212
lastfm.request('artist.getTopTracks', {
210-
artist: 'Sirenia',
213+
artist: 'The Pierces',
211214
handlers: {
212215
success: function(data) {
213216
var tracks = [];
@@ -224,7 +227,7 @@ exports.getLastfm = function(req, res, next) {
224227
},
225228
artistTopAlbums: function(done) {
226229
lastfm.request('artist.getTopAlbums', {
227-
artist: 'Sirenia',
230+
artist: 'The Pierces',
228231
handlers: {
229232
success: function(data) {
230233
var albums = [];
@@ -283,19 +286,16 @@ exports.getTwitter = function(req, res, next) {
283286

284287
/**
285288
* POST /api/twitter
286-
* @param tweet
289+
* Post a tweet.
287290
*/
288291

289292
exports.postTwitter = function(req, res, next) {
290293
req.assert('tweet', 'Tweet cannot be empty.').notEmpty();
291-
292294
var errors = req.validationErrors();
293-
294295
if (errors) {
295296
req.flash('errors', errors);
296297
return res.redirect('/api/twitter');
297298
}
298-
299299
var token = _.find(req.user.tokens, { kind: 'twitter' });
300300
var T = new Twit({
301301
consumer_key: secrets.twitter.consumerKey,
@@ -304,6 +304,7 @@ exports.postTwitter = function(req, res, next) {
304304
access_token_secret: token.tokenSecret
305305
});
306306
T.post('statuses/update', { status: req.body.tweet }, function(err, data, response) {
307+
if (err) return next(err);
307308
req.flash('success', { msg: 'Tweet has been posted.'});
308309
res.redirect('/api/twitter');
309310
});
@@ -317,7 +318,6 @@ exports.postTwitter = function(req, res, next) {
317318
exports.getSteam = function(req, res, next) {
318319
var steamId = '76561197982488301';
319320
var query = { l: 'english', steamid: steamId, key: secrets.steam.apiKey };
320-
321321
async.parallel({
322322
playerAchievements: function(done) {
323323
query.appid = '49520';
@@ -330,18 +330,18 @@ exports.getSteam = function(req, res, next) {
330330
playerSummaries: function(done) {
331331
query.steamids = steamId;
332332
var qs = querystring.stringify(query);
333-
request.get({ url: 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?' + qs, json: true }, function(error, request, body) {
333+
request.get({ url: 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?' + qs, json: true }, function(err, request, body) {
334334
if (request.statusCode === 401) return done(new Error('Missing or Invalid Steam API Key'));
335-
done(error, body);
335+
done(err, body);
336336
});
337337
},
338338
ownedGames: function(done) {
339339
query.include_appinfo = 1;
340340
query.include_played_free_games = 1;
341341
var qs = querystring.stringify(query);
342-
request.get({ url: 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?' + qs, json: true }, function(error, request, body) {
342+
request.get({ url: 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?' + qs, json: true }, function(err, request, body) {
343343
if (request.statusCode === 401) return done(new Error('Missing or Invalid Steam API Key'));
344-
done(error, body);
344+
done(err, body);
345345
});
346346
}
347347
},
@@ -370,25 +370,23 @@ exports.getStripe = function(req, res) {
370370

371371
/**
372372
* POST /api/stripe
373-
* @param stipeToken
374-
* @param stripeEmail
373+
* Make a payment.
375374
*/
376375

377376
exports.postStripe = function(req, res, next) {
378377
var stripeToken = req.body.stripeToken;
379378
var stripeEmail = req.body.stripeEmail;
380-
381379
stripe.charges.create({
382380
amount: 395,
383381
currency: 'usd',
384382
card: stripeToken,
385383
description: stripeEmail
386384
}, function(err, charge) {
387385
if (err && err.type === 'StripeCardError') {
388-
req.flash('errors', { msg: 'Your card has been declined.'});
386+
req.flash('errors', { msg: 'Your card has been declined.' });
389387
res.redirect('/api/stripe');
390388
}
391-
req.flash('success', { msg: 'Your card has been charged successfully.'});
389+
req.flash('success', { msg: 'Your card has been charged successfully.' });
392390
res.redirect('/api/stripe');
393391
});
394392
};
@@ -406,28 +404,22 @@ exports.getTwilio = function(req, res) {
406404

407405
/**
408406
* POST /api/twilio
409-
* Twilio API example.
410-
* @param number
411-
* @param message
407+
* Send a text message using Twilio.
412408
*/
413409

414410
exports.postTwilio = function(req, res, next) {
415411
req.assert('number', 'Phone number is required.').notEmpty();
416412
req.assert('message', 'Message cannot be blank.').notEmpty();
417-
418413
var errors = req.validationErrors();
419-
420414
if (errors) {
421415
req.flash('errors', errors);
422416
return res.redirect('/api/twilio');
423417
}
424-
425418
var message = {
426419
to: req.body.number,
427420
from: '+13472235148',
428421
body: req.body.message
429422
};
430-
431423
twilio.sendMessage(message, function(err, responseData) {
432424
if (err) return next(err.message);
433425
req.flash('success', { msg: 'Text sent to ' + responseData.to + '.'});
@@ -448,8 +440,7 @@ exports.getClockwork = function(req, res) {
448440

449441
/**
450442
* POST /api/clockwork
451-
* Clockwork SMS API example.
452-
* @param telephone
443+
* Send a text message using Clockwork SMS
453444
*/
454445

455446
exports.postClockwork = function(req, res, next) {
@@ -473,7 +464,6 @@ exports.postClockwork = function(req, res, next) {
473464
exports.getVenmo = function(req, res, next) {
474465
var token = _.find(req.user.tokens, { kind: 'venmo' });
475466
var query = querystring.stringify({ access_token: token.accessToken });
476-
477467
async.parallel({
478468
getProfile: function(done) {
479469
request.get({ url: 'https://api.venmo.com/v1/me?' + query, json: true }, function(err, request, body) {
@@ -483,7 +473,6 @@ exports.getVenmo = function(req, res, next) {
483473
getRecentPayments: function(done) {
484474
request.get({ url: 'https://api.venmo.com/v1/payments?' + query, json: true }, function(err, request, body) {
485475
done(err, body);
486-
487476
});
488477
}
489478
},
@@ -499,32 +488,24 @@ exports.getVenmo = function(req, res, next) {
499488

500489
/**
501490
* POST /api/venmo
502-
* @param user
503-
* @param note
504-
* @param amount
505491
* Send money.
506492
*/
507493

508494
exports.postVenmo = function(req, res, next) {
509495
req.assert('user', 'Phone, Email or Venmo User ID cannot be blank').notEmpty();
510496
req.assert('note', 'Please enter a message to accompany the payment').notEmpty();
511497
req.assert('amount', 'The amount you want to pay cannot be blank').notEmpty();
512-
513498
var errors = req.validationErrors();
514-
515499
if (errors) {
516500
req.flash('errors', errors);
517501
return res.redirect('/api/venmo');
518502
}
519-
520503
var token = _.find(req.user.tokens, { kind: 'venmo' });
521-
522504
var formData = {
523505
access_token: token.accessToken,
524506
note: req.body.note,
525507
amount: req.body.amount
526508
};
527-
528509
if (validator.isEmail(req.body.user)) {
529510
formData.email = req.body.user;
530511
} else if (validator.isNumeric(req.body.user) &&
@@ -533,7 +514,6 @@ exports.postVenmo = function(req, res, next) {
533514
} else {
534515
formData.user_id = req.body.user;
535516
}
536-
537517
request.post('https://api.venmo.com/v1/payments', { form: formData }, function(err, request, body) {
538518
if (err) return next(err);
539519
if (request.statusCode !== 200) {
@@ -553,7 +533,6 @@ exports.postVenmo = function(req, res, next) {
553533
exports.getLinkedin = function(req, res, next) {
554534
var token = _.find(req.user.tokens, { kind: 'linkedin' });
555535
var linkedin = Linkedin.init(token.accessToken);
556-
557536
linkedin.people.me(function(err, $in) {
558537
if (err) return next(err);
559538
res.render('api/linkedin', {
@@ -570,10 +549,8 @@ exports.getLinkedin = function(req, res, next) {
570549

571550
exports.getInstagram = function(req, res, next) {
572551
var token = _.find(req.user.tokens, { kind: 'instagram' });
573-
574552
ig.use({ client_id: secrets.instagram.clientID, client_secret: secrets.instagram.clientSecret });
575553
ig.use({ access_token: token.accessToken });
576-
577554
async.parallel({
578555
searchByUsername: function(done) {
579556
ig.user_search('richellemead', function(err, users, limit) {
@@ -611,6 +588,7 @@ exports.getInstagram = function(req, res, next) {
611588
* GET /api/yahoo
612589
* Yahoo API example.
613590
*/
591+
614592
exports.getYahoo = function(req, res) {
615593
Y.YQL('SELECT * FROM weather.forecast WHERE (location = 10007)', function(response) {
616594
var location = response.query.results.channel.location;

0 commit comments

Comments
 (0)