Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from brunooomelo/ft/quina
Browse files Browse the repository at this point in the history
feature quina
  • Loading branch information
brunooomelo committed Jan 23, 2019
2 parents dac6803 + 4d14a99 commit 57070ff
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ lottery --help
Usage: lottery <command> [flags]

where <command> is one of:
megasena
megasena, lotofacil, quina

lottery help <command> quick help on <command>
```
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "lottery-cli",
"version": "0.2.0",
"version": "0.3.0",
"main": "bin/lottery.js",
"description": "",
"description": "A CLI that will make a bettor's life easier",
"scripts": {
"test": "./node_modules/.bin/mocha test/**/*.test.js",
"test:watch": "yarn test -- --watch",
Expand Down
107 changes: 107 additions & 0 deletions src/cmd/quina.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const request = require('request-promise-native')
const ora = require('ora')
const { formatNumber } = require('accounting')

const spinner = ora({
text: 'Retrieving Lottery data...',
color: 'yellow'
})

module.exports = (args) => {
const contest = args.c || args.concurso
const query = `${contest ? `?concurso=${contest}` : ''}`
const token = `!ut/p/a1/jc69DoIwAATgZ_EJepS2wFgoaUswsojYxXQyTfgbjM9vNS4Oordd8l1yxJGBuNnfw9XfwjL78dmduIikhYFGA0tzSFZ3tG_6FCmP4BxBpaVhWQuA5RRWlUZlxR6w4r89vkTi1_5E3CfRXcUhD6osEAHA32Dr4gtsfFin44Bgdw9WWSwj/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_61L0H0G0J0VSC0AC4GLFAD20G6/res/id=buscaResultado/c=cacheLevelPage/=`
const url = `http://loterias.caixa.gov.br/wps/portal/loterias/landing/quina/${token}/${query}`
spinner.start()
return request(url, { jar: true })
.then(stopSpinner)
.then(parseResponseToJson)
.then(extractLotteryValuesFromBody)
.then(verifyValues)
.then(verifyError)
.then(responseConsole)
}

const stopSpinner = (body) => {
spinner.stop()
return body
}

const parseResponseToJson = (response) => {
return JSON.parse(response)
}

const verifyValues = body => {
if (typeof body.vrEstimativa === 'string') {
const value = body.vrEstimativa.replace(/[.,]/g, '')
body.vrEstimativa = value.slice(0, value.length - 2)
return body
}
return body
}

const verifyError = body => {
return body
}

const extractLotteryValuesFromBody = (body) => {
return {
nu_concurso: body.concurso,
quina: body.ganhadores,
quadra: body.ganhadores_quadra,
terno: body.ganhadores_terno,
duque: body.qt_ganhador_duque,
vr_quina: body.valor,
vr_quadra: body.valor_quadra,
vr_terno: body.valor_terno,
vr_duque: body.vr_rateio_duque,
sorteioAcumulado: body.sorteioAcumulado,
vrEstimativa: body.vrEstimado,
resultadoOrdenado: body.resultadoOrdenado.replace(/-/g, ' '),
dt_apuracaoStr: body.dataStr,
dtProximoConcursoStr: body.dtProximoConcursoStr,
localSorteio: body.de_local_sorteio,
no_cidade: body.no_cidade,
sg_uf: body.sg_uf,
mensagens: body.mensagens,
error: body.error
}
}

const responseConsole = (body) => {
const responseHeader = `
-------------------------------------------------
Concurso: ${body.nu_concurso} - ${body.dt_apuracaoStr}
Sorteio realizado no ${body.localSorteio} em ${body.no_cidade}, ${body.sg_uf}
-------------------------------------------------
`
console.info(responseHeader)
console.info(` ${body.resultadoOrdenado}`)
console.info(`
${showText(body)}
Quadra: 4 números acertados
${body.quadra} apostas ganhadoras, R$ ${formatNumber(body.vr_quadra, 2, '.', ',')}
Terno: 3 números acertados
${body.terno} apostas ganhadoras, R$ ${formatNumber(body.vr_terno, 2, '.', ',')}
Duque: 2 números acertados
${body.duque} apostas ganhadoras, R$ ${formatNumber(body.vr_duque, 2, '.', ',')}
`)

console.info(`
-------------------------------------------------
Proximo Sorteio ${body.dtProximoConcursoStr}
Estimativa de prêmio é R$ ${formatNumber(body.vrEstimativa, 2, '.', ',')}
-------------------------------------------------`)
}

