Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Data validation

* Fix for checking to see if a member actually exists in the leaderboard for the `around_me` calls
* Return appropriate `nil` in data returned for calls such as `percentile_for` and `ranked_in_list` for non-existent members
  • Loading branch information...
commit 33250e887313497be6ba8e7b31e00a346bceee45 1 parent 88e5582
@czarneckid czarneckid authored
View
5 CHANGELOG.markdown
@@ -1,5 +1,10 @@
# CHANGELOG
+## leaderboard 2.0.2 (2012-02-03)
+
+ * Fix for checking to see if a member actually exists in the leaderboard for the `around_me` calls
+ * Return appropriate `nil` in data returned for calls such as `percentile_for` and `ranked_in_list` for non-existent members
+
## leaderboard 2.0.1 (2011-11-07)
* Allow for only single options to be passed to `leaders`, `around_me` and `ranked_in_list` methods - https://github.com/agoragames/leaderboard/issues/4
View
8 lib/leaderboard.rb
@@ -315,6 +315,8 @@ def percentile_for(member)
#
# @return the percentile for a member in the named leaderboard.
def percentile_for_in(leaderboard_name, member)
+ return nil unless check_member_in?(leaderboard_name, member)
+
responses = @redis_connection.multi do |transaction|
transaction.zcard(leaderboard_name)
transaction.zrevrank(leaderboard_name, member)
@@ -393,6 +395,8 @@ def around_me_in(leaderboard_name, member, options = {})
leaderboard_options.merge!(options)
reverse_rank_for_member = @redis_connection.zrevrank(leaderboard_name, member)
+
+ return [] unless reverse_rank_for_member
page_size = validate_page_size(leaderboard_options[:page_size]) || @page_size
@@ -449,7 +453,7 @@ def ranked_in_list_in(leaderboard_name, members, options = {})
if leaderboard_options[:use_zero_index_for_rank]
data[:rank] = responses[index * 2]
else
- data[:rank] = responses[index * 2] + 1
+ data[:rank] = responses[index * 2] + 1 rescue nil
end
data[:score] = responses[index * 2 + 1].to_f
@@ -461,7 +465,7 @@ def ranked_in_list_in(leaderboard_name, members, options = {})
if leaderboard_options[:use_zero_index_for_rank]
data[:rank] = responses[index]
else
- data[:rank] = responses[index] + 1
+ data[:rank] = responses[index] + 1 rescue nil
end
end
end
View
2  lib/leaderboard/version.rb
@@ -1,3 +1,3 @@
class Leaderboard
- VERSION = '2.0.1'.freeze
+ VERSION = '2.0.2'.freeze
end
View
39 test/test_leaderboard.rb
@@ -13,7 +13,7 @@ def teardown
end
def test_version
- assert_equal '2.0.1', Leaderboard::VERSION
+ assert_equal '2.0.2', Leaderboard::VERSION
end
def test_initialize_with_defaults
@@ -418,7 +418,44 @@ def test_percentile_for
assert_equal 25, @leaderboard.percentile_for('member_4')
assert_equal 92, @leaderboard.percentile_for('member_12')
end
+
+ def test_around_me_for_invalid_member
+ rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)
+
+ leaders_around_me = @leaderboard.around_me('jones', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:page_size => 3}))
+ assert_equal 0, leaders_around_me.size
+ end
+
+ def test_score_and_rank_for_non_existent_member
+ score_and_rank_for_member = @leaderboard.score_and_rank_for('jones')
+ assert_equal 'jones', score_and_rank_for_member[:member]
+ assert_equal 0.0, score_and_rank_for_member[:score]
+ assert_nil score_and_rank_for_member[:rank]
+ end
+
+ def test_ranked_in_list_for_non_existent_member
+ rank_members_in_leaderboard
+
+ members = ['member_1', 'member_5', 'jones']
+ ranked_members = @leaderboard.ranked_in_list(members)
+
+ assert_equal 3, ranked_members.size
+ assert_nil ranked_members[2][:rank]
+ end
+
+ def test_percentile_for_non_existent_member
+ percentile = @leaderboard.percentile_for('jones')
+
+ assert_nil percentile
+ end
+
+ def test_change_score_for_non_existent_member
+ assert_equal 0.0, @leaderboard.score_for('jones')
+ @leaderboard.change_score_for('jones', 5)
+ assert_equal 5.0, @leaderboard.score_for('jones')
+ end
+
private
def rank_members_in_leaderboard(members_to_add = 5)
Please sign in to comment.
Something went wrong with that request. Please try again.