Skip to content

Commit

Permalink
Merge branch 'master' into upgrade-nyc
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles authored Jan 10, 2019
2 parents fd0f06b + a3d80c1 commit d7b3a33
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Hubot includes two official adapters:
* [Shell](./adapters/shell.md), i.e. for use with development
* [Campfire](./adapters/campfire.md)

There are Third-party adapaters are available for most chat services. Here are the most popular ones:
There are Third-party adapters available for most chat services. Here are the most popular ones:

* [Gitter](https://github.com/huafu/hubot-gitter2)
* [HipChat](https://github.com/hipchat/hubot-hipchat)
Expand Down
16 changes: 14 additions & 2 deletions docs/adapters/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ permalink: /docs/adapters/development/

## Adapter Basics

All adapters inherit from the Adapter class in the `src/adapter.coffee` file. There are certain methods that you will want to override. Here is a basic stub of what an extended Adapter class would look like:
All adapters inherit from the Adapter class in the `src/adapter.coffee` file. If you're writing your adapter in CoffeeScript, require the primary version of the adapter:

```coffee
Adapter = require('hubot').Adapter
```

If you're writing your adapter in ES2015, you must require the ES2015 entrypoint instead:

```js
const Adapter = require('hubot/es2015').Adapter;
```

There are certain methods that you will want to override. Here is a basic stub of what an extended Adapter class would look like:

```coffee
class Sample extends Adapter
Expand Down Expand Up @@ -52,7 +64,7 @@ exports.use = (robot) ->
"hubot": ">=2.0"
},
"devDependencies": {
"coffee-script": ">=1.2.0"
"coffeescript": ">=1.2.0"
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/deploying/azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Then, edit this file and look for the sections that give you steps 1, 2 and 3. Y

Now, create a new file in the base directory of hubot called `server.js` and put these two lines into it:

require('coffee-script/register');
require('coffeescript/register');
module.exports = require('hubot/bin/hubot.coffee');

Finally you will need to add the environment variables to the website to make sure it runs properly. You can either do it through the GUI (under configuration) or you can use the Azure PowerShell command line, as follows (example is showing slack as an adapter and mynewhubot as the website name).
Expand Down
6 changes: 3 additions & 3 deletions docs/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -871,12 +871,12 @@ You'll also need to install:

You may also want to install:

* *coffee-script* (if you're writing your tests in CoffeeScript rather than JavaScript)
* *coffeescript* (if you're writing your tests in CoffeeScript rather than JavaScript)
* a mocking library such as *Sinon.js* (if your script performs webservice calls or
other asynchronous actions)

Here is a sample script that tests the first couple of commands in the
[Hubot sample script](https://github.com/hubotio/generator-hubot/blob/master/generators/app/templates/scripts/example.coffee). This script uses *Mocha*, *chai*, *coffee-script*, and of course *hubot-test-helper*:
[Hubot sample script](https://github.com/hubotio/generator-hubot/blob/master/generators/app/templates/scripts/example.coffee). This script uses *Mocha*, *chai*, *coffeescript*, and of course *hubot-test-helper*:

**test/example-test.coffee**
```coffeescript
Expand Down Expand Up @@ -918,7 +918,7 @@ describe 'example script', ->

**sample output**
```bash
% mocha --require coffee-script/register test/*.coffee
% mocha --require coffeescript/register test/*.coffee


example script
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"async": ">=0.1.0 <1.0.0",
"chalk": "^1.0.0",
"cline": "^0.8.2",
"coffee-script": "1.6.3",
"coffeescript": "1.6.3",
"connect-multiparty": "^2.1.1",
"express": "^4.16.3",
"log": "1.4.0",
Expand Down
30 changes: 30 additions & 0 deletions src/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ const EventEmitter = require('events').EventEmitter

const User = require('./user')

// If necessary, reconstructs a User object. Returns either:
//
// 1. If the original object was falsy, null
// 2. If the original object was a User object, the original object
// 3. If the original object was a plain JavaScript object, return
// a User object with all of the original object's properties.
let reconstructUserIfNecessary = function (user) {
if (!user) {
return null
}

if (!user.constructor || (user.constructor && user.constructor.name !== 'User')) {
let id = user.id
delete user.id
// Use the old user as the "options" object,
// populating the new user with its values.
return new User(id, user)
} else {
return user
}
}

class Brain extends EventEmitter {
// Represents somewhat persistent storage for the robot. Extend this.
//
Expand Down Expand Up @@ -116,6 +138,14 @@ class Brain extends EventEmitter {
this.data[k] = data[k]
}

// Ensure users in the brain are still User objects.
if (data && data.users) {
for (let k in data.users) {
let user = this.data.users[k]
this.data.users[k] = reconstructUserIfNecessary(user)
}
}

this.emit('loaded', this.data)
}

Expand Down
1 change: 1 addition & 0 deletions src/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Robot {
// adapter - A String of the adapter name.
// httpd - A Boolean whether to enable the HTTP daemon.
// name - A String of the robot name, defaults to Hubot.
// alias - A String of the alias of the robot name
constructor (adapterPath, adapter, httpd, name, alias) {
if (name == null) {
name = 'Hubot'
Expand Down
19 changes: 19 additions & 0 deletions test/brain_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ describe('Brain', function () {
this.brain.mergeData({})
expect(this.brain.emit).to.have.been.calledWith('loaded', this.brain.data)
})

it('coerces loaded data into User objects', function () {
this.brain.mergeData({users: {'4': {'name': 'new', 'id': '4'}}})
let user = this.brain.userForId('4')
expect(user.constructor.name).to.equal('User')
expect(user.id).to.equal('4')
expect(user.name).to.equal('new')
})
})

describe('#save', () => it('emits a save event', function () {
Expand Down Expand Up @@ -308,5 +316,16 @@ describe('Brain', function () {
expect(result).to.have.members([this.user1, this.user2])
expect(result).to.not.have.members([this.user3])
})

it('returns User objects, not POJOs', function () {
expect(this.brain.userForId('1').constructor.name).to.equal('User')
for (let user of this.brain.usersForFuzzyName('Guy')) {
expect(user.constructor.name).to.equal('User')
}

for (let user of this.brain.usersForRawFuzzyName('Guy One')) {
expect(user.constructor.name).to.equal('User')
}
})
})
})

0 comments on commit d7b3a33

Please sign in to comment.