Skip to content
This repository
Browse code

Extract classes into their own files

  • Loading branch information...
commit 90ffb1e9b1ffb1c1a5f72690decc2e8ca6fe5dbe 1 parent be38cad
Tom Bell authored May 26, 2012
2  bin/hubot
@@ -71,7 +71,7 @@ unless process.platform is "win32"
71 71
     process.exit 0
72 72
 
73 73
 if Options.create
74  
-  creator = new Creator.Creator(Options.path)
  74
+  creator = new Creator(Options.path)
75 75
   creator.run()
76 76
 
77 77
 else
9  src/adapter.coffee
... ...
@@ -1,11 +1,12 @@
1  
-EventEmitter = require('events').EventEmitter
  1
+{EventEmitter} = require 'events'
  2
+HttpClient     = require 'scoped-http-client'
2 3
 
3 4
 class Adapter extends EventEmitter
4 5
   # An adapter is a specific interface to a chat source for robots.
5 6
   #
6 7
   # robot - A Robot instance.
7 8
   constructor: (@robot) ->
8  
-    @httpClient = require 'scoped-http-client'
  9
+    @httpClient = HttpClient
9 10
 
10 11
   # Public: Raw method for sending data back to the chat source.  Extend this.
11 12
   #
@@ -38,8 +39,6 @@ class Adapter extends EventEmitter
38 39
   # Public: Dispatch a received message to the robot.
39 40
   #
40 41
   # message - A TextMessage instance of the received message.
41  
-  #
42  
-  # Returns nothing.
43 42
   receive: (message) ->
44 43
     @robot.receive message
45 44
 
@@ -58,14 +57,12 @@ class Adapter extends EventEmitter
58 57
   # Public: Get all users whose names match fuzzyName. Currently, match
59 58
   # means 'starts with', but this could be extended to match initials,
60 59
   # nicknames, etc.
61  
-  #
62 60
   usersForRawFuzzyName: (fuzzyName) ->
63 61
     @robot.usersForRawFuzzyName fuzzyName
64 62
 
65 63
   # Public: If fuzzyName is an exact match for a user, returns an array with
66 64
   # just that user. Otherwise, returns an array of all users for which
67 65
   # fuzzyName is a raw fuzzy match (see usersForRawFuzzyName).
68  
-  #
69 66
   usersForFuzzyName: (fuzzyName) ->
70 67
     @robot.usersForFuzzyName fuzzyName
71 68
 
16  src/adapters/campfire.coffee
... ...
@@ -1,11 +1,11 @@
1  
-Robot        = require '../robot'
2  
-Adapter      = require '../adapter'
  1
+HTTPS          = require 'https'
  2
+{EventEmitter} = require 'events'
3 3
 
4  
-HTTPS        = require 'https'
5  
-EventEmitter = require('events').EventEmitter
  4
+Robot                                   = require '../robot'
  5
+Adapter                                 = require '../adapter'
  6
+{TextMessage,EnterMessage,LeaveMessage} = require '../message'
6 7
 
7 8
 class Campfire extends Adapter
8  
-
9 9
   send: (user, strings...) ->
10 10
     if strings.length > 0
11 11
       @bot.Room(user.room).speak strings.shift(), (err, data) =>
@@ -40,15 +40,15 @@ class Campfire extends Adapter
40 40
 
41 41
     bot.on "TextMessage", withAuthor (id, created, room, user, body, author) ->
42 42
       unless bot.info.id == author.id
43  
-        self.receive new Robot.TextMessage(author, body)
  43
+        self.receive new TextMessage(author, body)
44 44
 
45 45
     bot.on "EnterMessage", withAuthor (id, created, room, user, body, author) ->
46 46
       unless bot.info.id == author.id
47  
-        self.receive new Robot.EnterMessage(author)
  47
+        self.receive new EnterMessage(author)
48 48
 
49 49
     bot.on "LeaveMessage", withAuthor (id, created, room, user, body, author) ->
50 50
       unless bot.info.id == author.id
51  
-        self.receive new Robot.LeaveMessage(author)
  51
+        self.receive new LeaveMessage(author)
52 52
 
53 53
     bot.Me (err, data) ->
54 54
       bot.info = data.user
22  src/adapters/shell.coffee
... ...
@@ -1,11 +1,12 @@
1 1
 Readline = require 'readline'
2 2
 
3  
-Robot    = require '../robot'
4  
-Adapter  = require '../adapter'
  3
+Robot         = require '../robot'
  4
+Adapter       = require '../adapter'
  5
+{TextMessage} = require '../message'
5 6
 
6 7
 class Shell extends Adapter
7 8
   send: (user, strings...) ->
8  
-    unless process.platform is "win32"
  9
+    unless process.platform is 'win32'
9 10
       console.log "\x1b[01;32m#{str}\x1b[0m" for str in strings
10 11
     else
11 12
       console.log "#{str}" for str in strings
@@ -19,23 +20,24 @@ class Shell extends Adapter
19 20
     stdin = process.openStdin()
20 21
     stdout = process.stdout
21 22
 
22  
-    process.on "uncaughtException", (err) =>
  23
+    process.on 'uncaughtException', (err) =>
23 24
       @robot.logger.error "#{err}"
24 25
 
25 26
     @repl = Readline.createInterface stdin, stdout, null
26 27
 
27  
-    @repl.on "close", =>
  28
+    @repl.on 'close', =>
28 29
       stdin.destroy()
29 30
       @robot.shutdown()
30 31
       process.exit 0
31 32
 
32  
-    @repl.on "line", (buffer) =>
33  
-      @repl.close() if buffer.toLowerCase() is "exit"
  33
+    @repl.on 'line', (buffer) =>
  34
+      @repl.close() if buffer.toLowerCase() is 'exit'
34 35
       @repl.prompt()
35  
-      user = @userForId '1', name: "Shell", room: "Shell"
36  
-      @receive new Robot.TextMessage user, buffer
  36
+      user = @userForId '1', name: 'Shell', room: 'Shell'
  37
+      @receive new TextMessage user, buffer
  38
+
  39
+    self.emit 'connected'
37 40
 
38  
-    self.emit "connected"
39 41
     @repl.setPrompt "#{@robot.name}> "
40 42
     @repl.prompt()
41 43
 
2  src/brain.coffee
... ...
@@ -1,4 +1,4 @@
1  
-EventEmitter = require('events').EventEmitter
  1
+{EventEmitter} = require 'events'
2 2
 
3 3
 # http://www.the-isb.com/images/Nextwave-Aaron01.jpg
4 4
 class Brain extends EventEmitter
2  src/creator.coffee
@@ -63,4 +63,4 @@ class Creator
63 63
 
64 64
     @copy "#{@templateDir}/#{file}", "#{@path}/#{file}" for file in files
65 65
 
66  
-exports.Creator = Creator
  66
+module.exports = Creator
41  src/listener.coffee
... ...
@@ -0,0 +1,41 @@
  1
+{TextMessage} = require './message'
  2
+
  3
+class Listener
  4
+  # Listeners receive every message from the chat source and decide if they
  5
+  # want to act on it.
  6
+  #
  7
+  # robot    - The current Robot instance.
  8
+  # matcher  - The Function that determines if this listener should trigger the
  9
+  #            callback.
  10
+  # callback - The Function that is triggered if the incoming message matches.
  11
+  constructor: (@robot, @matcher, @callback) ->
  12
+
  13
+  # Public: Determines if the listener likes the content of the message.  If
  14
+  # so, a Response built from the given Message is passed to the Listener
  15
+  # callback.
  16
+  #
  17
+  # message - a Message instance.
  18
+  #
  19
+  # Returns a boolean of whether the matcher matched.
  20
+  call: (message) ->
  21
+    if match = @matcher message
  22
+      @callback new @robot.Response(@robot, message, match)
  23
+      true
  24
+    else
  25
+      false
  26
+
  27
+class TextListener extends Listener
  28
+  # TextListeners receive every message from the chat source and decide if they want
  29
+  # to act on it.
  30
+  #
  31
+  # robot    - The current Robot instance.
  32
+  # regex    - The Regex that determines if this listener should trigger the
  33
+  #            callback.
  34
+  # callback - The Function that is triggered if the incoming message matches.
  35
+  constructor: (@robot, @regex, @callback) ->
  36
+    @matcher = (message) =>
  37
+      if message instanceof TextMessage
  38
+        message.match @regex
  39
+
  40
+module.exports.Listener     = Listener
  41
+module.exports.TextListener = TextListener
47  src/message.coffee
... ...
@@ -0,0 +1,47 @@
  1
+class Message
  2
+  # Represents an incoming message from the chat.
  3
+  #
  4
+  # user - A User instance that sent the message.
  5
+  constructor: (@user, @done = false) ->
  6
+
  7
+  # Indicates that no other Listener should be called on this object
  8
+  finish: ->
  9
+    @done = true
  10
+
  11
+class TextMessage extends Message
  12
+  # Represents an incoming message from the chat.
  13
+  #
  14
+  # user - A User instance that sent the message.
  15
+  # text - The String message contents.
  16
+  constructor: (@user, @text) ->
  17
+    super @user
  18
+
  19
+  # Determines if the message matches the given regex.
  20
+  #
  21
+  # regex - The Regex to check.
  22
+  #
  23
+  # Returns a Match object or null.
  24
+  match: (regex) ->
  25
+    @text.match regex
  26
+
  27
+# Represents an incoming user entrance notification.
  28
+#
  29
+# user - A User instance for the user who entered.
  30
+class EnterMessage extends Message
  31
+
  32
+# Represents an incoming user exit notification.
  33
+#
  34
+# user - A User instance for the user who left.
  35
+class LeaveMessage extends Message
  36
+
  37
+class CatchAllMessage extends Message
  38
+  # Represents a message that no matchers matched.
  39
+  #
  40
+  # message - The original message.
  41
+  constructor: (@message) ->
  42
+
  43
+module.exports.Message         = Message
  44
+module.exports.TextMessage     = TextMessage
  45
+module.exports.EnterMessage    = EnterMessage
  46
+module.exports.LeaveMessage    = LeaveMessage
  47
+module.exports.CatchAllMessage = CatchAllMessage
86  src/response.coffee
... ...
@@ -0,0 +1,86 @@
  1
+HttpClient = require 'scoped-http-client'
  2
+
  3
+class Response
  4
+  # Public: Responses are sent to matching listeners.  Messages know about the
  5
+  # content and user that made the original message, and how to reply back to
  6
+  # them.
  7
+  #
  8
+  # robot   - The current Robot instance.
  9
+  # message - The current Message instance.
  10
+  # match   - The Match object from the successful Regex match.
  11
+  constructor: (@robot, @message, @match) ->
  12
+    @httpClient = HttpClient
  13
+
  14
+  # Public: Posts a message back to the chat source
  15
+  #
  16
+  # strings - One or more strings to be posted.  The order of these strings
  17
+  #           should be kept intact.
  18
+  #
  19
+  # Returns nothing.
  20
+  send: (strings...) ->
  21
+    @robot.adapter.send @message.user, strings...
  22
+
  23
+  # Public: Posts a topic changing message
  24
+  #
  25
+  # strings - One or more strings to set as the topic of the
  26
+  #           room the bot is in.
  27
+  #
  28
+  # Returns nothing.
  29
+  topic: (strings...) ->
  30
+    @robot.adapter.topic @message.user, strings...
  31
+
  32
+  # Public: Posts a message mentioning the current user.
  33
+  #
  34
+  # strings - One or more strings to be posted.  The order of these strings
  35
+  #           should be kept intact.
  36
+  #
  37
+  # Returns nothing.
  38
+  reply: (strings...) ->
  39
+    @robot.adapter.reply @message.user, strings...
  40
+
  41
+  # Public: Picks a random item from the given items.
  42
+  #
  43
+  # items - An Array of items (usually Strings).
  44
+  #
  45
+  # Returns an random item.
  46
+  random: (items) ->
  47
+    items[ Math.floor(Math.random() * items.length) ]
  48
+
  49
+  # Public: Tell the message to stop dispatching to listeners
  50
+  #
  51
+  # Returns nothing.
  52
+  finish: ->
  53
+    @message.finish()
  54
+
  55
+  # Public: Creates a scoped http client with chainable methods for
  56
+  # modifying the request.  This doesn't actually make a request though.
  57
+  # Once your request is assembled, you can call `get()`/`post()`/etc to
  58
+  # send the request.
  59
+  #
  60
+  # url - String URL to access.
  61
+  #
  62
+  # Examples:
  63
+  #
  64
+  #     res.http("http://example.com")
  65
+  #       # set a single header
  66
+  #       .header('Authorization', 'bearer abcdef')
  67
+  #
  68
+  #       # set multiple headers
  69
+  #       .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
  70
+  #
  71
