Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Vinicius committed Sep 13, 2018
0 parents commit b98cd00
Show file tree
Hide file tree
Showing 19 changed files with 1,035 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
.git
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
6 changes: 6 additions & 0 deletions Dockerfile
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" ]
8 changes: 8 additions & 0 deletions README.md
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
13 changes: 13 additions & 0 deletions api/index.js
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;
22 changes: 22 additions & 0 deletions app.js
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;
8 changes: 8 additions & 0 deletions configs/index.js
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
};
};
16 changes: 16 additions & 0 deletions controllers/feeds.js
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 added controllers/media.js
Empty file.
Empty file added controllers/tweet.js
Empty file.
16 changes: 16 additions & 0 deletions controllers/user.js
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
});
}
};
9 changes: 9 additions & 0 deletions docker-compose.yml
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"
24 changes: 24 additions & 0 deletions lib/response.handlers.js
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);
}
42 changes: 42 additions & 0 deletions lib/twitter.client.js
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)
);
}

}
30 changes: 30 additions & 0 deletions lib/twitter.errors.js
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
};
}

Loading

0 comments on commit b98cd00

Please sign in to comment.