Skip to content

Commit

Permalink
Feature para filtragem de linhas especificas
Browse files Browse the repository at this point in the history
Signed-off-by: rumd3x <[email protected]>
  • Loading branch information
rumd3x committed Jan 26, 2019
1 parent 30daf6e commit 9312879
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ SLACK_KEY='your-slack-bot-oauth-key-here'
SLACK_CHANNEL='general'
NOTIFY_LEVEL=0
NOTIFY_DAYS=all
NOTIFY_LINES=all
1 change: 1 addition & 0 deletions App/Modules/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct()
$dotenv->required('SLACK_CHANNEL')->notEmpty();
$dotenv->required('NOTIFY_LEVEL')->isInteger();
$dotenv->required('NOTIFY_DAYS')->notEmpty();
$dotenv->required('NOTIFY_LINES')->notEmpty();

$this->notifier = new Notifier();
$this->statusHandler = new StatusHandler();
Expand Down
27 changes: 26 additions & 1 deletion App/Modules/StatusHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ private function buildStatus(array $statusArray)
{
$return = collect();
foreach ($statusArray as $status) {
$statusObj = new LineStatus(new Line($status->codigo));
$line = new Line($status->codigo);
if (!$this->shouldNotifyLine($line)) {
continue;
}
$statusObj = new LineStatus($line);
$statusObj->dthOcorrencia = Carbon::parse($status->criado);
$statusObj->dthAtualizado = Carbon::parse($status->modificado);
$statusObj->situacao = $status->situacao;
Expand All @@ -96,6 +100,27 @@ private function buildStatus(array $statusArray)
return $return;
}

/**
* Parse NOTIFY_LINES config and filters by it
*
* @param Line $line
* @return bool
*/
private function shouldNotifyLine(Line $line)
{
$linesToNotify = getenv('NOTIFY_LINES');

if (strtolower(trim($linesToNotify)) === 'all') {
return true;
}

$linesToNotify = collect(explode(',', $linesToNotify))->map(function ($item) {
return (int) $item;
});

return $linesToNotify->contains($line->linha);
}

/**
* @param array $oldStatus
* @param array $newStatus
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ all: Enviar Notificações todos os dias
- Exemplo:
Para receber notificações todos os dias use `NOTIFY_DAYS=all`. Para receber notificações somente em dias da semana use `NOTIFY_DAYS=1,2,3,4,5`.

#### NOTIFY_LINES
A configuração `NOTIFY_LINES` diz as linhas dos trens/metrô que deverão ser monitoradas. Deve conter o numero das linhas separados por vírgula.

Os valores são:
```
all: Enviar Notificações para todas as linhas
1: Linha 1 Azul do Metrô
2: Linha 2 Verde do Metrô
3: Linha 3 Vermelha do Metrô
4: Linha 4 Amarela do Metrô
5: Linha 5 Lilás do Metrô
6: Linha 6 Laranja do Metrô
7: Linha 7 Rubi da CPTM
8: Linha 8 Diamante da CPTM
9: Linha 9 Esmeralda da CPTM
10: Linha 10 Turquesa da CPTM
11: Linha 11 Coral da CPTM
12: Linha 12 Safira da CPTM
13: Linha 13 Jade da CPTM
15: Linha 15 Prata da CPTM
17: Linha 17 Ouro da CPTM
```

- Exemplo:
Para receber notificações de todas as linhas `NOTIFY_LINES=all`. Para receber notificações somente da linha azul e amarela use `NOTIFY_LINES=1,4`.

### Debugando
#### Se você fez tudo acima corretamente e não está recebendo notificações em seu canal do slack:
* Verifique se o composer foi executado corretamente
Expand Down

0 comments on commit 9312879

Please sign in to comment.