+  #       # add URI query parameters
  72
+  #       .query(a: 1, b: 'foo & bar')
  73
+  #
  74
+  #       # make the actual request
  75
+  #       .get() (err, res, body) ->
  76
+  #         console.log body
  77
+  #
  78
+  #       # or, you can POST data
  79
+  #       .post(data) (err, res, body) ->
  80
+  #         console.log body
  81
+  #
  82
+  # Returns a ScopedClient instance.
  83
+  http: (url) ->
  84
+    @httpClient.create(url)
  85
+
  86
+module.exports = Response
210  src/robot.coffee
... ...
@@ -1,31 +1,32 @@
1 1
 Fs         = require 'fs'
2  
-Url        = require 'url'
3 2
 Log        = require 'log'
4 3
 Path       = require 'path'
5  
-Connect    = require 'connect'
6 4
 HttpClient = require 'scoped-http-client'
7 5
 
8  
-User       = require './user'
9  
-Brain      = require './brain'
  6
+User                                                    = require './user'
  7
+Brain                                                   = require './brain'
  8
+Response                                                = require './response'
  9
+{Listener,TextListener}                                 = require './listener'
  10
+{TextMessage,EnterMessage,LeaveMessage,CatchAllMessage} = require './message'
10 11
 
11  
-HUBOT_DEFAULT_ADAPTERS = [ "campfire", "shell" ]
  12
+HUBOT_DEFAULT_ADAPTERS = [ 'campfire', 'shell' ]
12 13
 
13 14
 class Robot
14 15
   # Robots receive messages from a chat source (Campfire, irc, etc), and
15 16
   # dispatch them to matching listeners.
16 17
   #
17 18
   # path - String directory full of Hubot scripts to load.
18  
-  constructor: (adapterPath, adapter, httpd, name = "Hubot") ->
  19
+  constructor: (adapterPath, adapter, httpd, name = 'Hubot') ->
19 20
     @name        = name
20 21
     @brain       = new Brain
21 22
     @alias       = false
22 23
     @adapter     = null
23 24
     @commands    = []
24  
-    @Response    = Robot.Response
  25
+    @Response    = Response
25 26
     @listeners   = []
26 27
     @loadPaths   = []
27 28
     @enableSlash = false
28  
-    @logger      = new Log process.env.HUBOT_LOG_LEVEL or "info"
  29
+    @logger      = new Log process.env.HUBOT_LOG_LEVEL or 'info'
29 30
 
30 31
     @parseVersion()
31 32
     @setupConnect() if httpd
@@ -59,18 +60,18 @@ class Robot
59 60
   #
60 61
   # Returns nothing.
61 62
   respond: (regex, callback) ->
62  
-    re = regex.toString().split("/")
  63
+    re = regex.toString().split('/')
63 64
     re.shift()           # remove empty first item
64 65
     modifiers = re.pop() # pop off modifiers
65 66
 
66  
-    if re[0] and re[0][0] is "^"
  67
+    if re[0] and re[0][0] is '^'
67 68
       @logger.warning "Anchors don't work well with respond, perhaps you want to use 'hear'"
68 69
       @logger.warning "The regex in question was #{regex.toString()}"
69 70
 
70  
-    pattern = re.join("/") # combine the pattern back again
  71
+    pattern = re.join('/') # combine the pattern back again
71 72
 
72 73
     if @alias
73  
-      alias = @alias.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") # escape alias for regexp
  74
+      alias = @alias.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') # escape alias for regexp
74 75
       newRegex = new RegExp("^(?:#{alias}[:,]?|#{@name}[:,]?)\\s*(?:#{pattern})", modifiers)
75 76
     else
76 77
       newRegex = new RegExp("^#{@name}[:,]?\\s*(?:#{pattern})", modifiers)
@@ -84,7 +85,7 @@ class Robot
84 85
   #
85 86
   # Returns nothing.
86 87
   enter: (callback) ->
87  
-    @listeners.push new Listener(@, ((msg) -> msg instanceof Robot.EnterMessage), callback)
  88
+    @listeners.push new Listener(@, ((msg) -> msg instanceof EnterMessage), callback)
88 89
 
89 90
   # Public: Adds a Listener that triggers when anyone leaves the room.
90 91
   #
@@ -92,7 +93,7 @@ class Robot
92 93
   #
93 94
   # Returns nothing.
94 95
   leave: (callback) ->
