Skip to content
Browse files

script to remember and show who is on vacation

  • Loading branch information...
1 parent 604ebcb commit 1034b4837332cf6c49cc74172ac03607a6e97207 Tej committed Jan 3, 2013
Showing with 153 additions and 0 deletions.
  1. +67 −0 src/scripts/whoisout.coffee
  2. +86 −0 test/scripts/whoisout.spec.coffee
View
67 src/scripts/whoisout.coffee
@@ -0,0 +1,67 @@
+## Description
+# Show / Enter who is out of office
+#
+# Dependencies:
+# "moment": "x"
+# "underscore": "x"
+#
+# Configuration:
+#
+# Commands:
+# hubot I will be out [date]
+# hubot whoisout
+#
+# Notes:
+#
+# Author:
+# Contejious
+
+moment = require 'moment'
+_ = require 'underscore'
+plugin = (robot)->
+ robot.brain.on 'loaded', =>
+ robot.brain.data.outList = [] unless _(robot.brain.data.outList).isArray()
+
+ robot.respond /whoisout/i, (msg)->
+ msg.send (plugin.getAbsentees robot, msg.match[1])
+
+ robot.respond /I will be out +(.*)/i, (msg)->
+ thisDate = plugin.parseDate msg.match[1]?.trim()
+ if thisDate
+ plugin.save robot, thisDate, msg.message
+ msg.send 'ok'
+ else
+ msg.send 'unable to save date'
+
+plugin.parseDate = (fuzzyDateString)->
+ if (@thisDate = (moment fuzzyDateString)).isValid()
+ return {start: @thisDate.toDate(), end: null}
+ else
+ return false
+
+plugin.save = (robot, vacationDateRange, msg)->
+ userOutList = robot.brain.data.outList
+ userVacation = _(userOutList).find (item)-> item.name is msg.user.name
+
+ if userVacation is undefined
+ userOutList.push
+ name: msg.user.name
+ dates: [vacationDateRange.start]
+ else
+ unless _(userVacation.dates).some( (item)-> (moment item).format('M/D/YY') is (moment vacationDateRange.start).format('M/D/YY'))
+ userVacation.dates.push vacationDateRange.start
+
+plugin.getAbsentees = (robot, targetDate)->
+ targetDate = new Date() unless targetDate?
+ if _(robot.brain.data.outList).isArray() and (robot.brain.data.outList.length > 0)
+ names = []
+ _(robot.brain.data.outList).each (item)->
+ if(_(item.dates).some( (dt)-> (moment dt).format('M/D/YY') is (moment targetDate).format('M/D/YY')))
+ names.push item.name
+ if names.length > 0
+ names.join '\n'
+ else
+ return 'Nobody'
+ else
+ return 'Nobody'
+module.exports = plugin
View
86 test/scripts/whoisout.spec.coffee
@@ -0,0 +1,86 @@
+chai = require 'chai'
+expect = chai.expect
+moment = require 'moment'
+
+Spy = require '../lib/spy/spy'
+chai.use require '../lib/spy/spy.chai'
+
+codeUnderTest = require '../scripts/whoisout'
+
+describe 'Parse Date', ->
+ beforeEach ->
+ @robot = new Spy()
+ @robot.brain =
+ data:{outList: []}
+ on: (fn)-> fn
+ @robot.respond = new Spy()
+ codeUnderTest.parseDate = new Spy().andCall codeUnderTest.parseDate
+ codeUnderTest @robot
+
+ describe 'when responding', ->
+ beforeEach ->
+ @robot.respond.mostRecentCall.args[1](
+ match:[
+ '',
+ '12/12/12']
+ send: new Spy 'message send'
+ message:
+ user:
+ name: 'slacker')
+
+ it 'should call parse date', ->
+ parseDateArgs = codeUnderTest.parseDate.mostRecentCall.args
+ (expect codeUnderTest.parseDate).was.called
+ (expect parseDateArgs[0]).to.equal '12/12/12'
+
+ describe 'when querying', ->
+ describe 'when target date is not specified', ->
+ beforeEach ->
+ @robot.brain.data.outList = [
+ {name: 'slacker', dates: [moment().format('M/D/YY')]}
+ { name: 'another slacker', dates: ['1/1/14']}]
+ @nameList = codeUnderTest.getAbsentees @robot
+ it 'should show who is out today', ->
+ (expect @nameList).to.equal 'slacker'
+
+ describe 'when parsing date', ->
+ describe 'when date is parsable by ECMA', ->
+ describe 'it is an invalid date', ->
+ beforeEach ->
+ @thisDate = codeUnderTest.parseDate 'this could never be valid'
+ it 'should return a valid date', ->
+ (expect @thisDate).to.be.false
+ describe 'it is a valid date', ->
+ beforeEach ->
+ @thisDate = codeUnderTest.parseDate '12/12/12'
+ it 'should return a valid date', ->
+ (expect @thisDate.start).to.eql new Date('12/12/12')
+
+ describe 'when saving date', ->
+ describe 'when user\'s entry exists', ->
+ beforeEach ->
+ @robot.brain.data =
+ outList:[
+ name: 'slacker'
+ dates: [new Date('1/1/12')]]
+ codeUnderTest.save @robot, {start:(new Date '12/12/12')}, {user: {name: 'slacker'}}
+
+ it 'should add this date to the user entry', ->
+ (expect @robot.brain.data.outList[0]).to.eql
+ name: 'slacker'
+ dates: [new Date('1/1/12'), new Date('12/12/12')]
+
+ describe 'when user\'s vacation date already exists', ->
+ beforeEach ->
+ codeUnderTest.save @robot, {start:(new Date '12/12/12')}, {user:{name: 'slacker'}}
+ it 'should not add it to the brain', ->
+ (expect @robot.brain.data.outList[0].dates.length).to.equal 2
+
+ describe 'when user\'s entry does not exist', ->
+ beforeEach ->
+ @robot.brain.data =
+ outList: []
+ codeUnderTest.save @robot, {start: (new Date '12/12/12')}, {user: {name: 'slacker'}}
+ it 'should add date to the right user', ->
+ (expect @robot.brain.data.outList[0].dates).to.eql [new Date('12/12/12')]
+ (expect @robot.brain.data.outList[0].name).to.equal 'slacker'

0 comments on commit 1034b48

Please sign in to comment.
Something went wrong with that request. Please try again.