forked from vmvini/twtest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcus Vinicius
committed
Sep 13, 2018
0 parents
commit b98cd00
Showing
19 changed files
with
1,035 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
npm-debug.log | ||
.git |
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,2 @@ | ||
node_modules | ||
.env |
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 @@ | ||
FROM node:8-alpine | ||
WORKDIR /usr/src/app | ||
COPY . . | ||
RUN npm install | ||
EXPOSE 3000 | ||
CMD [ "npm", "start" ] |
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,8 @@ | ||
INSTRUCTIONS | ||
|
||
rename template.env to .env | ||
put your twitter keys there | ||
|
||
execute 'docker-compose build' and 'docker-compose up' | ||
|
||
try request localhost:8080 |
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,13 @@ | ||
const express = require('express'); | ||
const feeds = require('./../controllers/feeds'); | ||
const users = require('./../controllers/user'); | ||
//const media = require('./../controllers/media'); | ||
//const tweet = require('./../controllers/tweet'); | ||
const router = express.Router(); | ||
|
||
router.get('/feeds/:token', feeds); | ||
router.get('/users/:token', users); | ||
//router.post('/media/:token', media); | ||
//router.post('/tweet/:token', tweet); | ||
|
||
module.exports = router; |
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,22 @@ | ||
require('dotenv').config(); | ||
const express = require('express'); | ||
const createError = require('http-errors'); | ||
const logger = require('morgan'); | ||
const app = express(); | ||
const api = require('./api'); | ||
|
||
app.use(logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use('/', api); | ||
app.use((req, res, next) => { | ||
res | ||
.status(404) | ||
.json({ | ||
...createError(404), | ||
success: false, | ||
message_code: 404 | ||
}); | ||
}); | ||
|
||
module.exports = app; |
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,8 @@ | ||
exports.getTwitterCredentials = function(access_token_secret) { | ||
return { | ||
consumer_key: process.env.TWITTER_CONSUMER_KEY, | ||
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | ||
access_token_key: process.env.TWITTER_ACCESS_TOKEN, | ||
access_token_secret | ||
}; | ||
}; |
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,16 @@ | ||
const getClient = require('./../lib/twitter.client'); | ||
const responseHandler = require('./../lib/response.handlers').responseHandler; | ||
const sendResponse = require('./../lib/response.handlers').sendResponse; | ||
const getTwitterCredentials = require('./../configs').getTwitterCredentials; | ||
|
||
module.exports = function(req, res) { | ||
getClient(getTwitterCredentials(req.params.token)) | ||
.timeline((err, timeline) => responseHandler(res, err, success(timeline) )); | ||
|
||
function success(timeline) { | ||
return (defaultData) => sendResponse(res, 200, { | ||
feeds: timeline, | ||
...defaultData | ||
}); | ||
} | ||
}; |
Empty file.
Empty file.
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,16 @@ | ||
const getClient = require('./../lib/twitter.client'); | ||
const responseHandler = require('./../lib/response.handlers').responseHandler; | ||
const sendResponse = require('./../lib/response.handlers').sendResponse; | ||
const getTwitterCredentials = require('./../configs/index').getTwitterCredentials; | ||
|
||
module.exports = function(req, res) { | ||
getClient(getTwitterCredentials(req.params.token)) | ||
.userinfo((err, user) => responseHandler(res, err, success(user) )); | ||
|
||
function success(user) { | ||
return (defaultData) => sendResponse(res, 200, { | ||
user: user, | ||
...defaultData | ||
}); | ||
} | ||
}; |
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,9 @@ | ||
version: '2' | ||
services: | ||
|
||
twitter: | ||
build: | ||
context: ./ | ||
dockerfile: ./Dockerfile | ||
ports: | ||
- "8080:3000" |
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,24 @@ | ||
const getErrorDetails = require('./twitter.errors'); | ||
|
||
exports.sendResponse = sendResponse; | ||
|
||
const defaultSuccessData = { | ||
success: true, | ||
message_code: 200, | ||
message: 'success' | ||
}; | ||
|
||
exports.responseHandler = function responseHandler(res, err, success) { | ||
if (err) { | ||
e = getErrorDetails(err); | ||
return sendResponse(res, 400, e); | ||
} else { | ||
return success(defaultSuccessData); | ||
} | ||
}; | ||
|
||
function sendResponse(res, status, data) { | ||
return res | ||
.status(status) | ||
.json(data); | ||
} |
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,42 @@ | ||
const Twitter = require('twitter'); | ||
|
||
module.exports = function getClient(credentials = { | ||
consumer_key, | ||
consumer_secret, | ||
access_token_key, | ||
access_token_secret | ||
}) { | ||
return ClientFactory(credentials); | ||
} | ||
|
||
function ClientFactory(credentials) { | ||
|
||
const client = new Twitter(credentials); | ||
|
||
return { | ||
timeline: getAuthenticatedUserTimeline, | ||
userinfo: getAuthenticatedUserInfo | ||
}; | ||
|
||
function getAuthenticatedUserTimeline(callback, count = 50) { | ||
client.get( | ||
'statuses/home_timeline', | ||
{ | ||
count | ||
}, | ||
(error, tweets, response) => callback(error, tweets, response) | ||
); | ||
} | ||
|
||
function getAuthenticatedUserInfo(callback) { | ||
client.get( | ||
'account/verify_credentials', | ||
{ | ||
include_email: true, | ||
skip_status: false | ||
}, | ||
(error, tweets, response) => callback(error, tweets, response) | ||
); | ||
} | ||
|
||
} |
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,30 @@ | ||
module.exports = function getErrorDetails(err) { | ||
if ( err.length > 1 ) { | ||
return multipleErrors(); | ||
} | ||
return singleError(); | ||
|
||
function singleError() { | ||
return createErrorResponse(err[0].code, err[0].message ); | ||
} | ||
|
||
function multipleErrors() { | ||
return createErrorResponse(400, concatErrorsMsgs(err)); | ||
} | ||
|
||
}; | ||
|
||
function concatErrorsMsgs(errors) { | ||
return errors | ||
.map(err => 'code: ' + err.code + ' msg: ' + err.message ) | ||
.join(';'); | ||
} | ||
|
||
function createErrorResponse(message_code, message) { | ||
return { | ||
success: false, | ||
message_code: message_code, | ||
message: message | ||
}; | ||
} | ||
|
Oops, something went wrong.