Permalink
Browse files

Extract classes into their own files

  • Loading branch information...
1 parent be38cad commit 90ffb1e9b1ffb1c1a5f72690decc2e8ca6fe5dbe @tombell tombell committed May 26, 2012
View
@@ -71,7 +71,7 @@ unless process.platform is "win32"
process.exit 0
if Options.create
- creator = new Creator.Creator(Options.path)
+ creator = new Creator(Options.path)
creator.run()
else
View
@@ -1,11 +1,12 @@
-EventEmitter = require('events').EventEmitter
+{EventEmitter} = require 'events'
+HttpClient = require 'scoped-http-client'
class Adapter extends EventEmitter
# An adapter is a specific interface to a chat source for robots.
#
# robot - A Robot instance.
constructor: (@robot) ->
- @httpClient = require 'scoped-http-client'
+ @httpClient = HttpClient
# Public: Raw method for sending data back to the chat source. Extend this.
#
@@ -38,8 +39,6 @@ class Adapter extends EventEmitter
# Public: Dispatch a received message to the robot.
#
# message - A TextMessage instance of the received message.
- #
- # Returns nothing.
receive: (message) ->
@robot.receive message
@@ -58,14 +57,12 @@ class Adapter extends EventEmitter
# Public: Get all users whose names match fuzzyName. Currently, match
# means 'starts with', but this could be extended to match initials,
# nicknames, etc.
- #
usersForRawFuzzyName: (fuzzyName) ->
@robot.usersForRawFuzzyName fuzzyName
# Public: If fuzzyName is an exact match for a user, returns an array with
# just that user. Otherwise, returns an array of all users for which
# fuzzyName is a raw fuzzy match (see usersForRawFuzzyName).
- #
usersForFuzzyName: (fuzzyName) ->
@robot.usersForFuzzyName fuzzyName
@@ -1,11 +1,11 @@
-Robot = require '../robot'
-Adapter = require '../adapter'
+HTTPS = require 'https'
+{EventEmitter} = require 'events'
-HTTPS = require 'https'
-EventEmitter = require('events').EventEmitter
+Robot = require '../robot'
+Adapter = require '../adapter'
+{TextMessage,EnterMessage,LeaveMessage} = require '../message'
class Campfire extends Adapter
-
send: (user, strings...) ->
if strings.length > 0
@bot.Room(user.room).speak strings.shift(), (err, data) =>
@@ -40,15 +40,15 @@ class Campfire extends Adapter
bot.on "TextMessage", withAuthor (id, created, room, user, body, author) ->
unless bot.info.id == author.id
- self.receive new Robot.TextMessage(author, body)
+ self.receive new TextMessage(author, body)
bot.on "EnterMessage", withAuthor (id, created, room, user, body, author) ->
unless bot.info.id == author.id
- self.receive new Robot.EnterMessage(author)
+ self.receive new EnterMessage(author)
bot.on "LeaveMessage", withAuthor (id, created, room, user, body, author) ->
unless bot.info.id == author.id
- self.receive new Robot.LeaveMessage(author)
+ self.receive new LeaveMessage(author)
bot.Me (err, data) ->
bot.info = data.user
@@ -1,11 +1,12 @@
Readline = require 'readline'
-Robot = require '../robot'
-Adapter = require '../adapter'
+Robot = require '../robot'
+Adapter = require '../adapter'
+{TextMessage} = require '../message'
class Shell extends Adapter
send: (user, strings...) ->
- unless process.platform is "win32"
+ unless process.platform is 'win32'
console.log "\x1b[01;32m#{str}\x1b[0m" for str in strings
else
console.log "#{str}" for str in strings
@@ -19,23 +20,24 @@ class Shell extends Adapter
stdin = process.openStdin()
stdout = process.stdout
- process.on "uncaughtException", (err) =>
+ process.on 'uncaughtException', (err) =>
@robot.logger.error "#{err}"
@repl = Readline.createInterface stdin, stdout, null
- @repl.on "close", =>
+ @repl.on 'close', =>
stdin.destroy()
@robot.shutdown()
process.exit 0
- @repl.on "line", (buffer) =>
- @repl.close() if buffer.toLowerCase() is "exit"
+ @repl.on 'line', (buffer) =>
+ @repl.close() if buffer.toLowerCase() is 'exit'
@repl.prompt()
- user = @userForId '1', name: "Shell", room: "Shell"
- @receive new Robot.TextMessage user, buffer
+ user = @userForId '1', name: 'Shell', room: 'Shell'
+ @receive new TextMessage user, buffer
+
+ self.emit 'connected'
- self.emit "connected"
@repl.setPrompt "#{@robot.name}> "
@repl.prompt()
View
@@ -1,4 +1,4 @@
-EventEmitter = require('events').EventEmitter
+{EventEmitter} = require 'events'
# http://www.the-isb.com/images/Nextwave-Aaron01.jpg
class Brain extends EventEmitter
View
@@ -63,4 +63,4 @@ class Creator
@copy "#{@templateDir}/#{file}", "#{@path}/#{file}" for file in files
-exports.Creator = Creator
+module.exports = Creator
View
@@ -0,0 +1,41 @@
+{TextMessage} = require './message'
+
+class Listener
+ # Listeners receive every message from the chat source and decide if they
+ # want to act on it.
+ #
+ # robot - The current Robot instance.
+ # matcher - The Function that determines if this listener should trigger the
+ # callback.
+ # callback - The Function that is triggered if the incoming message matches.
+ constructor: (@robot, @matcher, @callback) ->
+
+ # Public: Determines if the listener likes the content of the message. If
+ # so, a Response built from the given Message is passed to the Listener
+ # callback.
+ #
+ # message - a Message instance.
+ #
+ # Returns a boolean of whether the matcher matched.
+ call: (message) ->
+ if match = @matcher message
+ @callback new @robot.Response(@robot, message, match)
+ true
+ else
+ false
+
+class TextListener extends Listener
+ # TextListeners receive every message from the chat source and decide if they want
+ # to act on it.
+ #
+ # robot - The current Robot instance.
+ # regex - The Regex that determines if this listener should trigger the
+ # callback.
+ # callback - The Function that is triggered if the incoming message matches.
+ constructor: (@robot, @regex, @callback) ->
+ @matcher = (message) =>
+ if message instanceof TextMessage
+ message.match @regex
+
+module.exports.Listener = Listener
+module.exports.TextListener = TextListener
View
@@ -0,0 +1,47 @@
+class Message
+ # Represents an incoming message from the chat.
+ #
+ # user - A User instance that sent the message.
+ constructor: (@user, @done = false) ->
+
+ # Indicates that no other Listener should be called on this object
+ finish: ->
+ @done = true
+
+class TextMessage extends Message
+ # Represents an incoming message from the chat.
+ #
+ # user - A User instance that sent the message.
+ # text - The String message contents.
+ constructor: (@user, @text) ->
+ super @user
+
+ # Determines if the message matches the given regex.
+ #
+ # regex - The Regex to check.
+ #
+ # Returns a Match object or null.
+ match: (regex) ->
+ @text.match regex
+
+# Represents an incoming user entrance notification.
+#
+# user - A User instance for the user who entered.
+class EnterMessage extends Message
+
+# Represents an incoming user exit notification.
+#
+# user - A User instance for the user who left.
+class LeaveMessage extends Message
+
+class CatchAllMessage extends Message
+ # Represents a message that no matchers matched.
+ #
+ # message - The original message.
+ constructor: (@message) ->
+
+module.exports.Message = Message
+module.exports.TextMessage = TextMessage
+module.exports.EnterMessage = EnterMessage
+module.exports.LeaveMessage = LeaveMessage
+module.exports.CatchAllMessage = CatchAllMessage
View
@@ -0,0 +1,86 @@
+HttpClient = require 'scoped-http-client'
+
+class Response
+ # Public: Responses are sent to matching listeners. Messages know about the
+ # content and user that made the original message, and how to reply back to
+ # them.
+ #
+ # robot - The current Robot instance.
+ # message - The current Message instance.
+ # match - The Match object from the successful Regex match.
+ constructor: (@robot, @message, @match) ->
+ @httpClient = HttpClient
+
+ # Public: Posts a message back to the chat source
+ #
+ # strings - One or more strings to be posted. The order of these strings
+ # should be kept intact.
+ #
+ # Returns nothing.
+ send: (strings...) ->
+ @robot.adapter.send @message.user, strings...
+
+ # Public: Posts a topic changing message
+ #
+ # strings - One or more strings to set as the topic of the
+ # room the bot is in.
+ #
+ # Returns nothing.
+ topic: (strings...) ->
+ @robot.adapter.topic @message.user, strings...
+
+ # Public: Posts a message mentioning the current user.
+ #
+ # strings - One or more strings to be posted. The order of these strings
+ # should be kept intact.
+ #
+ # Returns nothing.
+ reply: (strings...) ->
+ @robot.adapter.reply @message.user, strings...
+
+ # Public: Picks a random item from the given items.
+ #
+ # items - An Array of items (usually Strings).
+ #
+ # Returns an random item.
+ random: (items) ->
+ items[ Math.floor(Math.random() * items.length) ]
+
+ # Public: Tell the message to stop dispatching to listeners
+ #
+ # Returns nothing.
+ finish: ->
+ @message.finish()
+
+ # Public: Creates a scoped http client with chainable methods for
+ # modifying the request. This doesn't actually make a request though.
+ # Once your request is assembled, you can call `get()`/`post()`/etc to
+ # send the request.
+ #
+ # url - String URL to access.
+ #
+ # Examples:
+ #
+ # res.http("http://example.com")
+ # # set a single header
+ # .header('Authorization', 'bearer abcdef')
+ #
+ # # set multiple headers
+ # .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
+ #
+ # # add URI query parameters
+ # .query(a: 1, b: 'foo & bar')
+ #
+ # # make the actual request
+ # .get() (err, res, body) ->
+ # console.log body
+ #
+ # # or, you can POST data
+ # .post(data) (err, res, body) ->
+ # console.log body
+ #
+ # Returns a ScopedClient instance.
+ http: (url) ->
+ @httpClient.create(url)
+
+module.exports = Response
Oops, something went wrong.

0 comments on commit 90ffb1e

Please sign in to comment.