Class: Lichess::GamesGateway

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

Constant Summary collapse

MAX_GAMES =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ GamesGateway

Returns a new instance of GamesGateway.



9
10
11
# File 'lib/lichess/games_gateway.rb', line 9

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/lichess/games_gateway.rb', line 5

def client
  @client
end

Instance Method Details

#export(game_id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/lichess/games_gateway.rb', line 13

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_gamesObject



44
45
46
47
48
49
50
# File 'lib/lichess/games_gateway.rb', line 44

def ongoing_games
  path = "/api/account/playing"

  result = @client.get(path)

  JSON.parse(result.body)
end

#users_games(user_id, options = {}, &blk) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lichess/games_gateway.rb', line 24

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

  path = "/api/games/user/#{user_id}?max=#{num_games}"
  path << "&ongoing=#{options[:ongoing]}" if options[:ongoing]
  path << "&perfType=#{options[:perf]}" if options[:perf]
  path << "&vs=#{options[:vs]}" if options[:vs]

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

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