Last active
August 29, 2015 14:02
-
-
Save bayleedev/a936c308b20b0c26376f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hubot | |
listen_for: {} | |
# @script_user | |
respond: (regex, callback) -> | |
@listen_for[regex] = callback | |
# @test_user | |
# @return bool Phrases matches one of the respond to callbacks. | |
responds_to: (phrase) -> | |
for regex of @listen_for | |
return true if phrase.match(regex) | |
false | |
# @test_user | |
# @return array Items that the robot passes to the callback | |
responds_with: (phrase) -> | |
for regex,callback of @listen_for | |
return ret if ret = phrase.match(regex) | |
false | |
# @test_user | |
# Calls the method based on the input phrase | |
send: (phrase) -> | |
for regex,callback of @listen_for | |
if matches = phrase.match(regex) | |
return callback(create_message(regex)) | |
false | |
# @test_user | |
# Creates the "message" that it gives to the callback | |
# In a method so it can be stubbed via Sinon or other | |
# stubbing/mocking/spying library. | |
create_message: (matches) -> | |
new Message(matches) | |
class Message | |
@match = {} | |
constructor: (@match) -> | |
send: -> | |
console.log.apply(console, arguments) | |
module.exports = Hubot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment