Skip to content

Commit

Permalink
Merge tag 'vumigo-0.2.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
hodgestar committed Feb 24, 2015
2 parents 5aa5b40 + fe9402f commit 1931d86
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
# The short X.Y version.
version = '0.2'
# The full version, including alpha/beta/rc tags.
release = '0.2.10'
release = '0.2.12'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
28 changes: 28 additions & 0 deletions lib/kv/dummy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ var DummyKvResource = DummyResource.extend(function(self, name, store) {
*/
self.store = store || {};

/**attribute:DummyKvResource.ttl
An object mapping keys set to expire to their lifetime (in seconds).
*/
self.ttl = {};

self.incr = function(key, amount) {
/**:DummyKvResource.incr(key[, amount])
Expand Down Expand Up @@ -63,6 +68,28 @@ var DummyKvResource = DummyResource.extend(function(self, name, store) {
return value;
};

self.set_ttl = function(key, seconds) {
/**:DummyKvResource.set_ttl(key[, seconds])
Set or remove the ttl (expiry time) of a key.
If seconds is ``null`` or undefined the key is set not to expire (and
its ttl is removed).
:param string key:
The key to set the ttl for.
:param integer seconds:
The number of seconds to set the ttl to. Defaults to ``null``.
*/
seconds = (typeof seconds !== 'undefined') ? seconds : null;
if (seconds === null) {
delete self.ttl[key];
}
else {
self.ttl[key] = seconds;
}
};

self.handlers.get = function(cmd) {
var value = self.store[cmd.key];

Expand All @@ -78,6 +105,7 @@ var DummyKvResource = DummyResource.extend(function(self, name, store) {

self.handlers.set = function(cmd) {
self.store[cmd.key] = cmd.value;
self.set_ttl(cmd.key, cmd.seconds);
return {success: true};
};

Expand Down
15 changes: 1 addition & 14 deletions lib/states/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,9 @@ var State = Eventable.extend(function(self, name, opts) {
Set the state that the user will visit after this state using the given
state name.
:param string opts.name:
:param string name:
The name of the next state
*/
/**:State:set_next_state(opts)
Set the state that the user will visit after this state using an
options object.
:param string opts.name:
The name of the next state
:param object opts.metadata:
Metadata to pass to the next state. Optional.
:param object opts.creator_opts:
Options to pass to the state creator when creating the state.
Optional.
*/
/**:State:set_next_state(fn[, arg1[, arg2[, ...]]])
Use a function to set the state that the user will visit this state.
Expand Down
25 changes: 23 additions & 2 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,40 @@ var User = Eventable.extend(function(self, im) {
});
};

self.save = function() {
self.default_ttl = function() {
/**:User.default_ttl()
Returns the default expiry time of saved user state (in seconds).
This may be set using the ``user_ttl`` sandbox config key. It
defaults to 604800 seconds (seven days). Expiry may be disabled by
setting ``user_ttl`` to ``null``.
*/
var ttl = self.im.config.user_ttl;
return (typeof ttl !== 'undefined') ? ttl : 604800;
};

self.save = function(opts) {
/**:User.save()
Save a user's current state to the key-value data store resource, then
emits a :class:`UserSaveEvent`.
:param object opts.seconds:
How long the user's state should be stored for before expiring. See
:meth:`User.default_ttl` for how the default is determined.
Returns a promise that is fulfilled once the user data has been saved
and events have been emitted.
*/
opts = _.defaults(opts || {}, {
seconds: self.default_ttl(),
});
return self.im
.api_request("kv.set", {
key: self.key(),
value: self.serialize()
value: self.serialize(),
seconds: opts.seconds,
})
.then(function() {
return self.emit(new UserSaveEvent(self));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vumigo_v02",
"version": "0.2.11",
"version": "0.2.12",
"description": "Javascript toolkit and examples for Vumi's Javascript sandbox",
"main": "lib/index.js",
"scripts": {
Expand Down
33 changes: 33 additions & 0 deletions test/test_kv/test_dummy.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ describe("kv.dummy", function() {
});
});

describe(".set_ttl", function() {
it("should set the ttl if given", function() {
assert.deepEqual(api.kv.ttl, {});
api.kv.set_ttl("foo", 5);
assert.deepEqual(api.kv.ttl, {"foo": 5});
});

it("should unset the ttl if seconds is null", function() {
api.kv.ttl.foo = 10;
api.kv.set_ttl("foo", null);
assert.deepEqual(api.kv.ttl, {});
});

it("should unset the ttl if seconds is undefined", function() {
api.kv.ttl.foo = 10;
api.kv.set_ttl("foo");
assert.deepEqual(api.kv.ttl, {});
});
});

describe(".handlers", function() {
describe(".get", function() {
it("should retrieve the value for the given key", function() {
Expand Down Expand Up @@ -103,6 +123,19 @@ describe("kv.dummy", function() {
}).then(function(reply) {
assert(reply.success);
assert.equal(api.kv.store.foo, 'bar');
assert.equal(typeof api.kv.ttl.foo, 'undefined');
});
});

it("should set an expiry if one is given", function() {
return request('kv.set', {
key: 'foo',
value: 'bar',
seconds: 15
}).then(function(reply) {
assert(reply.success);
assert.equal(api.kv.store.foo, 'bar');
assert.equal(api.kv.ttl.foo, 15);
});
});
});
Expand Down
45 changes: 45 additions & 0 deletions test/test_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ describe("user", function() {
});
});

describe(".default_ttl", function() {
it("should be seven days if no config is set", function() {
assert.strictEqual(user.default_ttl(), 604800);
});

describe("should be overriden", function() {
it("when config.user_ttl is an integer", function() {
im.config.user_ttl = 60;
assert.strictEqual(user.default_ttl(), 60);
});

it("when config.user_ttl is null", function() {
im.config.user_ttl = null;
assert.strictEqual(user.default_ttl(), null);
});
});
});

describe(".save", function() {
it("should save the user", function() {
user.set_answer('why', 'no');
Expand All @@ -220,6 +238,33 @@ describe("user", function() {
assert.equal(e.user, user);
});
});

it("should set the default ttl", function() {
return user
.save()
.then(function() {
assert.strictEqual(
im.api.kv.ttl[user.key()],
user.default_ttl());
});
});

it("should set an integer custom ttl", function() {
return user
.save({seconds: 5})
.then(function() {
assert.strictEqual(im.api.kv.ttl[user.key()], 5);
});
});

it("should not set a ttl for null seconds", function() {
return user
.save({seconds: null})
.then(function() {
assert.strictEqual(
user.key() in im.api.kv.ttl, false);
});
});
});

describe(".set_lang", function() {
Expand Down

0 comments on commit 1931d86

Please sign in to comment.