Skip to content

Commit

Permalink
prevRank on lib
Browse files Browse the repository at this point in the history
  • Loading branch information
henrod committed Aug 8, 2018
1 parent b0c14fb commit 414c2da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
20 changes: 12 additions & 8 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ var (

// Member maps an member identified by their publicID to their score and rank
type Member struct {
PublicID string
Score int
Rank int
previousRank int
LeaderboardID string
PublicID string
Score int
Rank int
PreviousRank int
}

//MemberList is a list of member
Expand All @@ -73,6 +74,7 @@ type Score struct {
PublicID string
Score int
Rank int
PreviousRank int
}

//ScoreList is a list of Scores
Expand Down Expand Up @@ -169,7 +171,7 @@ func (p *Podium) buildGetTopPercentURL(leaderboard string, percentage int) strin

func (p *Podium) buildUpdateScoreURL(leaderboard, memberID string, scoreTTL int) string {
var pathname = fmt.Sprintf("/l/%s/members/%s/score", leaderboard, memberID)
pathname = p.appendScoreTTL(pathname, scoreTTL)
pathname = p.appendScoreTTLAndPrevRank(pathname, scoreTTL)
return p.buildURL(pathname)
}

Expand All @@ -179,7 +181,7 @@ func (p *Podium) buildIncrementScoreURL(leaderboard, memberID string, scoreTTL i

func (p *Podium) buildUpdateScoresURL(memberID string, scoreTTL int) string {
var pathname = fmt.Sprintf("/m/%s/scores", memberID)
pathname = p.appendScoreTTL(pathname, scoreTTL)
pathname = p.appendScoreTTLAndPrevRank(pathname, scoreTTL)
return p.buildURL(pathname)
}

Expand Down Expand Up @@ -215,12 +217,14 @@ func (p *Podium) buildHealthcheckURL() string {
return p.buildURL(pathname)
}

func (p *Podium) appendScoreTTL(pathname string, scoreTTL int) string {
func (p *Podium) appendScoreTTLAndPrevRank(pathname string, scoreTTL int) string {
pathname = fmt.Sprintf("%s?prevRank=true", pathname)

if scoreTTL <= 0 {
return pathname
}

return fmt.Sprintf("%s?scoreTTL=%d", pathname, scoreTTL)
return fmt.Sprintf("%s&scoreTTL=%d", pathname, scoreTTL)
}

// GetTop returns the top members for this leaderboard. Page is 1-index
Expand Down
6 changes: 3 additions & 3 deletions lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var _ = Describe("Lib", func() {
leaderboard := globalLeaderboard

//mock url that should be called
url := "http://podium/l/" + leaderboard + "/members/1/score?scoreTTL=10"
url := "http://podium/l/" + leaderboard + "/members/1/score?prevRank=true&scoreTTL=10"
httpmock.RegisterResponder("PUT", url,
httpmock.NewStringResponder(200, `{ "success": true, "publicID": "1", "score": 2, "rank": 1 }`))

Expand Down Expand Up @@ -132,7 +132,7 @@ var _ = Describe("Lib", func() {
leaderboard := globalLeaderboard

//mock url that should be called
url := "http://podium/l/" + leaderboard + "/members/1/score?scoreTTL=10"
url := "http://podium/l/" + leaderboard + "/members/1/score?prevRank=true&scoreTTL=10"
httpmock.RegisterResponder("PATCH", url,
httpmock.NewStringResponder(200, `{ "success": true, "member": { "publicID": "123", "score": 12, "rank": 1 } }`))

Expand Down Expand Up @@ -169,7 +169,7 @@ var _ = Describe("Lib", func() {
leaderboard2 := localeLeaderboard

//mock url that should be called
url := "http://podium/m/1/scores?scoreTTL=10"
url := "http://podium/m/1/scores?prevRank=true&scoreTTL=10"
httpmock.RegisterResponder("PUT", url,
httpmock.NewStringResponder(200, `{ "success": true, "scores": [ { "leaderboardID": "brazil", "publicID": "1", "score": 1, "rank": 3, "previousRank": 1 } ] }`))

Expand Down

0 comments on commit 414c2da

Please sign in to comment.