95  
-    @listeners.push new Listener(@, ((msg) -> msg instanceof Robot.LeaveMessage), callback)
  96
+    @listeners.push new Listener(@, ((msg) -> msg instanceof LeaveMessage), callback)
96 97
 
97 98
 
98 99
   # Public: Adds a Listener that triggers when no other text matchers match.
@@ -101,11 +102,11 @@ class Robot
101 102
   #
102 103
   # Returns nothing.
103 104
   catchAll: (callback) ->
104  
-    @listeners.push new Listener(@, ((msg) -> msg instanceof Robot.CatchAllMessage), ((msg) -> msg.message = msg.message.message; callback msg))
  105
+    @listeners.push new Listener(@, ((msg) -> msg instanceof CatchAllMessage), ((msg) -> msg.message = msg.message.message; callback msg))
105 106
 
106 107
   # Public: Passes the given message to any interested Listeners.
107 108
   #
108  
-  # message - A Robot.Message instance. Listeners can flag this message as
  109
+  # message - A Message instance. Listeners can flag this message as
109 110
   #  'done' to prevent further execution
110 111
   #
111 112
   # Returns nothing.
@@ -118,8 +119,8 @@ class Robot
118 119
       catch error
119 120
         @logger.error "Unable to call the listener: #{error}\n#{error.stack}"
120 121
         false
121  
-    if message not instanceof Robot.CatchAllMessage and results.indexOf(true) is -1
122  
-      @receive new Robot.CatchAllMessage(message)
  122
+    if message not instanceof CatchAllMessage and results.indexOf(true) is -1
  123
+      @receive new CatchAllMessage(message)
123 124
 
124 125
 
125 126
   # Public: Loads every script in the given path.
@@ -166,6 +167,7 @@ class Robot
166 167
     user = process.env.CONNECT_USER
167 168
     pass = process.env.CONNECT_PASSWORD
168 169
 
  170
+    Connect        = require 'connect'
169 171
     Connect.router = require 'connect_router'
170 172
 
171 173
     @connect = Connect()
@@ -200,7 +202,7 @@ class Robot
200 202
       setInterval =>
201 203
         HttpClient.create("#{herokuUrl}hubot/ping")
202 204
           .post() (err, res, body) =>
203  
-            @logger.info "keep alive ping!"
  205
+            @logger.info 'keep alive ping!'
204 206
       , 1200000
205 207
 
206 208
   # Load the adapter Hubot is going to use.
@@ -210,7 +212,7 @@ class Robot
210 212
   #
211 213
   # Returns nothing.
212 214
   loadAdapter: (path, adapter) ->
213  
-    @logger.debug "Loading adapter #{adapter}"
  215
+    @logger.debug 'Loading adapter #{adapter}'
214 216
 
215 217
     try
216 218
       path = if adapter in HUBOT_DEFAULT_ADAPTERS
@@ -234,7 +236,7 @@ class Robot
234 236
   #
235 237
   # Returns nothing
236 238
   parseHelp: (path) ->
237  
-    Fs.readFile path, "utf-8", (err, body) =>
  239
+    Fs.readFile path, 'utf-8', (err, body) =>
238 240
       throw err if err?
239 241
       for i, line of body.split("\n")
240 242
         break    if !(line[0] == '#' or line.substr(0, 2) == '//')
@@ -333,174 +335,10 @@ class Robot
333 335
   #
334 336
   # Returns: SemVer compliant version number
335 337
   parseVersion: ->
336  
-    package_path = __dirname + "/../package.json"
337  
-
  338
+    package_path = __dirname + '/../package.json'
338 339
     data = Fs.readFileSync package_path, 'utf8'
339  
-
340 340
     content = JSON.parse data
341 341
     @version = content.version
342 342
 
