Sha256: 03e6a08854b4d95ac4b25c80dddca418eebb15b2ba0b1c9ff31771c7be2f08c4

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require "net/http"
require "http"
require "logger"

module Lichess
  class Client
    attr_reader :token
    attr_accessor :logger

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

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

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

    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

    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)

      return response
    end

    private

    def base_url
      "https://lichess.org"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-lichess-0.2.0 lib/lichess/client.rb