Skip to content

Commit

Permalink
Merge pull request #120 from TAKANOME-DEV/fix-spaces-in-keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
MizouziE authored Sep 2, 2022
2 parents 253caa2 + 90bab6e commit a1dcc1a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 29 deletions.
17 changes: 17 additions & 0 deletions __tests__/unit-tests/filterKeyword.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const filterKeyword = require('../../src/utils/filterKeyword');

describe('Filter keyword function', () => {
let keyword = '';

it('should replace the space with %20 in the keyword', () => {
keyword = 'energy price';
const value = filterKeyword(keyword);
expect(value).toBe('energy%20price');
});

it('should replace all the spaces with %20 in the keyword', () => {
keyword = `electricity bill company firms`;
const value = filterKeyword(keyword);
expect(value).toBe('electricity%20bill%20company%20firms');
});
});
5 changes: 3 additions & 2 deletions src/controller/api/twitterAPIController.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
const axios = require('axios');
const keywords = require('../../data/keywords');
const twitterAccounts = require('../../data/accounts.json');
const filterKeyword = require('../../utils/filterKeyword');
const tweets = [];

keywords.forEach((keyword) => {
twitterAccounts.forEach(async (twitterAccount) => {
await axios.get(
'https://twitter.com/search?p=from%3A' +
twitterAccount.account +
`${keyword}`
`${filterKeyword(keyword)}`
);

tweets.push({
'See tweets from': '@' + twitterAccount.account,
link:
'https://twitter.com/search?q=from%3A' +
twitterAccount.account +
`${keyword}`,
`${filterKeyword(keyword)}`,
});
});
});
Expand Down
30 changes: 15 additions & 15 deletions src/data/keywords.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//List of keywords
module.exports = keywords = [
"green energy",
"energy price",
"energy prices",
"energy firms",
"renewable energy",
"energy company",
"energy tax",
"energy bill", // new
"energy resources", // new
"electricity bill", // new
"gas bill", // new
"electricity costs", // new
"energy", // new
"gas prices", //new
"petrol prices" //new
'green energy',
'energy price',
'energy prices',
'energy firms',
'renewable energy',
'energy company',
'energy tax',
'energy bill', // new
'energy resources', // new
'electricity bill', // new
'gas bill', // new
'electricity costs', // new
'energy', // new
'gas prices', //new
'petrol prices', //new
];
24 changes: 12 additions & 12 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const path = require("path");
const express = require("express");
const main = require("./routes/main");
const apiNews = require("./routes/api/news");
const apiTwitter = require("./routes/api/twitter");
const rateLimit = require("express-rate-limit");
const path = require('path');
const express = require('express');
const main = require('./routes/main');
const apiNews = require('./routes/api/news');
const apiTwitter = require('./routes/api/twitter');
const rateLimit = require('express-rate-limit');

function createServer() {
const app = express();

// set up rate limiter: maximum of 60 requests per minute
const limiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 60, // 1 requeest per second
max: 60, // 1 request per second
});

// apply rate limiter to all requests
app.use(limiter);
app.use(express.static("public"));
app.use("/", main);
app.use("/api/news", apiNews);
app.use("/api/twitter", apiTwitter);
app.use(express.static('public'));
app.use('/', main);
app.use('/api/news', apiNews);
app.use('/api/twitter', apiTwitter);
app.use((req, res, next) => {
res.status(404).sendFile(path.resolve("./src/views/not-found.html"));
res.status(404).sendFile(path.resolve('./src/views/not-found.html'));
});

return app;
Expand Down
8 changes: 8 additions & 0 deletions src/utils/filterKeyword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
*
* @param keyword the keyword to filter
* @returns string where space is replaced by %20
*/
module.exports = function filterKeyword(keyword) {
return keyword.replaceAll(/\s/g, '%20');
};

0 comments on commit a1dcc1a

Please sign in to comment.