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

Fizzbuzz - Contribución Open Source #178

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Tests in my project every push on GitHub
name: Run Tests in my project every push/Pull Request on GitHub

on: [push, pull_request]

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
package.json
Binary file added images/endPoint-stack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions lib/controllers/ExplorerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class ExplorerController{
const explorers = Reader.readJsonFile("explorers.json");
return ExplorerService.getAmountOfExplorersByMission(explorers, mission);
}

static getExplorersByStack(stack){
const explorers = Reader.readJsonFile("explorers.json");
return ExplorerService.getExplorersByStacks(explorers, stack);
}
}

module.exports = ExplorerController;
6 changes: 6 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ app.get("/v1/fizzbuzz/:score", (request, response) => {
response.json({score: score, trick: fizzbuzzTrick});
});

app.get("/v1/explorers/stack/:stack", (request, response) => {
const stack = request.params.stack;
const explorersStacks = ExplorerController.getExplorersByStack(stack);
response.json(explorersStacks);
});

app.listen(port, () => {
console.log(`FizzBuzz API in localhost:${port}`);
});
Expand Down
4 changes: 4 additions & 0 deletions lib/services/ExplorerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ExplorerService {
return explorersUsernames;
}

static getExplorersByStacks (explorers, stack){
const explorersByStack = explorers.filter((explorer) => explorer.stacks.includes(stack));
return explorersByStack;
}
}

module.exports = ExplorerService;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "node ./node_modules/.bin/jest",
"test": "node ./node_modules/jest/bin/jest.js",
"linter": "node ./node_modules/eslint/bin/eslint.js",
"linter-fix": "node ./node_modules/eslint/bin/eslint.js . --fix",
"server": "node ./lib/server.js"
Expand Down
53 changes: 44 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
# Linter
# Contribución Open Source

1. Instalar dependencia:
1. Instalamos las dependencias necesarias para el proyecto con el siguiente comando:
```
npm i
```

> npm install eslint --save-dev
2. Se creó el método `getExplorersByStacks` en el archivo ExplorerService.js, dicho método recibe dos parámetros (explorers y stacks). Dentro del método se hace uso del método filter para así poder filtrar a los usuarios dependiendo de sus stack.

2. Modificar package.json, agregar debajo de test
```
static getExplorersByStacks (explorers, stack){
const explorersByStack = explorers.filter((explorer) => explorer.stacks.includes(stack));
return explorersByStack;
}
```

> "linter": "node ./node_modules/eslint/bin/eslint.js"
3. Antes de continuar se realizarón las pruebas de unidad para el método creado en el paso anterior, las pruebas se ubican en el archivo ExplorerService.test.js. Para las pruebas se utilizo el archivo de explorers.json el cuál contiene la lista de explorers asi que se importo la clase Reader:
```
const Reader = require("../../lib/utils/reader");
```
* Después, se creó una constante llamada explorers para poder hacer uso de la clase importada en el paso anterior y como parámetro se envía el archivo de explorers.json:
```
const explorers = Reader.readJsonFile("explorers.json");
```

3. Crear configuración en archivo .eslintrc (si se versiona)
* En el caso de las pruebas se realizaron dos test, uno para verificar si se recibía la lista de explorers con el stack "javascript" y el otro para verificar la cantidad de explorers con el mismo stack. Dando como resultado:

> npm init @eslint/config
![Archivo ExplorerService.test.js](./images/test.png "Archivo ExplorerService.test.js")

Rules: https://eslint.org/docs/rules/
Airbnb Code Style: https://github.com/airbnb/javascript
4. En el archivo ExplorerController.js, se creó un método llamado `getExplorersByStack` y recibe un parámetro llamado stack. Al crear este método se permite extender un puente entre la funcionalidad y el server. El método queda de la siguiente manera:
```
static getExplorersByStack(stack){
const explorers = Reader.readJsonFile("explorers.json");
return ExplorerService.getExplorersByStacks(explorers, stack);
}
```
5. Por último, en el archivo `server.js`, se creó el nuevo endpoint solicitado que regresa toda la lista de explorers filtrados por un stack.
```
app.get("/v1/explorers/stack/:stack", (request, response) => {
const stack = request.params.stack;
const explorersStacks = ExplorerController.getExplorersByStack(stack);
response.json(explorersStacks);
});
```
## Resultado

