module SRL

Constants

API

SpeedRunsLive API URL. :nodoc:

RELEASE

The current version of srl-api.

Public Class Methods

current_races → obj click to toggle source

Return an array of PastRace objects for completed races.

You may filter the results by providing a `player` or `game` argument, to limit results to a specific player or game abbreviation.

# To fetch only the six million LTTP Rando races.
completed_races(game: 'alttphacks') 

# To only retrieve Edgeworth's FF Randomizer races.
completed_races(player: 'Edgeworth', game: 'ffhacks')

Results are paginated at `page_size` records per page, starting at 1. An upper limit to this number has not been tested, though I'd recommend that you not be a twat and limit your requests to something that will not murder the server.

# File lib/srl/api.rb, line 82
def completed_races(args = {})
  query =
    Query.new('pastraces', PastRace, args.merge({ pkey: 'pastraces' })) 

  return query.page unless block_given?

  yield query
end
Also aliased as: past_races
current_races → array click to toggle source

Return an array of Race objects for races currently being run or set up.

# File lib/srl/api.rb, line 59
def current_races
  SRL::Utils.collection(query('races').fetch('races'), Race)
end
game(abbreviation) → obj click to toggle source

Fetch usage data about a specific game, identified by its abbreviation.

# File lib/srl/api.rb, line 24
def game(abbrev)
  res = query("stat", game: abbrev)
  game = Game.from_hash(res.fetch('game'))
  game.stats = res.fetch('stats')

  game
end
leaderboard(abbrev) → obj click to toggle source

Fetch the leaderboard for a specific game, identified by its abbreviation.

# File lib/srl/api.rb, line 48
def leaderboard(abbrev)
  SRL::Utils.collection(
    query("leaderboard/#{abbrev}").fetch('leaders'),
    Player
  )
end
current_races → obj
Alias for: completed_races
player(name) → obj click to toggle source

Fetch a player's public profile, by name. Raises a NameError if the given name does not exist.

# File lib/srl/api.rb, line 36
def player(name)
  player = Player.from_hash(query("players/#{name}"))

  raise NameError, "Player '#{name}' not found." unless player.exists?

  player
end
release() click to toggle source

Return the current release version as a dotted string.

# File lib/srl.rb, line 8
def self.release
  RELEASE
end

Private Class Methods

query(url, params = {}) click to toggle source

Return a hash with the results of a query to the SRL API. :nodoc:

# File lib/srl/api.rb, line 98
def query(url, params = {})
  url = URI([API, url].join) # *hiss* "URI" has been wrong for years!
  url.query = URI.encode_www_form(params) unless params.empty?

  res = Net::HTTP.get_response(url)
  raise NetworkError, res unless res.is_a?(Net::HTTPSuccess)

  JSON.parse(res.body)
end