Skip to content

Commit dea1a06

Browse files
committed
Merge pull request #1 from intercom/BL/users
Create/update/list users
2 parents d34fbfe + 99a2b7c commit dea1a06

File tree

7 files changed

+160
-4
lines changed

7 files changed

+160
-4
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
"args": "after-used"
146146
}
147147
],
148-
"camelcase": 2,
148+
"camelcase": 0,
149149
"comma-spacing": 2,
150150
"new-cap": 2,
151151
"new-parens": 2,

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,67 @@
77
npm test
88
```
99

10+
## Running the code locally
11+
12+
Compile using babel:
13+
14+
```
15+
gulp babel
16+
```
17+
18+
Start a repl:
19+
20+
```
21+
node
22+
```
23+
24+
Require Intercom:
25+
26+
```node
27+
var Intercom = require('./dist/index');
28+
```
29+
30+
Create a client:
31+
32+
```node
33+
var client = new Intercom.Client("app_id", "app_api_key");
34+
```
35+
36+
## Users
37+
38+
```node
39+
// Create/update a user
40+
client.users.create({ email: "[email protected]" }, function (r) {
41+
console.log(r);
42+
});
43+
```
44+
45+
```node
46+
// List users
47+
client.users.list(function (d) { console.log(d.body.users.length) });
48+
```
49+
50+
```node
51+
// List users by tag or segment
52+
client.users.listBy({ tag_id: 'haven' }, function (d) {
53+
console.log(d.body.users.length)
54+
});
55+
```
56+
57+
```node
58+
// Find user by id
59+
client.users.find({ id: '1234' }, function (d) {
60+
console.log(d.body)
61+
});
62+
```
63+
64+
```node
65+
// Delete user by id
66+
client.users.delete({ id: '1234' }, function (d) {
67+
console.log(d.body)
68+
});
69+
```
70+
1071
## License
1172

1273
Apache-2.0

lib/client.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
var unirest = require('unirest');
2+
import {User} from './user';
23

34
export class Client {
45
constructor(appId, appApiKey) {
56
this.appId = appId;
67
this.appApiKey = appApiKey;
8+
this.users = new User(this);
79
}
810
ping(f) {
911
unirest.get('https://api.intercom.io/admins')
12+
.auth(this.appId, this.appApiKey)
13+
.type('json')
1014
.header('Accept', 'application/json')
11-
.header('Content-Type', 'application/json')
1215
.end(r => f(r.status));
1316
}
17+
post(endpoint, data, f) {
18+
unirest.post(`https://api.intercom.io${endpoint}`)
19+
.auth(this.appId, this.appApiKey)
20+
.type('json')
21+
.send(data)
22+
.header('Accept', 'application/json')
23+
.end(r => f(r));
24+
}
25+
get(endpoint, data, f) {
26+
unirest.get(`https://api.intercom.io${endpoint}`)
27+
.auth(this.appId, this.appApiKey)
28+
.type('json')
29+
.query(data)
30+
.header('Accept', 'application/json')
31+
.end(r => f(r));
32+
}
33+
delete(endpoint, data, f) {
34+
unirest.delete(`https://api.intercom.io${endpoint}`)
35+
.auth(this.appId, this.appApiKey)
36+
.type('json')
37+
.query(data)
38+
.header('Accept', 'application/json')
39+
.end(r => f(r));
40+
}
1441
}

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from '../lib/client';
1+
export * from './client';
2+
export * from './user';

lib/user.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export class User {
2+
constructor(client) {
3+
this.client = client;
4+
}
5+
create(data, f) {
6+
this.client.post('/users', data, f);
7+
}
8+
list(f) {
9+
this.client.get('/users', {}, f);
10+
}
11+
listBy(params, f) {
12+
this.client.get('/users', params, f);
13+
}
14+
find(params, f) {
15+
this.client.get(`/users/${params.id}`, {}, f);
16+
}
17+
delete(params, f) {
18+
this.client.delete(`/users/${params.id}`, {}, f);
19+
}
20+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"gulp-nsp": "^0.4.5",
3131
"gulp-babel": "^5.1.0",
3232
"babel-core": "^5.5.0",
33-
"nock" : "*"
33+
"nock" : "*",
34+
"locus" : "*"
3435
},
3536
"scripts": {
3637
"prepublish": "gulp prepublish",

test/user.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import assert from 'assert';
2+
import {Client} from '../lib';
3+
var nock = require('nock');
4+
5+
describe('users', function () {
6+
it('should be created', function (done) {
7+
nock('https://api.intercom.io').post('/users', { email: '[email protected]' }).reply(200, {});
8+
let client = new Client('foo', 'bar');
9+
client.users.create({ email: '[email protected]' }, function (r) {
10+
assert.equal(200, r.status);
11+
done();
12+
});
13+
});
14+
it('should list', function (done) {
15+
nock('https://api.intercom.io').get('/users').reply(200, {});
16+
let client = new Client('foo', 'bar');
17+
client.users.list(function (r) {
18+
assert.equal(200, r.status);
19+
done();
20+
});
21+
});
22+
it('should list by params', function (done) {
23+
nock('https://api.intercom.io').get('/users').query({ tag_id: '1234' }).reply(200, {});
24+
let client = new Client('foo', 'bar');
25+
client.users.listBy({ tag_id: '1234' }, function (r) {
26+
assert.equal(200, r.status);
27+
done();
28+
});
29+
});
30+
it('find users by id', function (done) {
31+
nock('https://api.intercom.io').get('/users/baz').reply(200, {});
32+
let client = new Client('foo', 'bar');
33+
client.users.find({ id: 'baz' }, function (r) {
34+
assert.equal(200, r.status);
35+
done();
36+
});
37+
});
38+
it('deletes users by id', function (done) {
39+
nock('https://api.intercom.io').delete('/users/baz').reply(200, {});
40+
let client = new Client('foo', 'bar');
41+
client.users.delete({ id: 'baz' }, function (r) {
42+
assert.equal(200, r.status);
43+
done();
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)