343  
-class Robot.Message
344  
-  # Represents an incoming message from the chat.
345  
-  #
346  
-  # user - A User instance that sent the message.
347  
-  constructor: (@user, @done = false) ->
348  
-
349  
-  # Indicates that no other Listener should be called on this object
350  
-  finish: ->
351  
-    @done = true
352  
-
353  
-class Robot.TextMessage extends Robot.Message
354  
-  # Represents an incoming message from the chat.
355  
-  #
356  
-  # user - A User instance that sent the message.
357  
-  # text - The String message contents.
358  
-  constructor: (@user, @text) ->
359  
-    super @user
360  
-
361  
-  # Determines if the message matches the given regex.
362  
-  #
363  
-  # regex - The Regex to check.
364  
-  #
365  
-  # Returns a Match object or null.
366  
-  match: (regex) ->
367  
-    @text.match regex
368  
-
369  
-# Represents an incoming user entrance notification.
370  
-#
371  
-# user - A User instance for the user who entered.
372  
-class Robot.EnterMessage extends Robot.Message
373  
-
374  
-# Represents an incoming user exit notification.
375  
-#
376  
-# user - A User instance for the user who left.
377  
-class Robot.LeaveMessage extends Robot.Message
378  
-
379  
-class Robot.CatchAllMessage extends Robot.Message
380  
-  # Represents a message that no matchers matched.
381  
-  #
382  
-  # message - The original message.
383  
-  constructor: (@message) ->
384  
-
385  
-class Listener
386  
-  # Listeners receive every message from the chat source and decide if they
387  
-  # want to act on it.
388  
-  #
389  
-  # robot    - The current Robot instance.
390  
-  # matcher  - The Function that determines if this listener should trigger the
391  
-  #            callback.
392  
-  # callback - The Function that is triggered if the incoming message matches.
393  
-  constructor: (@robot, @matcher, @callback) ->
394  
-
395  
-  # Public: Determines if the listener likes the content of the message.  If
396  
-  # so, a Response built from the given Message is passed to the Listener
397  
-  # callback.
398  
-  #
399  
-  # message - a Robot.Message instance.
400  
-  #
401  
-  # Returns a boolean of whether the matcher matched.
402  
-  call: (message) ->
403  
-    if match = @matcher message
404  
-      @callback new @robot.Response(@robot, message, match)
405  
-      true
406  
-    else
407  
-      false
408  
-
409  
-class TextListener extends Listener
410  
-  # TextListeners receive every message from the chat source and decide if they want
411  
-  # to act on it.
412  
-  #
413  
-  # robot    - The current Robot instance.
414  
-  # regex    - The Regex that determines if this listener should trigger the
415  
-  #            callback.
416  
-  # callback - The Function that is triggered if the incoming message matches.
417  
-  constructor: (@robot, @regex, @callback) ->
418  
-    @matcher = (message) =>
419  
-      if message instanceof Robot.TextMessage
420  
-        message.match @regex
421  
-
422  
-class Robot.Response
423  
-  # Public: Responses are sent to matching listeners.  Messages know about the
424  
-  # content and user that made the original message, and how to reply back to
425  
-  # them.
426  
-  #
427  
-  # robot   - The current Robot instance.
428  
-  # message - The current Robot.Message instance.
429  
-  # match   - The Match object from the successful Regex match.
430  
-  constructor: (@robot, @message, @match) ->
431  
-
432  
-  # Public: Posts a message back to the chat source
433  
-  #
434  
-  # strings - One or more strings to be posted.  The order of these strings
435  
-  #           should be kept intact.
436  
-  #
437  
-  # Returns nothing.
438  
-  send: (strings...) ->
439  
-    @robot.adapter.send @message.user, strings...
440  
-
441  
-  # Public: Posts a topic changing message
442  
-  #
443  
-  # strings - One or more strings to set as the topic of the
444  
-  #           room the bot is in.
445  
-  #
446  
-  # Returns nothing.
447  
-  topic: (strings...) ->
448  
-    @robot.adapter.topic @message.user, strings...
449  
-
450  
-  # Public: Posts a message mentioning the current user.
451  
-  #
452  
-  # strings - One or more strings to be posted.  The order of these strings
453  
-  #           should be kept intact.
454  
-  #
455  
-  # Returns nothing.
456  
-  reply: (strings...) ->
457  
-    @robot.adapter.reply @message.user, strings...
458  
-
459  
-  # Public: Picks a random item from the given items.
460  
-  #
461  
-  # items - An Array of items (usually Strings).
462  
-  #
463  
-  # Returns an random item.
464  
-  random: (items) ->
465  
-    items[ Math.floor(Math.random() * items.length) ]
466  
-
467  
-  # Public: Tell the message to stop dispatching to listeners
468  
-  #
469  
-  # Returns nothing.
470  
-  finish: ->
471  
-    @message.finish()
472  
-
473  
-  # Public: Creates a scoped http client with chainable methods for
474  
-  # modifying the request.  This doesn't actually make a request though.
475  
-  # Once your request is assembled, you can call `get()`/`post()`/etc to
476  
-  # send the request.
477  
-  #
478  
-  # url - String URL to access.
479  
-  #
480  
-  # Examples:
481  
-  #
482  
-  #     res.http("http://example.com")
483  
-  #       # set a single header
484  
-  #       .header('Authorization', 'bearer abcdef')
485  
-  #
486  
-  #       # set multiple headers
487  
-  #       .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
488  
-  #
489  
-  #       # add URI query parameters
490  
-  #       .query(a: 1, b: 'foo & bar')
491  
-  #
492  
-  #       # make the actual request
493  
-  #       .get() (err, res, body) ->
494  
-  #         console.log body
495  
-  #
496  
-  #       # or, you can POST data
497  
-  #       .post(data) (err, res, body) ->
498  
-  #         console.log body
499  
-  #
500  
-  # Returns a ScopedClient instance.
501  
-  http: (url) ->
502  
-    @httpClient.create(url)
503  
-
504  
-Robot.Response::httpClient = HttpClient
505 343
 
