Permalink
Browse files

Guard against an out of bounds index in member_at

  • Loading branch information...
1 parent 7d90315 commit 89d222e13d7c13cab1bf7822ad9e71e6fc12da63 @czarneckid czarneckid committed Sep 16, 2016
Showing with 8 additions and 1 deletion.
  1. +1 −0 CHANGELOG.markdown
  2. +1 −1 lib/leaderboard.rb
  3. +6 −0 spec/leaderboard_spec.rb
View
@@ -4,6 +4,7 @@
* More pipelining [#58](https://github.com/agoragames/leaderboard/pull/58).
Also adds `members_data_for(...) and `members_data_for_in(...)` methods.
+* Guard against an out of bounds index in `member_at`
## 3.10.0 (2015-03-20)
View
@@ -880,7 +880,7 @@ def member_at(position, options = {})
#
# @return a page of leaders from the named leaderboard.
def member_at_in(leaderboard_name, position, options = {})
- if position <= total_members_in(leaderboard_name)
+ if position > 0 && position <= total_members_in(leaderboard_name)
leaderboard_options = DEFAULT_LEADERBOARD_REQUEST_OPTIONS.dup
leaderboard_options.merge!(options)
page_size = validate_page_size(leaderboard_options[:page_size]) || @page_size
@@ -242,6 +242,12 @@
expect(@leaderboard.member_at(1, :with_member_data => true)[:member_data]).to eq({:member_name => 'Leaderboard member 50'}.to_s)
end
+ it 'should not return a single member when calling member_at with a position < 0' do
+ rank_members_in_leaderboard(5)
+ expect(@leaderboard.member_at(1)[:rank]).to eql(1)
+ expect(@leaderboard.member_at(-5)).to be nil
+ end
+
it 'should return the correct information when calling around_me' do
rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)

0 comments on commit 89d222e

Please sign in to comment.