Skip to content

Commit

Permalink
Add 'setAttributes' method and test case for modifying an existing en…
Browse files Browse the repository at this point in the history
…dpoint's attributes
  • Loading branch information
Jeremy Race committed Feb 3, 2015
1 parent 8f35d0f commit 3350528
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ var SNS = require('sns-mobile'),
// EVENTS.DELETED_USER
// EVENTS.ADD_USER_FAILED
// EVENTS.ADDED_USER
// EVENTS.ATTRIBUTES_UPDATE_FAILED
// EVENTS.ATTRIBUTES_UPDATED
var myApp = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.ANDROID,
Expand Down Expand Up @@ -141,6 +143,18 @@ function (endpointArn, deviceId) {}
```
When a user is added this is emitted.

#### attributesUpdateFailed
```
function (endpointArn, err) {}
```
If updating the attributes for an endpoint fails this is emitted.

#### attributesUpdated
```
function (endpointArn, attributes) {}
```
When an endpoint's attributes are updated this is emitted.


## API

Expand Down Expand Up @@ -182,6 +196,15 @@ Get all users, this could take a while due to a potentially high number of reque
#### addUser(deviceToken, [data], callback)
Add a device/user to SNS with optional extra data. Callback has format fn(err, endpointArn).

#### setAttributes(endpointArn, attributes, callback)
Update an existing endpoint's attributes. Attributes is an object with the following optional properties:

* CustomUserData: <object|string>
* Enabled: <string>
* Token: <string>

Callback has format fn(err, endpointArn).

#### deleteUser(endpointArn, callback)
Delete a user from SNS. Callback has format callback(err)

Expand Down
35 changes: 33 additions & 2 deletions lib/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ var EMITTED_EVENTS = {
DELETED_USER: 'userDeleted',
FAILED_SEND: 'sendFailed',
ADDED_USER: 'userAdded',
ADD_USER_FAILED: 'addUserFailed'
ADD_USER_FAILED: 'addUserFailed',
ATTRIBUTES_UPDATED: 'attributesUpdated',
ATTRIBUTES_UPDATE_FAILED: 'attributesUpdateFailed'
};

var async = require('async')
Expand Down Expand Up @@ -118,7 +120,7 @@ Interface.prototype.addUser = function(deviceId, customUserData, callback) {
if (!err) {
self.emit(EMITTED_EVENTS.ADDED_USER, res.EndpointArn, deviceId);
} else {
self.emit(EMITTED_EVENTS.ADD_USER_FAILED, deviceId, err);
self.emit(EMITTED_EVENTS.ADD_USER_FAILED, deviceId, err);
}
return callback(err, ((res && res.EndpointArn) ? res.EndpointArn : null));
});
Expand All @@ -145,6 +147,35 @@ Interface.prototype.getUser = function(endpointArn, callback) {
};


/**
* Set a user's attributes by their EndpointArn
* @param {String} endpointArn
* @param {Object} attributes An object that can contain string properties for CustomUserData, Enabled and Token
* @param {Function} callback
*/

Interface.prototype.setAttributes = function(endpointArn, attributes, callback) {
var self = this;
if (typeof attributes == 'object') {
if (typeof attributes.CustomUserData == 'object') {
attributes.CustomUserData = JSON.stringify(attributes.CustomUserData);
}
}
var params = {
EndpointArn: endpointArn,
Attributes: attributes
};
this.sns.setEndpointAttributes(params, function(err, res) {
if (!err) {
self.emit(EMITTED_EVENTS.ATTRIBUTES_UPDATED, endpointArn, attributes);
} else {
self.emit(EMITTED_EVENTS.ATTRIBUTES_UPDATE_FAILED, endpointArn, err);
}
return callback(err, attributes);
});
};


/**
* Return a list of users.
* @param {Function} callback
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,43 @@ describe('SNS Module.', function() {
});
});

it('Should retrieve a user by their EndpointArn and update their properties.', function(done) {
sns.addUser('anotherfakedeviceidthatimadeup', JSON.stringify({
username: 'fakeuserforattributetest'
}), function(err, endpointArn) {
sns.getUser(endpointArn, function(err, res) {

var attributes = {
CustomUserData: {
user_id: 'updated-attribute-user-id'
},
Enabled: 'true'
};
sns.setAttributes(res.EndpointArn, attributes, function(err, res) {
assert(!err);
assert(res === attributes);

sns.getUser(endpointArn, function(err, res) {
assert(!err);
assert(res.EndpointArn === endpointArn);
assert(res.Attributes);
assert(res.Attributes.Enabled === attributes.Enabled);
assert(res.Attributes.CustomUserData);
var responseUserData = JSON.parse(res.Attributes.CustomUserData);
var userData = JSON.parse(attributes.CustomUserData);
assert(responseUserData.user_id === userData.user_id);

sns.deleteUser(endpointArn, function(err) {
// delete test user we created so that we can re-run the test
done();
});
});

});
});
});
});

it('Should retrieve a user by their EndpointArn and delete them', function(done) {
sns.addUser('somefakedeviceidthatimadeup', JSON.stringify({
username: 'fakeuser'
Expand Down

0 comments on commit 3350528

Please sign in to comment.