506 344
 module.exports = Robot
4  src/user.coffee
@@ -3,8 +3,8 @@ class User
3 3
   #
4 4
   # id      - A unique ID for the user.
5 5
   # options - An optional Hash of key, value pairs for this user.
6  
-  constructor: (@id, options = { }) ->
7  
-    for k of (options or { })
  6
+  constructor: (@id, options = {}) ->
  7
+    for k of (options or {})
8 8
       @[k] = options[k]
9 9
     @['name'] ||= @id
10 10
 
3  test/brain_test.coffee
... ...
@@ -1,5 +1,5 @@
1 1
 assert = require 'assert'
2  
-Robot  = require '../src/robot'
  2
+
3 3
 Brain  = require '../src/brain'
4 4
 
5 5
 saved   = false
@@ -23,4 +23,3 @@ brain.resetSaveInterval 0.1
23 23
 process.on 'exit', ->
24 24
   assert.ok saved
25 25
   assert.ok closed
26  
-
7  test/google_images_test.coffee
... ...
@@ -1,7 +1,10 @@
1 1
 Tests  = require './tests'
2 2
 assert = require 'assert'
3 3
 helper = Tests.helper()
4  
-Robot  = require '../src/robot'
  4
+
  5
+Robot         = require '../src/robot'
  6
+{TextMessage} = require '../src/message'
  7
+
5 8
 require('../src/scripts/google-images') helper
6 9
 
7 10
 # start up a danger room for google images
@@ -41,7 +44,7 @@ messages = [
41 44
 user = {}
42 45
 danger.start tests, ->
43 46
   for message in messages
44  
-    helper.receive new Robot.TextMessage user, message
  47
+    helper.receive new TextMessage user, message
45 48
 
46 49
   helper.stop()
47 50
 
17  test/tests.coffee
... ...
@@ -1,8 +1,11 @@
1  
-Robot   = require '../src/robot'
2  
-Adapter = require '../src/adapter'
3  
-User    = require '../src/user'
4  
-Path    = require 'path'
5  
-Url     = require 'url'
  1
+Url           = require 'url'
  2
+Path          = require 'path'
  3
+
  4
+Robot         = require '../src/robot'
  5
+Adapter       = require '../src/adapter'
  6
+User          = require '../src/user'
  7
+Response      = require '../src/response'
  8
+{TextMessage} = require '../src/message'
6 9
 
7 10
 # A programmer's best friend.
8 11
 # http://timenerdworld.files.wordpress.com/2010/12/joint-venture-s1e3_1.jpg
@@ -59,12 +62,12 @@ class Danger extends Adapter
59 62
   receive: (text) ->
60 63
     if typeof text is 'string'
61 64
       user = new User 1, 'helper'
62  
-      super new Robot.TextMessage user, text
  65
+      super new TextMessage user, text
63 66
     else
64 67
       super text
65 68
 
66 69
 if not process.env.HUBOT_LIVE
67  
-  class Helper.Response extends Robot.Response
  70
+  class Helper.Response extends Response
68 71
     # This changes ever HTTP request to hit the danger server above
69 72
     http: (url) ->
70 73
       super(url).host('127.0.0.1').port(9001)

0 notes on commit 90ffb1e

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