Skip to content

Commit

Permalink
Merge ee190dd into f3b2174
Browse files Browse the repository at this point in the history
  • Loading branch information
huyphamily committed Sep 1, 2014
2 parents f3b2174 + ee190dd commit 896ccc1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
55 changes: 53 additions & 2 deletions server/api/search/grab.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@ var _ = require('lodash');
var request = require('request');
var instagramKey = process.env.INSTAGRAM_KEY;

//setting our error counter
var instagramError = 0;
var redditError = 0;

exports.instagram = function(query, cb) {
var storage = [];

request('https://api.instagram.com/v1/tags/'+ query.split(" ").join("") +'/media/recent?client_id=' + instagramKey, function(error, response, result){
if(error){console.log(error);}
//handling errors and retries
if(error && instagramError < 3){
console.log('instagram error', error);
instagramError++;
exports.instagram(query, cb);
return;
} else if (error && instagramError >= 3){
console.log('instagram error', error);
cb(error);
instagramError = 0;
return;
}

//resetting counter
instagramError = 0;

//parsing the json string
var data = JSON.parse(result).data;

//grabing specific data from instagram
data.forEach(function(value){
var obj = {};
obj.picture = value.images.standard_resolution.url;
Expand All @@ -21,6 +42,7 @@ exports.instagram = function(query, cb) {
storage.push(obj);
});

//passing the array of data into the callback
cb( storage );
});
};
Expand All @@ -29,9 +51,37 @@ exports.reddit = function(query, cb) {
var storage = [];

request('http://www.reddit.com/search.json?q='+ query, function(error, response, result){
if(error){console.log(error);}
//handling 504 errors sent back by reddit
if(response.statusCode === 504 && redditError < 3){
console.log('reddit 504 error');
redditError++;
exports.reddit(query, cb);
return;
} else if (response.statusCode === 504 && redditError >= 3) {
cb({'error': '504'});
redditError = 0;
return;
//handling other errors
} else if (error && redditError < 3) {
console.log('reddit error', error);
redditError++;
exports.reddit(query, cb);
return;
} else if ( error && redditError >= 3) {
console.log('reddit error', error);
cb(error);
redditError = 0;
return;
}

//resetting counter
redditError = 0;
// console.log('reddit statuscode', response.statusCode );

//parsing the json string
var data = JSON.parse(result).data.children;

//grabing specific data from reddit
data.forEach(function(value){
var obj = {};
obj.title = value.data.title;
Expand All @@ -41,6 +91,7 @@ exports.reddit = function(query, cb) {
storage.push(obj);
});

//passing array of data into the callback
cb( storage );
});

Expand Down
2 changes: 1 addition & 1 deletion server/api/search/search.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ exports.index = function(req, res) {
checkToggle();
});

twitter.getTweets(query, function(result){
twitter.getTweets(query, function(result){
data.twitter = result;
toggle.twitter = true;
checkToggle();
Expand Down
32 changes: 19 additions & 13 deletions server/api/search/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@
var should = require('should');
var app = require('../../app');
var request = require('supertest');
var grab = require('./grab.js');
var result;

describe('GET /api/search', function() {
this.timeout(15000);

it('should respond with Object with each social media as keys', function(done) {
request(app)
.get('/api/search?q=hello+there')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
res.body.should.be.instanceof(Object);
res.body.instagram.should.be.instanceof(Array);
result = res.body;
result.should.be.instanceof(Object);
result.should.have.keys('twitter', 'instagram', 'reddit');
done();
});
});
});

// it('should respond with an Array for each Key', function(done) {
// request(app)
// .get('/api/search')
// .expect(200)
// .end(function(err, res) {
// if (err) return done(err);
// res.body.should.be.instanceof(Object);
// done();
// });
// });
it('should respond with Object with Instagram information', function(done) {

result.instagram.should.be.instanceof(Array);
result.instagram[0].link.should.be.instanceof(String);
done();

});

it('should respond with Object with Reddit information', function(done) {

result.reddit.should.be.instanceof(Array);
result.reddit[0].link.should.be.instanceof(String);
done();

});

});

0 comments on commit 896ccc1

Please sign in to comment.