Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add filter keyword function to replace spaces with %20 #120

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
takanome-dev marked this conversation as resolved.
Show resolved Hide resolved
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');
};