Class: Behave::Leaderboard

Inherits:
Object
  • Object
show all
Defined in:
lib/behave.rb

Defined Under Namespace

Modules: Config

Class Method Summary collapse

Class Method Details

.create(name, referenceId, attrs = {}) ⇒ Object

Create a new leaderboard



117
118
119
120
121
# File 'lib/behave.rb', line 117

def self.create(name, referenceId, attrs={})
  attrs[:name] = name
  attrs[:reference_id] = referenceId
  Behave.api "/leaderboards", :post, body: attrs
end

.delete(leaderboardId) ⇒ Object

Remove a leaderboard



124
125
126
# File 'lib/behave.rb', line 124

def self.delete(leaderboardId)
  Behave.api "/leaderboards/#{leaderboardId}", :delete
end

.results(leaderboardId, options = {}) ⇒ Object

Fetch leaderboard results



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/behave.rb', line 129

def self.results(leaderboardId, options={})
  options[:limit] ||= 1000
  options[:max] ||= 0
  options[:page] ||= 1
  begin
    options[:offset] = (options[:page] - 1) * options[:limit]
    res = Behave.api "/leaderboards/#{leaderboardId}/results", :post, body: options
    count = res.count
    # Get total fetched since start of iteration
    total = (options[:page] - 1) * options[:limit] + count
    # If above, keep only needed elements
    res = res[0..(count - (total - options[:max]))] if options[:max] > 0 && total > options[:max]
    return res unless block_given?
    yield res, options[:page]
    options[:page] += 1
  # Continue wgile still need to fetch more results
  end while count > 0 && count == options[:limit] && (options[:max] == 0 || total < options[:max])
end