Se puede verificar en el navegador si se obtiene respuesta al buscar a los explorers por stack:

![Endpoint para obtener la lista de explores por el stack](./images/endPoint-stack.png "Endpoint para obtener la lista de explores por el stack")

Por último, se corre el comando `npm run linter-fix`, con ellos los archivos quedarán con el formato definido en las reglas que se establecieron en la guía de estilo (Linter).
147 changes: 147 additions & 0 deletions test/services/ExplorerService.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,157 @@
const Reader = require("../../lib/utils/reader");
const ExplorerService = require("./../../lib/services/ExplorerService");

describe("Tests para ExplorerService", () => {
const explorers = Reader.readJsonFile("explorers.json");

test("Requerimiento 1: Calcular todos los explorers en una misión", () => {
const explorers = [{mission: "node"}];
const explorersInNode = ExplorerService.filterByMission(explorers, "node");
expect(explorersInNode.length).toBe(1);
});

test("Requerimiento 4: Calcular todos los explorers que tengan en stack un valor recibido", () => {
const explorersByStacks = ExplorerService.getExplorersByStacks(explorers, "javascript");
expect(explorersByStacks).toEqual([
{
"name": "Woopa1",
"githubUsername": "ajolonauta1",
"score": 1,
"mission": "node",
"stacks": [
"javascript",
"reasonML",
"elm"
]
},
{
"name": "Woopa2",
"githubUsername": "ajolonauta2",
"score": 2,
"mission": "node",
"stacks": [
"javascript",
"groovy",
"elm"
]
},
{
"name": "Woopa4",
"githubUsername": "ajolonauta4",
"mission": "node",
"score": 4,
"stacks": [
"javascript"
]
},
{
"name": "Woopa5",
"githubUsername": "ajolonauta5",
"score": 5,
"mission": "node",
"stacks": [
"javascript",
"elixir",
"elm"
]
},
{
"name": "Woopa9",
"githubUsername": "ajolonauta9",
"score": 9,
"mission": "java",
"stacks": [
"javascript",
"elixir",
"groovy",
"reasonML",
"elm"
]
},
{
"name": "Woopa10",
"githubUsername": "ajolonauta10",
"score": 10,
"mission": "java",
"stacks": [
"javascript",
"elixir",
"groovy",
"reasonML",
"elm"
]
},
{
"name": "Woopa11",
"githubUsername": "ajolonauta11",
"score": 11,
"mission": "node",
"stacks": [
"javascript",
"elixir",
"groovy",
"reasonML",
"elm"
]
},
{
"name": "Woopa12",
"githubUsername": "ajolonauta12",
"score": 12,
"mission": "node",
"stacks": [
"javascript",
"elixir",
"groovy",
"reasonML",
"elm"
]
},
{
"name": "Woopa13",
"githubUsername": "ajolonauta13",
"score": 13,
"mission": "node",
"stacks": [
"javascript",
"elixir",
"groovy",
"reasonML",
"elm"
]
},
{
"name": "Woopa14",
"githubUsername": "ajolonauta14",
"score": 14,
"mission": "node",
"stacks": [
"javascript",
"elixir",
"groovy",
"reasonML",
"elm"
]
},
{
"name": "Woopa15",
"githubUsername": "ajolonauta15",
"score": 15,
"mission": "node",
"stacks": [
"javascript",
"elixir",
"groovy",
"reasonML",
"elm"
]
}
]);
});

test('Requerimiento 4: Obtener la cantidad de explorers que tengan en stack un valor recibido', () => {
const explorersByStacks = ExplorerService.getExplorersByStacks(explorers, "javascript");
expect(explorersByStacks.length).toBe(11);
})

});