-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update routes, apiclient, twitterapiclient and templates
Finished fetching data from the twitter api and rendering it through the template files. See #1
- Loading branch information
1 parent
c1022ac
commit 89c59ad
Showing
5 changed files
with
116 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |