ruby-lichess 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +63 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/lichess.rb +9 -0
- data/lib/lichess/client.rb +53 -0
- data/lib/lichess/exceptions.rb +7 -0
- data/lib/lichess/games_gateway.rb +62 -0
- data/lib/lichess/users_gateway.rb +43 -0
- data/lib/lichess/version.rb +3 -0
- data/ruby-lichess.gemspec +32 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f0fd7dc34cb7fb1e7367a6ca2de585a33688cf60
|
4
|
+
data.tar.gz: fef0ebc23442f86bf7ae1d3f1fd9240b5113604f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f443d56fe5a3d729dfda3a527d87cbcf979944167c94e4fb37556ae18770d394aa910dd31ef48c15069c7940fcdd67723dbc6dcb8bd23fd4b8a6db613d7b7d3
|
7
|
+
data.tar.gz: 33b554dd1450a2f19bd9750bbbd53af1ee75bac21b6b8db028d969ece36463fbd851f86e9aea5f15db89e3a7d86ef7f211dee7b8735e2da0cc0d0f2d44480346
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ruby-lichess (0.1.0)
|
5
|
+
http (~> 4.0)
|
6
|
+
ndjson (~> 1.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
addressable (2.5.2)
|
12
|
+
public_suffix (>= 2.0.2, < 4.0)
|
13
|
+
coderay (1.1.2)
|
14
|
+
diff-lcs (1.3)
|
15
|
+
domain_name (0.5.20180417)
|
16
|
+
unf (>= 0.0.5, < 1.0.0)
|
17
|
+
dotenv (2.5.0)
|
18
|
+
http (4.0.0)
|
19
|
+
addressable (~> 2.3)
|
20
|
+
http-cookie (~> 1.0)
|
21
|
+
http-form_data (~> 2.0)
|
22
|
+
http_parser.rb (~> 0.6.0)
|
23
|
+
http-cookie (1.0.3)
|
24
|
+
domain_name (~> 0.5)
|
25
|
+
http-form_data (2.1.1)
|
26
|
+
http_parser.rb (0.6.0)
|
27
|
+
method_source (0.9.0)
|
28
|
+
ndjson (1.0.0)
|
29
|
+
pry (0.11.3)
|
30
|
+
coderay (~> 1.1.0)
|
31
|
+
method_source (~> 0.9.0)
|
32
|
+
public_suffix (3.0.3)
|
33
|
+
rake (10.5.0)
|
34
|
+
rspec (3.8.0)
|
35
|
+
rspec-core (~> 3.8.0)
|
36
|
+
rspec-expectations (~> 3.8.0)
|
37
|
+
rspec-mocks (~> 3.8.0)
|
38
|
+
rspec-core (3.8.0)
|
39
|
+
rspec-support (~> 3.8.0)
|
40
|
+
rspec-expectations (3.8.1)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.8.0)
|
43
|
+
rspec-mocks (3.8.0)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.8.0)
|
46
|
+
rspec-support (3.8.0)
|
47
|
+
unf (0.1.4)
|
48
|
+
unf_ext
|
49
|
+
unf_ext (0.0.7.5)
|
50
|
+
|
51
|
+
PLATFORMS
|
52
|
+
ruby
|
53
|
+
|
54
|
+
DEPENDENCIES
|
55
|
+
bundler (~> 1.16)
|
56
|
+
dotenv (= 2.5.0)
|
57
|
+
pry (~> 0.11)
|
58
|
+
rake (~> 10.0)
|
59
|
+
rspec (~> 3.0)
|
60
|
+
ruby-lichess!
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Ruby::Lichess
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'ruby-lichess'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ruby-lichess
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Users
|
22
|
+
|
23
|
+
*All Top Ten*
|
24
|
+
|
25
|
+
Get the top ten of all different types of games.
|
26
|
+
|
27
|
+
```
|
28
|
+
result = lichess.users.all_top_ten()
|
29
|
+
```
|
30
|
+
|
31
|
+
*Get leaderboard*
|
32
|
+
|
33
|
+
Get the leaderboard for the specified variant
|
34
|
+
|
35
|
+
```
|
36
|
+
result = lichess.users.leaderboard("blitz")
|
37
|
+
```
|
38
|
+
|
39
|
+
Can specify the number of users to get for the particular variant
|
40
|
+
|
41
|
+
```
|
42
|
+
result = lichess.users.leaderboard("blitz", 100)
|
43
|
+
```
|
44
|
+
|
45
|
+
|
46
|
+
Get users data
|
47
|
+
|
48
|
+
Get users current activity
|
49
|
+
|
50
|
+
### Games
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/omgrr/ruby-lichess.
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ruby/lichess"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/lichess.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "http"
|
3
|
+
|
4
|
+
module Lichess
|
5
|
+
class Client
|
6
|
+
attr_reader :token
|
7
|
+
|
8
|
+
def initialize(token)
|
9
|
+
@token = token
|
10
|
+
end
|
11
|
+
|
12
|
+
def users
|
13
|
+
@users_gateway ||= UsersGateway.new(self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def games
|
17
|
+
@games_gateway ||= GamesGateway.new(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(path, http_headers: {})
|
21
|
+
url = "#{base_url}#{path}"
|
22
|
+
|
23
|
+
http_headers[:accept] ||= "application/vnd.lichess.v3+json"
|
24
|
+
http_headers[:content_type] ||= "application/json"
|
25
|
+
http_headers[:authorization] ||= "Bearer #{@token}"
|
26
|
+
|
27
|
+
response = HTTP
|
28
|
+
.headers(http_headers)
|
29
|
+
.get(url)
|
30
|
+
|
31
|
+
return response
|
32
|
+
end
|
33
|
+
|
34
|
+
def post(path, body: nil, http_headers: {})
|
35
|
+
url = "#{base_url}#{path}"
|
36
|
+
|
37
|
+
http_headers[:accept] ||= "application/vnd.lichess.v3+json"
|
38
|
+
http_headers[:content_type] ||= "application/json"
|
39
|
+
|
40
|
+
response = HTTP
|
41
|
+
.headers(http_headers)
|
42
|
+
.post(url)
|
43
|
+
|
44
|
+
return response
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def base_url
|
50
|
+
"https://lichess.org"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "ndjson"
|
2
|
+
|
3
|
+
module Lichess
|
4
|
+
class GamesGateway
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
MAX_GAMES = 30
|
8
|
+
|
9
|
+
def initialize(client)
|
10
|
+
@client = client
|
11
|
+
end
|
12
|
+
|
13
|
+
def export(game_id)
|
14
|
+
path = "/game/export/#{game_id}"
|
15
|
+
|
16
|
+
http_headers = {}
|
17
|
+
http_headers[:accept] = "application/json"
|
18
|
+
http_headers[:content_type] = "application/json"
|
19
|
+
|
20
|
+
result = @client.get(path, http_headers: http_headers)
|
21
|
+
JSON.parse(result.body)
|
22
|
+
end
|
23
|
+
|
24
|
+
def users_games(user_id, options = {}, &blk)
|
25
|
+
num_games = options[:num_games] || 10
|
26
|
+
|
27
|
+
if num_games > MAX_GAMES
|
28
|
+
raise Exception::TooManyGames.new("Cannot request more than 30 games")
|
29
|
+
end
|
30
|
+
|
31
|
+
path = "/api/games/user/#{user_id}?max=#{num_games}"
|
32
|
+
path << "&ongoing=#{options[:ongoing]}" if options[:ongoing]
|
33
|
+
path << "&perfType=#{options[:perf]}" if options[:perf]
|
34
|
+
path << "&vs=#{options[:vs]}" if options[:vs]
|
35
|
+
|
36
|
+
result = @client.get(path, http_headers: ndjson_headers)
|
37
|
+
stringio = StringIO.new(result.body)
|
38
|
+
|
39
|
+
NDJSON::Parser.new(stringio).each do |json|
|
40
|
+
yield json
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def ongoing_games
|
45
|
+
path = "/api/account/playing"
|
46
|
+
|
47
|
+
result = @client.get(path)
|
48
|
+
|
49
|
+
JSON.parse(result.body)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def ndjson_headers
|
55
|
+
http_headers = {}
|
56
|
+
http_headers[:accept] = "application/x-ndjson"
|
57
|
+
http_headers[:content_type] = "application/x-ndjson"
|
58
|
+
|
59
|
+
http_headers
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module Lichess
|
4
|
+
class UsersGateway
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(username)
|
12
|
+
path = "/api/user/#{username}"
|
13
|
+
result = @client.get(path)
|
14
|
+
|
15
|
+
if result.code == 404
|
16
|
+
raise Lichess::Exception::UserNotFound.new("#{username} not found")
|
17
|
+
end
|
18
|
+
|
19
|
+
JSON.parse(result.body)
|
20
|
+
end
|
21
|
+
|
22
|
+
def activity(username)
|
23
|
+
path = "/api/user/#{username}/activity"
|
24
|
+
result = @client.get(path)
|
25
|
+
|
26
|
+
if result.body.to_s == "[]"
|
27
|
+
raise Lichess::Exception::UserNotFound.new("#{username} not found")
|
28
|
+
end
|
29
|
+
|
30
|
+
JSON.parse(result.body)
|
31
|
+
end
|
32
|
+
|
33
|
+
def all_top_ten
|
34
|
+
path = "/player"
|
35
|
+
JSON.parse(@client.get(path).body)
|
36
|
+
end
|
37
|
+
|
38
|
+
def leaderboard(variant, number_of_users = 10)
|
39
|
+
path = "/player/top/#{number_of_users}/#{variant}"
|
40
|
+
JSON.parse(@client.get(path).body)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "lichess/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ruby-lichess"
|
7
|
+
spec.version = Lichess::VERSION
|
8
|
+
spec.authors = ["omgrr"]
|
9
|
+
spec.email = ["jerwis0731@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Ruby library for lichess api}
|
12
|
+
spec.description = %q{Ruby library for lichess api}
|
13
|
+
spec.homepage = "https://github.com/omgrr/ruby-lichess.git"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "ndjson", "~> 1.0"
|
25
|
+
spec.add_dependency "http", "~> 4.0"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
28
|
+
spec.add_development_dependency "dotenv", "2.5.0"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency "pry", "~> 0.11"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-lichess
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- omgrr
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ndjson
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: http
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dotenv
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.5.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.5.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.11'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.11'
|
111
|
+
description: Ruby library for lichess api
|
112
|
+
email:
|
113
|
+
- jerwis0731@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- Gemfile
|
121
|
+
- Gemfile.lock
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- lib/lichess.rb
|
128
|
+
- lib/lichess/client.rb
|
129
|
+
- lib/lichess/exceptions.rb
|
130
|
+
- lib/lichess/games_gateway.rb
|
131
|
+
- lib/lichess/users_gateway.rb
|
132
|
+
- lib/lichess/version.rb
|
133
|
+
- ruby-lichess.gemspec
|
134
|
+
homepage: https://github.com/omgrr/ruby-lichess.git
|
135
|
+
licenses:
|
136
|
+
- MIT
|
137
|
+
metadata: {}
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.6.14.1
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Ruby library for lichess api
|
158
|
+
test_files: []
|