chess_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0df6140f4d7090e9b14a2c2475c7e44d72e6691f0f103dab539646e969b37f83
4
+ data.tar.gz: 5efb7576c73f950267b4bf4dad1f123d2820dd14422cabdcedab767eeceafce7
5
+ SHA512:
6
+ metadata.gz: 695b30a9d8c428b876ec0bbba0076551717738b78c10fbb41d7c105368aaf0c35feb9336d21e7add9218afa8863c85dc7b9a5aeb8a9ee42269a827626efd61ac
7
+ data.tar.gz: 8ddfb5dc83fc08edf9ead35c675823dab755daa42c112fcd5d648949a8921be3f42075dff6c32b4719d14d53a50d4ab30433b03975b0a65a1cc7d61713045683
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-08-29
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in chess_api.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chess_api (0.1.0)
5
+ faraday (~> 1.7)
6
+ faraday_middleware (~> 1.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.4.4)
12
+ faraday (1.7.0)
13
+ faraday-em_http (~> 1.0)
14
+ faraday-em_synchrony (~> 1.0)
15
+ faraday-excon (~> 1.1)
16
+ faraday-httpclient (~> 1.0.1)
17
+ faraday-net_http (~> 1.0)
18
+ faraday-net_http_persistent (~> 1.1)
19
+ faraday-patron (~> 1.0)
20
+ faraday-rack (~> 1.0)
21
+ multipart-post (>= 1.2, < 3)
22
+ ruby2_keywords (>= 0.0.4)
23
+ faraday-em_http (1.0.0)
24
+ faraday-em_synchrony (1.0.0)
25
+ faraday-excon (1.1.0)
26
+ faraday-httpclient (1.0.1)
27
+ faraday-net_http (1.0.1)
28
+ faraday-net_http_persistent (1.2.0)
29
+ faraday-patron (1.0.0)
30
+ faraday-rack (1.0.0)
31
+ faraday_middleware (1.1.0)
32
+ faraday (~> 1.0)
33
+ multipart-post (2.1.1)
34
+ rake (13.0.6)
35
+ rspec (3.10.0)
36
+ rspec-core (~> 3.10.0)
37
+ rspec-expectations (~> 3.10.0)
38
+ rspec-mocks (~> 3.10.0)
39
+ rspec-core (3.10.1)
40
+ rspec-support (~> 3.10.0)
41
+ rspec-expectations (3.10.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.10.0)
44
+ rspec-mocks (3.10.2)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.10.0)
47
+ rspec-support (3.10.2)
48
+ ruby2_keywords (0.0.5)
49
+
50
+ PLATFORMS
51
+ universal-darwin-20
52
+
53
+ DEPENDENCIES
54
+ chess_api!
55
+ rake (~> 13.0)
56
+ rspec (~> 3.0)
57
+
58
+ BUNDLED WITH
59
+ 2.2.26
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Ryan Caldwell
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # ChessApi
2
+
3
+ `chess_api` is a gem that wraps [Chess.com's Published-Data API](https://www.chess.com/news/view/published-data-api).
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'chess_api'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install chess_api
19
+
20
+ ## Usage
21
+
22
+ To interact with the `ChessApi`, you'll first need to create a client object. You have the option of specifying a [Faraday adapter](https://lostisland.github.io/faraday/adapters/).
23
+
24
+ ```ruby
25
+ client = ChessApi::Client.new
26
+ # or
27
+ client = ChessApi::Client.new(adapter: your_adapter_here)
28
+ ```
29
+
30
+ From there, you'll be able to perform several operations:
31
+
32
+ ### Fetch a player on Chess.com by username
33
+
34
+ ```ruby
35
+ irb(main):018:0> player = client.player.retrieve(username: "GothamChess")
36
+ =>
37
+ #<ChessApi::Player:0x000000013bc41198
38
+ ...
39
+
40
+ irb(main):019:0> player.username
41
+ => "gothamchess"
42
+
43
+ irb(main):020:0> player.title
44
+ => "IM"
45
+
46
+ irb(main):021:0> player.is_streamer
47
+ => true
48
+
49
+ irb(main):022:0> player.twitch_url
50
+ => "https://twitch.tv/GothamChess"
51
+ ```
52
+
53
+ As you can see in the example above, you can access the fields returned by the Chess.com API through standard Ruby method syntax.
54
+
55
+ ### Fetching a list of players with a particular title on Chess.com
56
+
57
+ ```ruby
58
+ irb(main):001:0> client.titled_player.with_title(title_abbreviation: "WGM")
59
+ =>
60
+ #<ChessApi::Collection:0x000000013ecc78d0
61
+ @data=
62
+ [#<ChessApi::TitledPlayer:0x000000013f175d78 @attributes=#<OpenStruct username="abrahamyan-la">>,
63
+ #<ChessApi::TitledPlayer:0x000000013f175620 @attributes=#<OpenStruct username="adriananikolova">>,
64
+ #<ChessApi::TitledPlayer:0x000000013f175260 @attributes=#<OpenStruct username="advantagelucy">>,
65
+ #<ChessApi::TitledPlayer:0x000000013f174ec8 @attributes=#<OpenStruct username="ahachess">>,
66
+ ...
67
+ ]
68
+ ```
69
+
70
+ ### Fetching a player's online status on Chess.com
71
+ ```ruby
72
+ irb(main):001:0> client.player.online_status(username: "hikaru")
73
+ => false
74
+
75
+ irb(main):002:0> client.player.online_status(username: "ryanjcaldwell")
76
+ => true
77
+ ```
78
+
79
+ ### Fetching the clubs that a player belongs to on Chess.com
80
+ ```ruby
81
+ irb(main):001:0> clubs = client.player.clubs(username: "GothamChess")
82
+ =>
83
+ #<ChessApi::Collection:0x000000015dbe5998
84
+ ...
85
+
86
+ irb(main):002:0> clubs.data.map { |club| [club.name, club.joined] }
87
+ =>
88
+ [["Chess.comTV", 1533262149],
89
+ ["Hikaru Nakamura Fan Club", 1547248727],
90
+ ["Chess.com Beta", 1572489771],
91
+ ["PRO Chess League", 1547048660],
92
+ ["Streamers", 1533671931],
93
+ ["4 Player Chess", 1533746676],
94
+ ["Gotham's Mob", 1528247350],
95
+ ["Arena Kings Championship Season 5", 1575824630],
96
+ ["Marshall Chess Club Official - Match Team", 1587681476],
97
+ ["Fiona's Fight Club", 1616428995]]
98
+ ```
99
+
100
+ ## Development
101
+
102
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
103
+
104
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
105
+
106
+ ## Contributing
107
+
108
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RyanJamesCaldwell/chess_api.
109
+
110
+
111
+ ## License
112
+
113
+ The gem is available as open source under the terms of the MIT License.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "chess_api"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ client = ChessApi::Client.new
15
+
16
+ puts
17
+ puts "We've created a 'client' variable as a ChessApi::Client."
18
+ puts
19
+
20
+ require "irb"
21
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ module ChessApi
7
+ class Client
8
+ BASE_URL = 'https://api.chess.com/pub/'
9
+
10
+ def initialize(adapter: Faraday.default_adapter)
11
+ @adapter = adapter
12
+ end
13
+
14
+ def player
15
+ PlayerResource.new(self)
16
+ end
17
+
18
+ def titled_player
19
+ TitledPlayerResource.new(self)
20
+ end
21
+
22
+ def connection
23
+ @connection ||= Faraday.new do |conn|
24
+ conn.url_prefix = BASE_URL
25
+ conn.request :json
26
+ conn.response :json, content_type: 'application/json'
27
+ conn.adapter @adapter
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,16 @@
1
+ module ChessApi
2
+ class Collection
3
+ attr_reader :data
4
+
5
+ def self.from_response(response, key:, type:)
6
+ body = response.body
7
+ new(
8
+ data: body[key].map { |attrs| type.new(attrs) }
9
+ )
10
+ end
11
+
12
+ def initialize(data:)
13
+ @data = data
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module ChessApi
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'ostruct'
2
+
3
+ module ChessApi
4
+ class Object
5
+ def initialize(attributes)
6
+ @attributes = OpenStruct.new(attributes)
7
+ end
8
+
9
+ def method_missing(method, *args, &block)
10
+ attribute = @attributes.send(method, *args, &block)
11
+ attribute.is_a?(Hash) ? Object.new(attribute) : attribute
12
+ end
13
+
14
+ def respond_to_missing?(method, include_private = false)
15
+ true
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module ChessApi
2
+ class Club < Object
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module ChessApi
2
+ class Player < Object
3
+ def self.from_response(response)
4
+ new(response.body)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module ChessApi
2
+ class TitledPlayer < Object
3
+ def initialize(name)
4
+ super({username: name})
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module ChessApi
2
+ class Resource
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ private
10
+
11
+ def get_request(endpoint)
12
+ handle_response(client.connection.get(endpoint))
13
+ end
14
+
15
+ def handle_response(response)
16
+ raise ChessApi::Error, "An error happened while making your request" unless response.success?
17
+
18
+ response
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,40 @@
1
+ module ChessApi
2
+ class PlayerResource < Resource
3
+
4
+ module OnlineStatus
5
+ ONLINE = "online",
6
+ OFFLINE = "offline"
7
+ end
8
+
9
+ def retrieve(username:)
10
+ @username = username
11
+
12
+ ChessApi::Player.new(
13
+ get_request("player/#{downcased_username}").body
14
+ )
15
+ end
16
+
17
+ def online_status(username:)
18
+ @username = username
19
+ is_online = get_request("player/#{downcased_username}/is-online").body["online"]
20
+
21
+ is_online ? OnlineStatus::ONLINE : OnlineStatus::OFFLINE
22
+ end
23
+
24
+ def clubs(username:)
25
+ @username = username
26
+
27
+ ChessApi::Collection.from_response(
28
+ get_request("player/#{downcased_username}/clubs"),
29
+ key: "clubs",
30
+ type: ChessApi::Club
31
+ )
32
+ end
33
+
34
+ private
35
+
36
+ def downcased_username
37
+ @username&.downcase
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ module ChessApi
2
+ class TitledPlayerResource < Resource
3
+ def with_title(title_abbreviation:)
4
+ Collection.from_response(
5
+ get_request("titled/#{title_abbreviation&.upcase}"),
6
+ key: "players",
7
+ type: ChessApi::TitledPlayer
8
+ )
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChessApi
4
+ VERSION = "0.1.0"
5
+ end
data/lib/chess_api.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'chess_api/version'
4
+
5
+ module ChessApi
6
+ autoload :Client, 'chess_api/client'
7
+ autoload :Resource, 'chess_api/resource'
8
+ autoload :Object, 'chess_api/object'
9
+ autoload :Collection, 'chess_api/collection'
10
+ autoload :Error, 'chess_api/error'
11
+
12
+ # Objects
13
+ autoload :Player, 'chess_api/objects/player'
14
+ autoload :TitledPlayer, 'chess_api/objects/titled_player'
15
+ autoload :Club, 'chess_api/objects/club'
16
+
17
+ # Resources
18
+ autoload :PlayerResource, 'chess_api/resources/players'
19
+ autoload :TitledPlayerResource, 'chess_api/resources/titled_players'
20
+
21
+ def self.build_object(data, klass)
22
+ return data unless data.is_a?(Hash)
23
+ klass.new(data)
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chess_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Caldwell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ description: chess_api is a gem that wraps the Chess.com API.
42
+ email:
43
+ - ryanjcaldwell4@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/chess_api.rb
58
+ - lib/chess_api/client.rb
59
+ - lib/chess_api/collection.rb
60
+ - lib/chess_api/error.rb
61
+ - lib/chess_api/object.rb
62
+ - lib/chess_api/objects/club.rb
63
+ - lib/chess_api/objects/player.rb
64
+ - lib/chess_api/objects/titled_player.rb
65
+ - lib/chess_api/resource.rb
66
+ - lib/chess_api/resources/players.rb
67
+ - lib/chess_api/resources/titled_players.rb
68
+ - lib/chess_api/version.rb
69
+ homepage: https://github.com/RyanJamesCaldwell/chess_api
70
+ licenses: []
71
+ metadata:
72
+ homepage_uri: https://github.com/RyanJamesCaldwell/chess_api
73
+ source_code_uri: https://github.com/RyanJamesCaldwell/chess_api
74
+ changelog_uri: https://github.com/RyanJamesCaldwell/chess_api/blob/main/CHANGELOG.md
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.4.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.2.22
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: chess_api is a gem that wraps the Chess.com API.
94
+ test_files: []