class Lichess::GamesGateway

Constants

MAX_GAMES
VALID_PARAMS

Attributes

client[R]

Public Class Methods

new(client) click to toggle source
# File lib/lichess/games_gateway.rb, line 13
def initialize(client)
  @client = client
end

Public Instance Methods

export(game_id) click to toggle source
# File lib/lichess/games_gateway.rb, line 17
def export(game_id)
  path = "/game/export/#{game_id}"

  http_headers = {}
  http_headers[:accept] = "application/json"
  http_headers[:content_type] = "application/json"

  result = @client.get(path, http_headers: http_headers)
  JSON.parse(result.body)
end
ongoing_games() click to toggle source
# File lib/lichess/games_gateway.rb, line 56
def ongoing_games
  path = "/api/account/playing"

  result = @client.get(path)

  JSON.parse(result.body)
end
users_games(user_id, options = {}) { |json| ... } click to toggle source
# File lib/lichess/games_gateway.rb, line 29
def users_games(user_id, options = {}, &blk)
  num_games = options[:num_games] || 10

  if num_games > MAX_GAMES
    raise Exception::TooManyGames.new("Cannot request more than 30 games")
  end

  invalid_params = options.keys.reject { |k| VALID_PARAMS[:users_games].include? k }
  raise Exception::InvalidParameter.new("#{invalid_params.join(",")} parameters were supplied, but are invalid") if invalid_params.any?

  path = "/api/games/user/#{user_id}?max=#{num_games}"

  # kept to keep backward compat
  path << "&perfType=#{options[:perf]}" if options[:perf]

  options.each do |key, value|
    path << "&#{key}=#{value}"
  end

  result = @client.get(path, http_headers: ndjson_headers)
  stringio = StringIO.new(result.body)

  NDJSON::Parser.new(stringio).each do |json|
    yield json
  end
end

Private Instance Methods

ndjson_headers() click to toggle source
# File lib/lichess/games_gateway.rb, line 66
def ndjson_headers
  http_headers = {}
  http_headers[:accept] = "application/x-ndjson"
  http_headers[:content_type] = "application/x-ndjson"

  http_headers
end