Skip to content

Commit

Permalink
Remove help
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Nov 3, 2015
1 parent 2328edc commit 9cde573
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 53 deletions.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "hubot-cne",
"version": "0.0.1",
"version": "1.0.0",
"description": "A hubot script to get fuel price in Chile from cne api",
"main": "index.coffee",
"scripts": {
"test": "mocha --compilers coffee:coffee-script/register"
},
"engines": {
"node": ">=0.12"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lgaticaq/hubot-cne.git"
Expand All @@ -27,10 +30,10 @@
"cne": "^0.3.1"
},
"devDependencies": {
"chai": "^3.3.0",
"chai": "^3.4.0",
"coffee-script": "^1.10.0",
"hubot-test-helper": "^1.0.0",
"hubot-test-helper": "^1.2.0",
"mocha": "^2.3.3",
"nock": "^2.13.0"
"nock": "^2.17.0"
}
}
51 changes: 26 additions & 25 deletions src/cne.coffee
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
# Description:
# Te dice en donde venden la gasolina mas barata.
# Obtiene la estación de servicio con el precio mas barato de un combustible
#
# Dependencies:
# cne
# "cne": "^0.3.1"
#
# Commands:
# @pudu cne help
# @pudu cne obtener <tipo-de-combustible> <comuna>
# @pudu cne listar combustibles
# @pudu cne listar comunas
# hubot cne obtener <combustible> <comuna> - Obtiene la estación de servicio con el precio mas barato
# hubot cne listar combustibles - Obtiene el listado de combustibles disponibles
# hubot cne listar comunas - Obtiene el listado de comunas disponibles
#
# Author:
# @lgaticaq
# lgaticaq

cne = require "cne"

module.exports = (robot) ->
help = (msg) ->
msg.send "`pudu cne obtener <combustible> en <comuna>`"
msg.send "`pudu cne listar combustibles`"
msg.send "`pudu cne listar comunas`"
getFuelTypes = (res) ->
res.send "```#{cne.fuelTypes.join(", ")}```"

getFuelTypes = (msg) ->
msg.send "```#{cne.fuelTypes.join(", ")}```"
getCommunes = (res) ->
res.send "```#{cne.communes.join(", ")}```"

getCommunes = (msg) ->
msg.send "```#{cne.communes.join(", ")}```"

robot.respond /cne help/i, help
robot.respond /cne listar combustibles/i, getFuelTypes
robot.respond /cne listar comunas/i, getCommunes

robot.respond /cne obtener (\w+) en ([\w\sñáéíóúñÁÉÍÓÚÑ]+)/i, (msg) ->
fuelType = msg.match[1].trim()
robot.respond /cne obtener (\w+) en ([\w\sñáéíóúñÁÉÍÓÚÑ]+)/i, (res) ->
fuelType = res.match[1].trim()
commune = res.match[2].trim()
communes = (x.toLowerCase() for x in cne.communes)

if fuelType not in cne.fuelTypes
msg.send "En el servicentro no tenemos #{fuelType} ni tampoco criptonita"
msg.send "Estos son los tipos de combustibles:"
msg.send "```#{fuelTypes.join(", ")}```"
res.send "El combustible *#{fuelType}* no esta disponible"
res.send "Estos son los combustibles disponibles:"
res.send "```#{cne.fuelTypes.join(", ")}```"
else if commune.toLowerCase() not in communes
res.send "La comuna *#{commune}* no esta disponible"
res.send "Estos son las comunas disponibles:"
res.send "```#{cne.communes.join(", ")}```"
else
options =
fuelType: fuelType
options.commune = msg.match[2].trim()
options.commune = commune
cne.get(options)
.then (data) ->
price = data.precio_por_combustible[fuelType]
Expand All @@ -49,6 +49,7 @@ module.exports = (robot) ->
commune = data.nombre_comuna
dist = data.nombre_distribuidor
addr = "#{street} #{number} #{commune}"
msg.send "En #{dist} de #{addr} la venden a $#{price} CLP el litro"
res.send "En #{dist} de #{addr} la venden a $#{price} CLP el litro"
.fail (err) ->
msg.send "No hay servicentro en #{options.commune}"
res.reply "ocurrio un error al consultar el combustible"
robot.emit "error", err, res
87 changes: 63 additions & 24 deletions test/cne_test.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Helper = require("hubot-test-helper")
expect = require("chai").expect
nock = require("nock")
cne = require("cne")

