-Robot   = require('hubot').Robot
-Adapter = require('hubot').Adapter
-TextMessage = require('hubot').TextMessage
-HTTPS = require 'https'
+{Robot, Adapter, TextMessage} = require 'hubot'
+https = require 'https'
 
 class Slack extends Adapter
+  ###################################################################
+  # Slightly abstract logging, primarily so that it can
+  # be easily altered for unit tests.
+  ###################################################################
   log: console.log.bind console
   logError: console.error.bind console
 
+
+  ###################################################################
+  # Communicating back to the chat rooms. These are exposed
+  # as methods on the argument passed to callbacks from
+  # robot.respond, robot.listen, etc.
+  ###################################################################
   send: (params, strings...) ->
     @log "Sending message"
-
-    user = @userFromParams(params)
+    user = @userFromParams params
 
     strings.forEach (str) =>
       str = @escapeHtml str
-
-      args = JSON.stringify({"channel": user.reply_to, "text": str, username: @robot.name})
+      args = JSON.stringify
+        username : @robot.name
+        channel  : user.reply_to
+        text     : str
+
       @post "/services/hooks/hubot", args
 
   reply: (params, strings...) ->
     @log "Sending reply"
 
-    user = @userFromParams(params)
+    user = @userFromParams params
     strings.forEach (str) =>
       @send params, "#{user.name}: #{str}"
 
   topic: (params, strings...) ->
     # TODO: Set the topic
 
+
+  ###################################################################
+  # HTML helpers.
+  ###################################################################
   escapeHtml: (string) ->
     string
       # Escape entities

  request: (method, path, body, callback) ->
    self = @

    #console.log method, path, body
    host = @options.team + '.slack.com'
    headers = "Host": host

    path += "?token=" + @options.token

    req_options =
      "agent"  : false
      "hostname"   : host
      "port"   : 443
      "path"   : path
      "method" : method
      "headers": headers

    if method is "POST"
      headers["Content-Type"] = "application/x-www-form-urlencoded"
      req_options.headers["Content-Length"] = body.length

    request = HTTPS.request req_options, (response) ->
      data = ""
      response.on "data", (chunk) ->
        data += chunk

      .replace(/<((\bhttps?)(.*))?>/g, '$1')


  ###################################################################
  # Parsing inputs.
  ###################################################################
  userFromParams: (params) ->
    # hubot < 2.4.2: params = user
    # hubot >= 2.4.2: params = {user: user, ...}
    params = params.user or params

    # Ghetto hack to make robot.messageRoom work with Slack's adapter
    #