const showText = body => {
if (body.sorteioAcumulado === true) {
return ('ACUMULADO!!\n\n Quina: 15 números acertados\n Não houve ganhador')
}
return (
'Quina: 15 números acertados 🎉\n ' + body.quina + ' apostas ganhadoras, R$ ' + formatNumber(body.vr_quina, 2, '.', ',')
)
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module.exports = () => {
case 'lotofacil':
require('./cmd/lotofacil')(args)
break
case 'quina':
require('./cmd/quina')(args)
break
default:
error(`"${cmd}" is not a valid command!`, true)
break
Expand Down
177 changes: 177 additions & 0 deletions test/quina.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
const chai = require('chai')
const sinon = require('sinon')
const sinonChai = require('sinon-chai')
const nock = require('nock')
const { formatNumber } = require('accounting')

const expect = chai.expect
chai.use(sinonChai)

const quina = require('../src/cmd/quina')

describe('Quina', () => {
let consoleStub

const responseHeader = ({ concurso, data, local, cidade, estado }) => `
-------------------------------------------------
Concurso: ${concurso} - ${data}
Sorteio realizado no ${local} em ${cidade}, ${estado}
-------------------------------------------------
`

const responseAwardsWithWinner = `
Quina: 15 números acertados 🎉
1 apostas ganhadoras, R$ 3.927.137,86
Quadra: 4 números acertados
83 apostas ganhadoras, R$ 6.012,52
Terno: 3 números acertados
7054 apostas ganhadoras, R$ 106,38
Duque: 2 números acertados
168524 apostas ganhadoras, R$ 2,44
`
const responseAwardsWithoutWinner = `
ACUMULADO!!
Quina: 15 números acertados
Não houve ganhador
Quadra: 4 números acertados
65 apostas ganhadoras, R$ 6.910,61
Terno: 3 números acertados
5040 apostas ganhadoras, R$ 134,02
Duque: 2 números acertados
133790 apostas ganhadoras, R$ 2,77
`

const responseFooter = ({ data, valor }) => `
-------------------------------------------------
Proximo Sorteio ${data}
Estimativa de prêmio é R$ ${formatNumber(valor, 2, '.', ',')}
-------------------------------------------------`

const parseValue = body => {
if (typeof body.vrEstimativa === 'string') {
const value = body.vrEstimativa.replace(/[.,]/g, '')
body.vrEstimativa = value.slice(0, value.length - 2)
return body
}
return body
}

const responseMockAcumulado = {
'concurso': 4882,
'ganhadores': 0,
'ganhadores_quadra': 65,
'ganhadores_terno': 5040,
'qt_ganhador_duque': 133790,
'valor': 0,
'valor_quadra': 6910.61,
'valor_terno': 134.02,
'vr_rateio_duque': 2.77,
'sorteioAcumulado': true,
'vrEstimado': 4500000,
'resultadoOrdenado': '15-48-54-65-66',
'dataStr': '22/01/2019',
'dtProximoConcursoStr': '23/01/2019',
'de_local_sorteio': 'Caminhão da Sorte',
'no_cidade': 'QUIRINÓPOLIS',
'sg_uf': 'GO',
'mensagens': [],
'error': false
}
const responseMock = {
'concurso': 4877,
'ganhadores': 1,
'ganhadores_quadra': 83,
'ganhadores_terno': 7054,
'qt_ganhador_duque': 168524,
'valor': 3927137.86,
'valor_quadra': 6012.52,
'valor_terno': 106.38,
'vr_rateio_duque': 2.44,
'sorteioAcumulado': false,
'vrEstimado': 600000,
'resultadoOrdenado': '04-10-64-71-75',
'dataStr': '16/01/2019',
'dtProximoConcursoStr': '17/01/2019',
'de_local_sorteio': 'Espaço Loterias Caixa',
'no_cidade': 'SÃO PAULO',
'sg_uf': 'SP',
'mensagens': [],
'error': false
}

const paramsHeader = mock => ({
concurso: mock.concurso,
data: mock.dataStr,
local: mock.de_local_sorteio,
cidade: mock.no_cidade,
estado: mock.sg_uf
})

const paramsFooter = mock => ({
valor: mock.vrEstimado,
data: mock.dtProximoConcursoStr
})

beforeEach(() => {
consoleStub = sinon.stub(console, 'info')
})
afterEach(() => {
consoleStub.restore()
})

it('should search last lottery', async () => {
nock('http://loterias.caixa.gov.br')
.get('/wps/portal/loterias/landing/quina/!ut/p/a1/jc69DoIwAATgZ_EJepS2wFgoaUswsojYxXQyTfgbjM9vNS4Oordd8l1yxJGBuNnfw9XfwjL78dmduIikhYFGA0tzSFZ3tG_6FCmP4BxBpaVhWQuA5RRWlUZlxR6w4r89vkTi1_5E3CfRXcUhD6osEAHA32Dr4gtsfFin44Bgdw9WWSwj/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_61L0H0G0J0VSC0AC4GLFAD20G6/res/id=buscaResultado/c=cacheLevelPage/=/')
.reply(200, responseMockAcumulado)

const footer = paramsFooter(parseValue(responseMockAcumulado))
const header = paramsHeader(responseMockAcumulado)

await quina({})

expect(consoleStub).to.have.been.calledWith(responseHeader(header))
expect(consoleStub).to.have.been.calledWithMatch(responseMockAcumulado.resultadoOrdenado.replace(/-/g, ' '))
expect(consoleStub).to.have.been.calledWith(responseAwardsWithoutWinner)
expect(consoleStub).to.have.been.calledWith(responseFooter(footer))
})

it('should return the result of a specific search and have a winner', async () => {
nock('http://loterias.caixa.gov.br')
.get('/wps/portal/loterias/landing/quina/!ut/p/a1/jc69DoIwAATgZ_EJepS2wFgoaUswsojYxXQyTfgbjM9vNS4Oordd8l1yxJGBuNnfw9XfwjL78dmduIikhYFGA0tzSFZ3tG_6FCmP4BxBpaVhWQuA5RRWlUZlxR6w4r89vkTi1_5E3CfRXcUhD6osEAHA32Dr4gtsfFin44Bgdw9WWSwj/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_61L0H0G0J0VSC0AC4GLFAD20G6/res/id=buscaResultado/c=cacheLevelPage/=/')
.query({ concurso: 4877 })
.reply(200, responseMock)

const header = paramsHeader(responseMock)
const footer = paramsFooter(parseValue(responseMock))

await quina({ concurso: 4877 })
expect(consoleStub).to.have.been.calledWith(responseHeader(header))
expect(consoleStub).to.have.been.calledWithMatch(responseMock.resultadoOrdenado.replace(/-/g, ' '))
expect(consoleStub).to.have.been.calledWith(responseAwardsWithWinner)
expect(consoleStub).to.have.been.calledWith(responseFooter(footer))
})

it('should return the result of a specific search', async () => {
nock('http://loterias.caixa.gov.br')
.get('/wps/portal/loterias/landing/quina/!ut/p/a1/jc69DoIwAATgZ_EJepS2wFgoaUswsojYxXQyTfgbjM9vNS4Oordd8l1yxJGBuNnfw9XfwjL78dmduIikhYFGA0tzSFZ3tG_6FCmP4BxBpaVhWQuA5RRWlUZlxR6w4r89vkTi1_5E3CfRXcUhD6osEAHA32Dr4gtsfFin44Bgdw9WWSwj/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_61L0H0G0J0VSC0AC4GLFAD20G6/res/id=buscaResultado/c=cacheLevelPage/=/')
.query({ concurso: 4882 })
.reply(200, responseMockAcumulado)

const header = paramsHeader(responseMockAcumulado)
const footer = paramsFooter(parseValue(responseMockAcumulado))

await quina({ concurso: 4882 })

expect(consoleStub).to.have.been.calledWith(responseHeader(header))
expect(consoleStub).to.have.been.calledWithMatch(responseMockAcumulado.resultadoOrdenado.replace(/-/g, ' '))
expect(consoleStub).to.have.been.calledWith(responseAwardsWithoutWinner)
expect(consoleStub).to.have.been.calledWith(responseFooter(footer))
})
})

0 comments on commit 57070ff

Please sign in to comment.