Skip to content
This repository has been archived by the owner on Nov 10, 2019. It is now read-only.

Commit

Permalink
Fix tweet url
Browse files Browse the repository at this point in the history
  • Loading branch information
sallar committed Sep 23, 2018
1 parent 5cae47b commit 9fa0455
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
7 changes: 2 additions & 5 deletions src/index.js
Expand Up @@ -3,7 +3,7 @@ const bodyParser = require("body-parser");
const cache = require("memory-cache");
const cors = require("cors");
const VError = require("verror").WError;
const { twitter, alerts, fetch } = require("./utils");
const { getTwitterMediaUrl, alerts, fetch } = require("./utils");

const app = express();

Expand All @@ -14,8 +14,6 @@ app.use(
})
);

twitter.createClient();

app.get("/", (req, res) => {
res.send(`memsize=${cache.memsize()}`);
});
Expand Down Expand Up @@ -44,8 +42,7 @@ app.post("/v1/tweetMedia", (req, res, next) => {
return next(new VError(alerts.error.imageInvalid));
}

twitter
.getMediaUrl(image)
getTwitterMediaUrl(image)
.then(mediaUrl =>
res.json({
mediaUrl
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.js
@@ -1,9 +1,9 @@
const { fetchDataForAllYears } = require("./fetch");
const alerts = require("./alerts");
const twitter = require("./twitter")(alerts);
const getTwitterMediaUrl = require("./twitter")(alerts);

module.exports = {
fetch: fetchDataForAllYears,
alerts,
twitter
getTwitterMediaUrl
};
23 changes: 8 additions & 15 deletions src/utils/twitter.js
Expand Up @@ -2,19 +2,15 @@ const Twitter = require("twitter");
const VError = require("verror").WError;
const dataUriToBuffer = require("data-uri-to-buffer");

module.exports = function(alerts) {
const twitter = {};
const twitterClient = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});

twitter.createClient = function() {
return new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
};

twitter.getMediaUrl = async function getMediaUrl(base64data) {
module.exports = alerts =>
async function getMediaUrl(base64data) {
try {
const buff = dataUriToBuffer(base64data);
const mediaResponse = await twitterClient.post("media/upload", {
Expand All @@ -32,6 +28,3 @@ module.exports = function(alerts) {
throw new VError(err, alerts.error.imageUploadFailed);
}
};

return twitter;
};

0 comments on commit 9fa0455

Please sign in to comment.