helper = new Helper("./../src/cne.coffee")

Expand All @@ -13,33 +14,31 @@ describe "cne", ->
dist = "SHELL"
addr = "#{street} #{number} #{commune}"
fuelType = "gasolina_93"
invalidFuelType = "gasolina_69"
valid = "santiago"
invalid = "chuchunco norte"
data = {
estado: "OK",
data: [
{
id: " te912002",
fecha: "2015-08-31 10:23:16",
direccion_calle: street,
direccion_numero: number,
nombre_comuna: commune,
nombre_distribuidor: dist,
precio_por_combustible: {
"gasolina_93": price,
"petroleo_diesel": 548,
"gasolina_95": 842
}
}
]
}

beforeEach ->
room = helper.createRoom()
data = {
estado: "OK",
data: [
{
id: " te912002",
fecha: "2015-08-31 10:23:16",
direccion_calle: street,
direccion_numero: number,
nombre_comuna: commune,
nombre_distribuidor: dist,
precio_por_combustible: {
"gasolina_93": price,
"petroleo_diesel": 548,
"gasolina_95": 842
}
}
]
}
do nock.disableNetConnect
nock("http://api.cne.cl")
.get("/api/listaInformacion/6M5jaVAzPS")
.reply(200, data)
nock.disableNetConnect()

afterEach ->
room.destroy()
Expand All @@ -48,6 +47,9 @@ describe "cne", ->
context "ask for a valid commune", ->

beforeEach (done) ->
nock("http://api.cne.cl")
.get("/api/listaInformacion/6M5jaVAzPS")
.reply(200, data)
room.user.say("alice", "hubot cne obtener #{fuelType} en #{valid}")
setTimeout(done, 100)

Expand All @@ -60,11 +62,48 @@ describe "cne", ->
context "ask for a invalid commune", ->

beforeEach (done) ->
nock("http://api.cne.cl")
.get("/api/listaInformacion/6M5jaVAzPS")
.reply(200, data)
room.user.say("alice", "hubot cne obtener #{fuelType} en #{invalid}")
setTimeout(done, 100)

it "should respond a warning", ->
it "should respond a instructions for valid commune", ->
expect(room.messages).to.eql([
["alice", "hubot cne obtener #{fuelType} en #{invalid}"]
["hubot", "No hay servicentro en chuchunco norte"]
["hubot", "La comuna *#{invalid}* no esta disponible"]
["hubot", "Estos son las comunas disponibles:"]
["hubot", "```#{cne.communes.join(", ")}```"]
])

context "ask for a invalid fuel type", ->

beforeEach (done) ->
nock("http://api.cne.cl")
.get("/api/listaInformacion/6M5jaVAzPS")
.reply(200, data)
room.user.say("alice", "hubot cne obtener #{invalidFuelType} en #{valid}")
setTimeout(done, 100)

it "should respond a instructions for valid fuel type", ->
expect(room.messages).to.eql([
["alice", "hubot cne obtener #{invalidFuelType} en #{valid}"]
["hubot", "El combustible *#{invalidFuelType}* no esta disponible"]
["hubot", "Estos son los combustibles disponibles:"]
["hubot", "```#{cne.fuelTypes.join(", ")}```"]
])

context "server error", ->

beforeEach (done) ->
nock("http://api.cne.cl")
.get("/api/listaInformacion/6M5jaVAzPS")
.replyWithError("something awful happened")
room.user.say("alice", "hubot cne obtener #{fuelType} en #{valid}")
setTimeout(done, 100)

it "should respond a error", ->
expect(room.messages).to.eql([
["alice", "hubot cne obtener #{fuelType} en #{valid}"]
["hubot", "@alice ocurrio un error al consultar el combustible"]
])

0 comments on commit 9cde573

Please sign in to comment.