Class: Lichess::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, logger: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
# File 'lib/lichess/client.rb', line 10

def initialize(token, logger: nil)
  @token = token
  @logger = logger || Logger.new(STDOUT)
  @logger.level = Logger::INFO
  @http = HTTP.use(logging: {logger: @logger})
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/lichess/client.rb', line 8

def logger
  @logger
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#gamesObject



21
22
23
# File 'lib/lichess/client.rb', line 21

def games
  @games_gateway ||= GamesGateway.new(self)
end

#get(path, http_headers: {}) ⇒ Object



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

def get(path, http_headers: {})
  url = "#{base_url}#{path}"

  http_headers[:accept] ||= "application/vnd.lichess.v3+json"
  http_headers[:content_type] ||= "application/json"
  http_headers[:authorization] ||= "Bearer #{@token}"

  response = @http
    .headers(http_headers)
    .get(url)

  return response
end

#post(path, body: nil, http_headers: {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lichess/client.rb', line 39

def post(path, body: nil, http_headers: {})
  url = "#{base_url}#{path}"

  http_headers[:accept] ||= "application/vnd.lichess.v3+json"
  http_headers[:content_type] ||= "application/json"

  response = @http
    .headers(http_headers)
    .post(url, body: body)

  return response
end

#usersObject



17
18
19
# File 'lib/lichess/client.rb', line 17

def users
  @users_gateway ||= UsersGateway.new(self)
end