Class: Lichess::UsersGateway

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ UsersGateway

Returns a new instance of UsersGateway.



7
8
9
# File 'lib/lichess/users_gateway.rb', line 7

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#activity(username) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/lichess/users_gateway.rb', line 27

def activity(username)
  path = "/api/user/#{username}/activity"
  result = @client.get(path)

  if result.body.to_s == "[]"
    raise Lichess::Exception::UserNotFound.new("#{username} not found")
  end

  JSON.parse(result.body)
end

#all_top_tenObject



38
39
40
41
# File 'lib/lichess/users_gateway.rb', line 38

def all_top_ten
  path = "/player"
  JSON.parse(@client.get(path).body)
end

#get(usernames) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lichess/users_gateway.rb', line 11

def get(usernames)
  if usernames.is_a?(Array)
    path = "/api/users"
    result = @client.post(path, body: usernames.join(","))
  else
    path = "/api/user/#{usernames}"
    result = @client.get(path)
  end

  if result.code == 404
    raise Lichess::Exception::UserNotFound.new("#{usernames} not found")
  end

  JSON.parse(result.body)
end

#leaderboard(variant, number_of_users = 10) ⇒ Object



43
44
45
46
# File 'lib/lichess/users_gateway.rb', line 43

def leaderboard(variant, number_of_users = 10)
  path = "/player/top/#{number_of_users}/#{variant}"
  JSON.parse(@client.get(path).body)
end