Skip to content

Commit

Permalink
Add top and top_in helper methods
Browse files Browse the repository at this point in the history
Add `top(number)` method as a shorthand of `members_from_rank_range(1,number)

Add `top_in(leaderboard_name, number)` method as a shorthan of `members_from_rank_range_in(leaderboard_name, 1, number)`

Testing top methods for rever leadearboard type

Testing Againts to Tie Ranking Leaderboard

Testing Competition Ranking Leaderboard
  • Loading branch information
robertomiranda committed Dec 10, 2014
1 parent 20c18f1 commit af7d296
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 5 deletions.
25 changes: 24 additions & 1 deletion lib/leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,29 @@ def members_from_rank_range_in(leaderboard_name, starting_rank, ending_rank, opt
end
end

# Retrieve members from the leaderboard within a range from 1 to the number given.
#
# @param ending_rank [int] Ending rank (inclusive).
# @param options [Hash] Options to be used when retrieving the data from the leaderboard.
#
# @return number from the leaderboard that fall within the given rank range.
def top(number, options = {})
members_from_rank_range(1, number, options)
end

# Retrieve members from the named leaderboard within a range from 1 to the number given.
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param starting_rank [int] Starting rank (inclusive).
# @param ending_rank [int] Ending rank (inclusive).
# @param options [Hash] Options to be used when retrieving the data from the leaderboard.
#
# @return members from the leaderboard that fall within the given rank range.
def top_in(leaderboard_name, number, options={})
members_from_rank_range_in(leaderboard_name, 1, number, options)
end


# Retrieve a member at the specified index from the leaderboard.
#
# @param position [int] Position in leaderboard.
Expand Down Expand Up @@ -989,4 +1012,4 @@ def validate_page_size(page_size)

page_size
end
end
end
45 changes: 44 additions & 1 deletion spec/competition_ranking_leaderboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,48 @@

leaderboard.disconnect
end

it 'should allow you to retrieve a given set of members from the leaderboard in a range from 1 to the number given' do
@leaderboard = CompetitionRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
rank_members_in_leaderboard(25)

members = @leaderboard.top(5)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[4][:member]).to eql('member_21')

members = @leaderboard.top(1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_25')

members = @leaderboard.top(26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[24][:member]).to eql('member_1')
end

it 'should allow you to retrieve a given set of members from the named leaderboard in a range from 1 to the number given' do
@leaderboard = CompetitionRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
rank_members_in_leaderboard(25)

members = @leaderboard.top_in("ties", 5)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[4][:member]).to eql('member_21')

members = @leaderboard.top(1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_25')

members = @leaderboard.top(26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[24][:member]).to eql('member_1')
end

end
end
end
42 changes: 41 additions & 1 deletion spec/leaderboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,46 @@
expect(members[24][:member]).to eql('member_1')
end

it 'should allow you to retrieve a given set of members from the leaderboard in a range from 1 to the number given' do
rank_members_in_leaderboard(25)

members = @leaderboard.top(5)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[4][:member]).to eql('member_21')

members = @leaderboard.top(1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_25')

members = @leaderboard.top(26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[24][:member]).to eql('member_1')
end

it 'should allow you to retrieve a given set of members from the named leaderboard in a range from 1 to the number given' do
rank_members_in_leaderboard(25)

members = @leaderboard.top_in("name", 5)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[4][:member]).to eql('member_21')

members = @leaderboard.top(1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_25')

members = @leaderboard.top(26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[24][:member]).to eql('member_1')
end

it 'should sort by rank if the :sort_by option is set to :rank' do
rank_members_in_leaderboard(25)

Expand Down Expand Up @@ -767,4 +807,4 @@
expect(@redis_connection.exists("name:member_data")).to be_falsey
expect(@redis_connection.exists("name:md")).to be_truthy
end
end
end
44 changes: 43 additions & 1 deletion spec/reverse_leaderboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,48 @@
expect(members[24][:member]).to eql('member_25')
end

it 'should allow you to retrieve a given set of members from the leaderboard in a range from 1 to the number given' do
rank_members_in_leaderboard(25)

members = @leaderboard.top(5)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_1')
expect(members[0][:score].to_i).to be(1)
expect(members[4][:member]).to eql('member_5')

members = @leaderboard.top(1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_1')

members = @leaderboard.top(26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_1')
expect(members[0][:score].to_i).to be(1)
expect(members[24][:member]).to eql('member_25')
end

it 'should allow you to retrieve a given set of members from the named leaderboard in a range from 1 to the number given' do
rank_members_in_leaderboard(25)

members = @leaderboard.top_in("name", 5)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_1')
expect(members[0][:score].to_i).to be(1)
expect(members[4][:member]).to eql('member_5')

members = @leaderboard.top(1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_1')

members = @leaderboard.top(26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_1')
expect(members[0][:score].to_i).to be(1)
expect(members[24][:member]).to eql('member_25')
end



it 'should sort by rank if the :sort_by option is set to :rank' do
rank_members_in_leaderboard(25)

Expand Down Expand Up @@ -370,4 +412,4 @@
expect(ranked_members[2][:rank]).to be(10)
expect(ranked_members[2][:score]).to eql(10.0)
end
end
end
65 changes: 64 additions & 1 deletion spec/tie_ranking_leaderboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,69 @@
end
end

it 'should allow you to retrieve a given set of members from the leaderboard in a rank range' do
@leaderboard = TieRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
rank_members_in_leaderboard(25)

members = @leaderboard.members_from_rank_range(5, 9)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_21')
expect(members[0][:score].to_i).to be(21)
expect(members[4][:member]).to eql('member_17')

members = @leaderboard.members_from_rank_range(1, 1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_25')

members = @leaderboard.members_from_rank_range(-1, 26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[24][:member]).to eql('member_1')
end

it 'should allow you to retrieve a given set of members from the leaderboard in a range from 1 to the number given' do
@leaderboard = TieRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
rank_members_in_leaderboard(25)

members = @leaderboard.top(5)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[4][:member]).to eql('member_21')

members = @leaderboard.top(1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_25')

members = @leaderboard.top(26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[24][:member]).to eql('member_1')
end

it 'should allow you to retrieve a given set of members from the named leaderboard in a range from 1 to the number given' do
@leaderboard = TieRankingLeaderboard.new('name', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})
rank_members_in_leaderboard(25)

members = @leaderboard.top_in("name", 5)
expect(members.size).to be(5)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[4][:member]).to eql('member_21')

members = @leaderboard.top(1)
expect(members.size).to be(1)
expect(members[0][:member]).to eql('member_25')

members = @leaderboard.top(26)
expect(members.size).to be(25)
expect(members[0][:member]).to eql('member_25')
expect(members[0][:score].to_i).to be(25)
expect(members[24][:member]).to eql('member_1')
end

it 'should expire the ties leaderboard in a given number of seconds' do
@leaderboard = TieRankingLeaderboard.new('ties', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15})

Expand Down Expand Up @@ -238,4 +301,4 @@
end
end
end
end
end

0 comments on commit af7d296

Please sign in to comment.