Skip to content

Commit

Permalink
Update routes, apiclient, twitterapiclient and templates
Browse files Browse the repository at this point in the history
Finished fetching data from the twitter api and rendering it through
the template files. See #1
  • Loading branch information
carloscuesta committed Dec 26, 2015
1 parent c1022ac commit 89c59ad
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 98 deletions.
1 change: 1 addition & 0 deletions src/app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ exports.index = function(req, res) {
Promise.all([ghUserCCStars, userTimeline]).then(function(data) {
res.render('views/index', {
githubData: data[0],
twitterData: data[1],
me: staticData.me,
site: staticData.site,
social: staticData.social
Expand Down
162 changes: 95 additions & 67 deletions src/app/scripts/apiclient.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,99 @@
'use strict';

var fetch = require('node-fetch'),
cache = require('memory-cache');

var ApiClient = (function(options) {
this.options = options;
this.baseUrl = options.base_url || '//';
this.cacheTime = options.cache * 6000 || 3000;
this.oAuth = options.oauth || false;
this.accesToken = options.accesToken;
this.accesTokenSecret = options.accesTokenSecret;

this.serialize = function(params) {
if(params){
return Object.keys(params).map(function(key) {
return key + '=' + params[key];
}).join('&');
} else {
return '';
}
};

this.getOAuth = function(src, params) {
var _self = this,
_url = _self.baseUrl + src + '?' + _self.serialize(params),
dataCache = cache.get(_url);

oAuth.get(_url, this.accesToken, this.accesTokenSecret, function (error, data, res) {
if (!error && res.statusCode == 200) {
if (!dataCache) {
return fetch(_url)
.then(function(data) {
if(data){
var dataParsed = data.json();
cache.put(_url, dataParsed, _self.cacheTime);
return dataParsed;
} else {
return data;
}
});
} else {
return Promise.resolve(dataCache);
}
}
});
};

this.get = function(src, params) {
var _self = this,
_url = _self.baseUrl + src + '?' + _self.serialize(params),
dataCache = cache.get(_url);

if (!dataCache) {
return fetch(_url)
.then(function(data) {
if(data){
var dataParsed = data.json();
cache.put(_url, dataParsed, _self.cacheTime);
return dataParsed;
} else {
return data;
}
});
} else {
return Promise.resolve(dataCache);
}
};
Object.defineProperty(exports, '__esModule', {
value: true
});

module.exports = ApiClient;
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

var Fetch = require('node-fetch');
var OAuth = require('oauth');

var GetApiClient = (function () {
function GetApiClient(options) {
_classCallCheck(this, GetApiClient);

this.baseUrl = options.base_url || '//';
this.oauth = options.oauth || false;
this.debug = options.debug || false;
this.oAuth = this.oauth ? new OAuth.OAuth(this.oauth.request_token, this.oauth.access_token, this.oauth.consumer_key, this.oauth.consumer_secret, '1.0A', null, 'HMAC-SHA1') : false;
}

_createClass(GetApiClient, [{
key: 'consoleDebugMessage',
value: function consoleDebugMessage(text, data) {
if (this.debug) {
console.log('\x1b[36m', text, '\x1b[0m', data);
}
}
}, {
key: 'serialize',
value: function serialize(params) {
return Object.keys(params).map(function (key) {
return key + '=' + params[key];
}).join('&');
}
}, {
key: 'onGetData',
value: function onGetData(data) {

if (!!data && !!data.json) {
data = data.json();
}

return data;
}
}, {
key: 'fetchData',
value: function fetchData(url) {
var _self = this;

return Fetch(url).then(function (data) {
return _self.onGetData(data);
});
}
}, {
key: 'getDataOAuth',
value: function getDataOAuth(url) {
var _self = this;

return new Promise(function (resolve, reject) {
_self.oAuth.get(url, _self.oauth.access_token, _self.oauth.access_token_secret, function (e, data, res) {
if (e) {
reject(e);
} else {
resolve(_self.onGetData(JSON.parse(data)));
}
});
});
}
}, {
key: 'getData',
value: function getData(url) {
var _self = this;

return _self.fetchData(url);
}
}, {
key: 'get',
value: function get(src, params) {
var _self = this,
_url = '' + _self.baseUrl + src + '?' + _self.serialize(params);

_self.consoleDebugMessage('url', _url);

if (_self.oauth) {
return this.getDataOAuth(_url);
}

return this.getData(_url);
}
}]);

return GetApiClient;
})();

exports['default'] = GetApiClient;
module.exports = exports['default'];
44 changes: 13 additions & 31 deletions src/app/scripts/twitterapiclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,22 @@ var ApiClient = require('./apiclient'),

var TwitterApiClient = (function() {

var oAuth = new OAuth.OAuth (
'https://api.twitter.com/oauth/request_token',
'https://api.twitter.com/oauth/access_token',
process.env.TWITTER_CONSUMER_KEY,
process.env.TWITTER_CONSUMER_KEY_SECRET,
'1.0A',
null,
'HMAC-SHA1'
);

var _ApiClient = new ApiClient({
'base_url': 'https://api.twitter.com/1.1',
'cache': 5,
'oauth': oAuth,
'accesToken': process.env.TWITTER_CONSUMER_KEY,
'accesTokenSecret': process.env.TWITTER_CONSUMER_KEY_SECRET,
});

var getOAuthRequestToken = function() {
oAuth.getOAuthRequestToken(function (error, oauth_token, oauth_token_secret) {
if (error) {
console.log(error);
} else {
var oauth = {};
oAuth.token = oauth_token;
oAuth.token_secret = oauth_token_secret;
next(oauth);
}
});
};
base_url: 'https://api.twitter.com/1.1',
oauth: {
request_token: 'https://api.twitter.com/oauth/request_token',
access_token: 'https://api.twitter.com/oauth/access_token',
consumer_key: 'vpF1dfWXDHiFaMT7LTFl0TOSq',
consumer_secret: 'HwQWzgRDXe4FBKiQXpcoqyOgJhDuo9z4KQqdY8przFNlVgsQcB',
access_token: '2974751872-3D4Bb0YFIJBadjAWZ3IP0vgPxOnEUilSiExbWqM',
access_token_secret: 'LLMzB5Itdv6i4VaqPVmMmXi2rZXmDxN3v1SDw0bDHwXnl',
},
debug: true
});


var getUserTimeline = function(params) {
getOAuthRequestToken();
return _ApiClient.get('statuses/user_timeline', params);
return _ApiClient.get('/statuses/user_timeline.json', params);
};

return {
Expand Down
1 change: 1 addition & 0 deletions src/app/templates/_includes/_body.jade
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include _sections/_aboutme.jade
include _sections/_github.jade
include _sections/_twitter.jade
include _sections/_contact.jade
include _footer.jade
script.
Expand Down
6 changes: 6 additions & 0 deletions src/app/templates/_includes/_sections/_twitter.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
section.twitter
.wrap
.row
h3 Twitter
-each tweet in twitterData
p #{tweet.text}

0 comments on commit 89c59ad

Please sign in to comment.