From d544aecd3e022b5f8881566e6fd4bac929d3543d Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 12 Jan 2016 15:04:42 +0100 Subject: [PATCH 1/4] json decode / encode --- lib/leaderboard.js | 33 ++++++++++++++++++++++++++++++--- src/leaderboard.coffee | 20 +++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/lib/leaderboard.js b/lib/leaderboard.js index d98b7e3..3b6dbbc 100644 --- a/lib/leaderboard.js +++ b/lib/leaderboard.js @@ -328,9 +328,13 @@ */ Leaderboard.prototype.memberDataForIn = function(leaderboardName, member, callback) { - return this.redisConnection.hget(this.memberDataKey(leaderboardName), member, function(err, reply) { - return callback(reply); - }); + return this.redisConnection.hget(this.memberDataKey(leaderboardName), member, (function(_this) { + return function(err, reply) { + var result; + result = _this.isJsonData(reply) ? JSON.parse(reply) : reply; + return callback(reply); + }; + })(this)); }; @@ -357,6 +361,8 @@ */ Leaderboard.prototype.updateMemberDataFor = function(leaderboardName, member, memberData, callback) { + var data; + data = typeof memberdata === 'object' ? JSON.stringify(memberData) : memberData; return this.redisConnection.hset(this.memberDataKey(leaderboardName), member, memberData, function(err, reply) { if (callback) { return callback(reply); @@ -1541,6 +1547,27 @@ return "" + leaderboardName + ":" + this.memberDataNamespace; }; + + /* + * Checks a string if encoded json + * + * @param value [String] String to be checked. + * + * @return boolean of string is json + */ + + Leaderboard.prototype.isJsonData = function(value) { + var e; + console.log(value); + try { + JSON.parse(str); + } catch (_error) { + e = _error; + return false; + } + return true; + }; + return Leaderboard; })(); diff --git a/src/leaderboard.coffee b/src/leaderboard.coffee index 508b5b4..003694f 100644 --- a/src/leaderboard.coffee +++ b/src/leaderboard.coffee @@ -222,7 +222,8 @@ class Leaderboard # @return String of optional member data. ### memberDataForIn: (leaderboardName, member, callback) -> - @redisConnection.hget(this.memberDataKey(leaderboardName), member, (err, reply) -> + @redisConnection.hget(this.memberDataKey(leaderboardName), member, (err, reply) => + result = if this.isJsonData(reply) then JSON.parse(reply) else reply callback(reply)) ### @@ -244,6 +245,7 @@ class Leaderboard # @param callback Optional callback for result of call. ### updateMemberDataFor: (leaderboardName, member, memberData, callback) -> + data = if typeof memberdata is 'object' then JSON.stringify(memberData) else memberData @redisConnection.hset(this.memberDataKey(leaderboardName), member, memberData, (err, reply) -> callback(reply) if callback) @@ -1078,4 +1080,20 @@ class Leaderboard memberDataKey: (leaderboardName) -> "#{leaderboardName}:#{@memberDataNamespace}" + ### + # Checks a string if encoded json + # + # @param value [String] String to be checked. + # + # @return boolean of string is json + ### + isJsonData: (value) -> + try + JSON.parse(str); + catch e + return false + return true + + + module.exports = Leaderboard From 488cbdf4e6a5a7e08ae8905c9359a11cf3c696c4 Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 12 Jan 2016 15:06:23 +0100 Subject: [PATCH 2/4] json decode / encode --- spec/leaderboard_spec.coffee | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/leaderboard_spec.coffee b/spec/leaderboard_spec.coffee index 5089392..2411da5 100644 --- a/spec/leaderboard_spec.coffee +++ b/spec/leaderboard_spec.coffee @@ -111,6 +111,17 @@ describe 'Leaderboard', -> reply.should.equal('Optional member data') done()) + it 'should allow you to store and retrieve optional member data as object', (done) -> + data = { + index: 3, + data: 'Optional member data' + } + @leaderboard.rankMember('member', 1, data, (reply) -> ) + + @leaderboard.memberDataFor('member', (reply) -> + reply.should.equal(data) + done()) + it 'should allow you to update optional member data', (done) -> @leaderboard.rankMember('member', 1, 'Optional member data', (reply) -> ) From 37471d12000c413535e4eeac8540fc8983c27418 Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 12 Jan 2016 15:07:20 +0100 Subject: [PATCH 3/4] also compiled --- lib/leaderboard.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/leaderboard.js b/lib/leaderboard.js index 3b6dbbc..06c3c26 100644 --- a/lib/leaderboard.js +++ b/lib/leaderboard.js @@ -1558,7 +1558,6 @@ Leaderboard.prototype.isJsonData = function(value) { var e; - console.log(value); try { JSON.parse(str); } catch (_error) { From 623547cd89a58cd9726a137456d510da4902c553 Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 12 Jan 2016 16:07:09 +0100 Subject: [PATCH 4/4] now working with whole API surface --- lib/leaderboard.js | 18 ++++++++++-------- spec/leaderboard_spec.coffee | 20 ++++++++++---------- src/leaderboard.coffee | 14 ++++++++------ 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/leaderboard.js b/lib/leaderboard.js index 06c3c26..50ee959 100644 --- a/lib/leaderboard.js +++ b/lib/leaderboard.js @@ -160,14 +160,15 @@ */ Leaderboard.prototype.rankMemberIn = function(leaderboardName, member, score, memberData, callback) { - var transaction; + var data, transaction; if (memberData == null) { memberData = null; } transaction = this.redisConnection.multi(); + data = typeof memberData === 'object' ? JSON.stringify(memberData) : memberData; transaction.zadd(leaderboardName, score, member); if (memberData != null) { - transaction.hset(this.memberDataKey(leaderboardName), member, memberData); + transaction.hset(this.memberDataKey(leaderboardName), member, data); } return transaction.exec(function(err, reply) { if (callback) { @@ -187,16 +188,17 @@ */ Leaderboard.prototype.rankMemberAcross = function(leaderboardNames, member, score, memberData, callback) { - var leaderboardName, transaction, _i, _len; + var data, leaderboardName, transaction, _i, _len; if (memberData == null) { memberData = null; } + data = typeof memberData === 'object' ? JSON.stringify(memberData) : memberData; transaction = this.redisConnection.multi(); for (_i = 0, _len = leaderboardNames.length; _i < _len; _i++) { leaderboardName = leaderboardNames[_i]; transaction.zadd(leaderboardName, score, member); if (memberData != null) { - transaction.hset(this.memberDataKey(leaderboardName), member, memberData); + transaction.hset(this.memberDataKey(leaderboardName), member, data); } } return transaction.exec(function(err, reply) { @@ -332,7 +334,7 @@ return function(err, reply) { var result; result = _this.isJsonData(reply) ? JSON.parse(reply) : reply; - return callback(reply); + return callback(result); }; })(this)); }; @@ -362,8 +364,8 @@ Leaderboard.prototype.updateMemberDataFor = function(leaderboardName, member, memberData, callback) { var data; - data = typeof memberdata === 'object' ? JSON.stringify(memberData) : memberData; - return this.redisConnection.hset(this.memberDataKey(leaderboardName), member, memberData, function(err, reply) { + data = typeof memberData === 'object' ? JSON.stringify(memberData) : memberData; + return this.redisConnection.hset(this.memberDataKey(leaderboardName), member, data, function(err, reply) { if (callback) { return callback(reply); } @@ -1559,7 +1561,7 @@ Leaderboard.prototype.isJsonData = function(value) { var e; try { - JSON.parse(str); + JSON.parse(value); } catch (_error) { e = _error; return false; diff --git a/spec/leaderboard_spec.coffee b/spec/leaderboard_spec.coffee index 2411da5..970de93 100644 --- a/spec/leaderboard_spec.coffee +++ b/spec/leaderboard_spec.coffee @@ -111,16 +111,16 @@ describe 'Leaderboard', -> reply.should.equal('Optional member data') done()) - it 'should allow you to store and retrieve optional member data as object', (done) -> - data = { - index: 3, - data: 'Optional member data' - } - @leaderboard.rankMember('member', 1, data, (reply) -> ) - - @leaderboard.memberDataFor('member', (reply) -> - reply.should.equal(data) - done()) + it 'should allow you to store and retrieve optional member data as object', (done) -> + data = { + index: 3, + data: 'Optional member data' + } + @leaderboard.rankMember('member', 1, data, (reply) -> ) + + @leaderboard.memberDataFor('member', (reply) -> + reply.should.deepEqual(data) + done()) it 'should allow you to update optional member data', (done) -> @leaderboard.rankMember('member', 1, 'Optional member data', (reply) -> ) diff --git a/src/leaderboard.coffee b/src/leaderboard.coffee index 003694f..53c2f5f 100644 --- a/src/leaderboard.coffee +++ b/src/leaderboard.coffee @@ -112,8 +112,9 @@ class Leaderboard ### rankMemberIn: (leaderboardName, member, score, memberData = null, callback) -> transaction = @redisConnection.multi() + data = if typeof memberData is 'object' then JSON.stringify(memberData) else memberData transaction.zadd(leaderboardName, score, member) - transaction.hset(this.memberDataKey(leaderboardName), member, memberData) if memberData? + transaction.hset(this.memberDataKey(leaderboardName), member, data) if memberData? transaction.exec((err, reply) -> callback(reply) if callback) @@ -126,10 +127,11 @@ class Leaderboard # @param member_data [String] Optional member data. ### rankMemberAcross: (leaderboardNames, member, score, memberData = null, callback) -> + data = if typeof memberData is 'object' then JSON.stringify(memberData) else memberData transaction = @redisConnection.multi() for leaderboardName in leaderboardNames transaction.zadd(leaderboardName, score, member) - transaction.hset(this.memberDataKey(leaderboardName), member, memberData) if memberData? + transaction.hset(this.memberDataKey(leaderboardName), member, data) if memberData? transaction.exec((err, reply) -> callback(reply) if callback) @@ -224,7 +226,7 @@ class Leaderboard memberDataForIn: (leaderboardName, member, callback) -> @redisConnection.hget(this.memberDataKey(leaderboardName), member, (err, reply) => result = if this.isJsonData(reply) then JSON.parse(reply) else reply - callback(reply)) + callback(result)) ### # Update the optional member data for a given member in the leaderboard. @@ -245,8 +247,8 @@ class Leaderboard # @param callback Optional callback for result of call. ### updateMemberDataFor: (leaderboardName, member, memberData, callback) -> - data = if typeof memberdata is 'object' then JSON.stringify(memberData) else memberData - @redisConnection.hset(this.memberDataKey(leaderboardName), member, memberData, (err, reply) -> + data = if typeof memberData is 'object' then JSON.stringify(memberData) else memberData + @redisConnection.hset(this.memberDataKey(leaderboardName), member, data, (err, reply) -> callback(reply) if callback) ### @@ -1089,7 +1091,7 @@ class Leaderboard ### isJsonData: (value) -> try - JSON.parse(str); + JSON.parse(value); catch e return false return true