Skip to content

Commit

Permalink
Use Hubot.Message directly with Hubot >= 2.11.0
Browse files Browse the repository at this point in the history
Hubot 2.11.0 now exports Message directly, so we can stop pulling out
TextMessage's superclass. We still need the workaround when using older
versions of Hubot.
  • Loading branch information
lilyball committed Dec 23, 2014
1 parent e823160 commit 1c49e4d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"mocha": "~1.13.0",
"grunt-contrib-watch": "~0.5.3",
"grunt-shell": "~0.5.0",
"hubot": "~2.10"
"hubot": "~2.11"
},
"main": "./index",
"engines": {
Expand Down
11 changes: 7 additions & 4 deletions src/message.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{TextMessage} = require 'hubot'
{Message, TextMessage} = require 'hubot'

# Hubot only started exporting Message in 2.11.0. Previous version do not export
# this class. In order to remain compatible with older versions, we can pull the
# Message class from TextMessage superclass.
if not Message
Message = TextMessage.__super__.constructor

class SlackTextMessage extends TextMessage
# Represents a TextMessage created from the Slack adapter
Expand All @@ -10,9 +16,6 @@ class SlackTextMessage extends TextMessage
constructor: (@user, @text, @rawText, @rawMessage) ->
super @user, @text, @rawMessage.ts

# For some reason Hubot doesn't export Message, but that's what we want to extend.
# As a workaround, let's grab TextMessage's superclass
Message = TextMessage.__super__.constructor
class SlackRawMessage extends Message
# Represents Slack messages that are not suitable to treat as text messages.
# These are hidden messages, or messages that have no text / attachments.
Expand Down
35 changes: 35 additions & 0 deletions test/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,38 @@ describe 'Receiving a Slack message', ->
@stubs.robot.received.should.have.length 1
msg = @stubs.robot.received[0]
msg.should.be.an.instanceOf SlackRawMessage

describe 'should handle SlackRawMessage inheritance properly when Hubot', ->
# this is a bit of a wacky one
# We need to muck with the require() machinery
# To that end, we're going to save off the modules we need to tweak, so we can
# remove the cache and reload from disk
beforeEach ->
mods = (require.resolve name for name in ['hubot', '../src/message'])
@saved = []
# ensure the modules are loaded
require path for path in mods # ensure the modules are loaded
@saved = (require.cache[path] for path in mods) # grab the modules
delete require.cache[path] for path in mods # remove the modules from the require cache

afterEach ->
# restore the saved modules
for mod in @saved
require.cache[mod.filename] = mod
delete @saved

it 'does not export Message', ->
delete require('hubot').Message # remove hubot.Message if it exists
{SlackRawMessage: rawMessage} = require '../src/message'
rawMessage::constructor.__super__.constructor.name.should.equal 'Message'

it 'does export Message', ->
if not require('hubot').Message
# create a placeholder class to use here
# We're not actually running any code from Message during the evaluation
# of src/message.coffee so we don't need the real class.
# note: using JavaScript escape because CoffeeScript doesn't allow shadowing otherwise
`function Message() {}`
require('hubot').Message = Message
{SlackRawMessage: rawMessage} = require '../src/message'
rawMessage::constructor.__super__.constructor.name.should.equal 'Message'

0 comments on commit 1c49e4d

Please sign in to comment.