chgk_rating 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile +3 -1
- data/LICENSE +1 -1
- data/README.md +4 -4
- data/Rakefile +4 -2
- data/chgk_rating.gemspec +19 -12
- data/lib/chgk_rating/attribute_mappings.rb +107 -108
- data/lib/chgk_rating/chgk_object.rb +5 -1
- data/lib/chgk_rating/client.rb +4 -2
- data/lib/chgk_rating/collections/base.rb +20 -15
- data/lib/chgk_rating/collections/players.rb +3 -1
- data/lib/chgk_rating/collections/ratings/player_ratings.rb +3 -1
- data/lib/chgk_rating/collections/ratings/team_ratings.rb +3 -1
- data/lib/chgk_rating/collections/recaps.rb +4 -2
- data/lib/chgk_rating/collections/teams.rb +3 -1
- data/lib/chgk_rating/collections/tournaments/player_tournaments.rb +12 -8
- data/lib/chgk_rating/collections/tournaments/tournament_team_players.rb +3 -1
- data/lib/chgk_rating/collections/tournaments/tournament_team_results.rb +3 -1
- data/lib/chgk_rating/collections/tournaments/tournament_teams.rb +4 -1
- data/lib/chgk_rating/collections/tournaments/tournaments.rb +12 -8
- data/lib/chgk_rating/concerns/searching.rb +10 -9
- data/lib/chgk_rating/connection.rb +10 -8
- data/lib/chgk_rating/error.rb +7 -5
- data/lib/chgk_rating/models/base.rb +23 -16
- data/lib/chgk_rating/models/player.rb +3 -1
- data/lib/chgk_rating/models/rating/player_rating.rb +3 -1
- data/lib/chgk_rating/models/rating/rating.rb +3 -1
- data/lib/chgk_rating/models/rating/team_rating.rb +3 -1
- data/lib/chgk_rating/models/recap.rb +3 -1
- data/lib/chgk_rating/models/team.rb +3 -1
- data/lib/chgk_rating/models/tournament/player_tournament.rb +3 -1
- data/lib/chgk_rating/models/tournament/tournament.rb +3 -1
- data/lib/chgk_rating/models/tournament/tournament_team.rb +3 -1
- data/lib/chgk_rating/models/tournament/tournament_team_player.rb +3 -1
- data/lib/chgk_rating/models/tournament/tournament_team_result.rb +3 -1
- data/lib/chgk_rating/request.rb +10 -9
- data/lib/chgk_rating/utils/snakecase.rb +9 -7
- data/lib/chgk_rating/utils/transformations.rb +42 -36
- data/lib/chgk_rating/version.rb +4 -2
- data/lib/chgk_rating.rb +3 -1
- data/lib/ext/date.rb +6 -8
- data/lib/ext/date_time.rb +4 -2
- data/lib/ext/uri.rb +6 -6
- metadata +61 -80
- data/spec/lib/chgk_rating/client_spec.rb +0 -247
- data/spec/lib/chgk_rating/collections/player_ratings_spec.rb +0 -15
- data/spec/lib/chgk_rating/collections/players_spec.rb +0 -45
- data/spec/lib/chgk_rating/collections/recaps_spec.rb +0 -21
- data/spec/lib/chgk_rating/collections/team_ratings_spec.rb +0 -25
- data/spec/lib/chgk_rating/collections/teams_spec.rb +0 -42
- data/spec/lib/chgk_rating/collections/tournaments/player_tournaments_spec.rb +0 -38
- data/spec/lib/chgk_rating/collections/tournaments/tournament_team_players_spec.rb +0 -18
- data/spec/lib/chgk_rating/collections/tournaments/tournament_team_results_spec.rb +0 -17
- data/spec/lib/chgk_rating/collections/tournaments/tournament_teams_spec.rb +0 -58
- data/spec/lib/chgk_rating/collections/tournaments/tournaments_spec.rb +0 -62
- data/spec/lib/chgk_rating/models/base_spec.rb +0 -34
- data/spec/lib/chgk_rating/models/player_rating_spec.rb +0 -22
- data/spec/lib/chgk_rating/models/player_spec.rb +0 -62
- data/spec/lib/chgk_rating/models/recap_spec.rb +0 -35
- data/spec/lib/chgk_rating/models/team_rating_spec.rb +0 -27
- data/spec/lib/chgk_rating/models/team_spec.rb +0 -97
- data/spec/lib/chgk_rating/models/tournament/tournament_spec.rb +0 -95
- data/spec/lib/chgk_rating/models/tournament/tournament_team_player_spec.rb +0 -21
- data/spec/lib/chgk_rating/models/tournament/tournament_team_result_spec.rb +0 -19
- data/spec/lib/chgk_rating/models/tournament/tournament_team_spec.rb +0 -36
- data/spec/lib/chgk_rating/utils/snakecase_spec.rb +0 -12
- data/spec/lib/chgk_rating/utils/transformations_spec.rb +0 -16
- data/spec/lib/chgk_rating_spec.rb +0 -5
- data/spec/lib/ext/date_spec.rb +0 -7
- data/spec/lib/ext/date_time_spec.rb +0 -11
- data/spec/lib/ext/uri_spec.rb +0 -7
- data/spec/spec_helper.rb +0 -15
- data/spec/support/shared_examples.rb +0 -63
- data/spec/support/test_client.rb +0 -6
- data/spec/support/vcr.rb +0 -11
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Collections
|
3
5
|
class TournamentTeams < Base
|
4
6
|
attr_reader :tournament
|
7
|
+
|
5
8
|
def initialize(params = {})
|
6
9
|
@tournament = build_model params[:tournament], ChgkRating::Models::Tournament
|
7
10
|
|
@@ -19,4 +22,4 @@ module ChgkRating
|
|
19
22
|
end
|
20
23
|
end
|
21
24
|
end
|
22
|
-
end
|
25
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Collections
|
3
5
|
class Tournaments < Base
|
@@ -7,18 +9,18 @@ module ChgkRating
|
|
7
9
|
@team = build_model params[:team]
|
8
10
|
|
9
11
|
@season_id = params[:season_id]
|
10
|
-
params
|
12
|
+
params[:lazy] = true if @team || @season_id
|
11
13
|
super
|
12
14
|
end
|
13
15
|
|
14
16
|
def revert_to_hash(key, values)
|
15
17
|
[
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
key,
|
19
|
+
{
|
20
|
+
'idteam' => @team&.id.to_s,
|
21
|
+
'idseason' => key,
|
22
|
+
'tournaments' => values.map(&:to_h)
|
23
|
+
}
|
22
24
|
]
|
23
25
|
end
|
24
26
|
|
@@ -39,10 +41,12 @@ module ChgkRating
|
|
39
41
|
def api_path
|
40
42
|
path = 'tournaments'
|
41
43
|
return path unless @team
|
44
|
+
|
42
45
|
path = "teams/#{@team.id}/#{path}"
|
43
46
|
return path unless @season_id
|
47
|
+
|
44
48
|
path + "/#{@season_id}"
|
45
49
|
end
|
46
50
|
end
|
47
51
|
end
|
48
|
-
end
|
52
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Concerns
|
3
5
|
module Searching
|
@@ -5,21 +7,20 @@ module ChgkRating
|
|
5
7
|
# Creates a Search child class of a given class that changes the URI where the
|
6
8
|
# GET request should be sent
|
7
9
|
def self.included(klass)
|
8
|
-
klass.const_set(
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
)
|
10
|
+
klass.const_set(:Search,
|
11
|
+
Class.new(klass) do |*_args|
|
12
|
+
def api_path
|
13
|
+
"#{super}/search"
|
14
|
+
end
|
15
|
+
end)
|
15
16
|
|
16
17
|
# The actual method to perform searching
|
17
18
|
# Instantiates a Search class with the given search params
|
18
19
|
# and send a GET request to the proper URI (defined above)
|
19
20
|
klass.define_singleton_method :search do |params|
|
20
|
-
klass.const_get(
|
21
|
+
klass.const_get(:Search).new(params)
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
25
|
-
end
|
26
|
+
end
|
@@ -1,20 +1,22 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday/follow_redirects'
|
2
4
|
|
3
5
|
module ChgkRating
|
4
6
|
module Connection
|
5
|
-
BASE_URL = 'http://rating.chgk.info/api'
|
7
|
+
BASE_URL = 'http://rating.chgk.info/api'
|
6
8
|
|
7
9
|
def connection
|
8
10
|
options = {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
headers: {
|
12
|
+
accept: 'application/json',
|
13
|
+
user_agent: "chgk_rating ruby gem/#{ChgkRating::VERSION}"
|
14
|
+
},
|
15
|
+
url: "#{BASE_URL}/"
|
14
16
|
}
|
15
17
|
|
16
18
|
Faraday.new options do |faraday|
|
17
|
-
faraday.use
|
19
|
+
faraday.use Faraday::FollowRedirects::Middleware
|
18
20
|
faraday.adapter Faraday.default_adapter
|
19
21
|
end
|
20
22
|
end
|
data/lib/chgk_rating/error.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
class Error < StandardError
|
3
5
|
ClientError = Class.new(self)
|
@@ -26,10 +28,10 @@ module ChgkRating
|
|
26
28
|
NotImplemented = Class.new(ServerError)
|
27
29
|
|
28
30
|
ERRORS = {
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
}
|
31
|
+
400 => ChgkRating::Error::BadRequest,
|
32
|
+
404 => ChgkRating::Error::NotFound,
|
33
|
+
501 => ChgkRating::Error::NotImplemented
|
34
|
+
}.freeze
|
33
35
|
|
34
36
|
class << self
|
35
37
|
# Create a new error from an HTTP response
|
@@ -43,4 +45,4 @@ module ChgkRating
|
|
43
45
|
super(message)
|
44
46
|
end
|
45
47
|
end
|
46
|
-
end
|
48
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Models
|
3
5
|
class Base < ChgkObject
|
@@ -24,7 +26,8 @@ module ChgkRating
|
|
24
26
|
# Set `force` to reload data even if it is already present.
|
25
27
|
def eager_load!(force = false)
|
26
28
|
return unless @lazy || force
|
27
|
-
|
29
|
+
|
30
|
+
extract_from raw_by(id)
|
28
31
|
@lazy = false
|
29
32
|
self
|
30
33
|
end
|
@@ -36,29 +39,31 @@ module ChgkRating
|
|
36
39
|
end
|
37
40
|
|
38
41
|
def self.no_lazy_support!
|
39
|
-
|
42
|
+
const_set :NO_LAZY_SUPPORT, true
|
40
43
|
undef_method :lazy
|
41
44
|
end
|
42
45
|
|
43
46
|
def to_h
|
44
|
-
self.class.attribute_mapping.
|
45
|
-
data =
|
46
|
-
data = mapping[:transform_down].call(data) if mapping.
|
47
|
-
memo[
|
48
|
-
memo
|
47
|
+
self.class.attribute_mapping.each_with_object({}) do |(attr, mapping), memo|
|
48
|
+
data = send attr
|
49
|
+
data = mapping[:transform_down].call(data) if mapping.key?(:transform_down)
|
50
|
+
memo[mapping[:raw_name]] = data
|
49
51
|
end
|
50
52
|
end
|
51
53
|
|
52
|
-
|
54
|
+
class << self
|
55
|
+
# Grab the attribute mapping for the class and its superclass
|
56
|
+
# (as superclass may present common mappings for multiple classes)
|
57
|
+
def attribute_mapping
|
58
|
+
return nil unless name
|
53
59
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
return nil unless self.name
|
58
|
-
ChgkRating::AttributeMappings.find(self.name).
|
59
|
-
merge(ChgkRating::AttributeMappings.find(self.superclass.name))
|
60
|
+
ChgkRating::AttributeMappings.find(name).
|
61
|
+
merge(ChgkRating::AttributeMappings.find(superclass.name))
|
62
|
+
end
|
60
63
|
end
|
61
64
|
|
65
|
+
private
|
66
|
+
|
62
67
|
def lazy_load?(params)
|
63
68
|
self.class.const_defined?(:NO_LAZY_SUPPORT) ? false : params[:lazy]
|
64
69
|
end
|
@@ -85,16 +90,18 @@ module ChgkRating
|
|
85
90
|
self.class.attribute_mapping.each do |attr, mapping|
|
86
91
|
data = get_data_from raw_data, attr, mapping
|
87
92
|
next unless data
|
93
|
+
|
88
94
|
instance_variable_set "@#{attr}", transform_up(data, mapping)
|
89
95
|
end
|
90
96
|
end
|
91
97
|
|
92
98
|
def get_data_from(raw, attr, mapping)
|
93
|
-
raw.is_a?(self.class) ? raw.send(attr) : raw[
|
99
|
+
raw.is_a?(self.class) ? raw.send(attr) : raw[mapping[:raw_name]]
|
94
100
|
end
|
95
101
|
|
96
102
|
def transform_up(data, mapping)
|
97
|
-
return data unless mapping.
|
103
|
+
return data unless mapping.key?(:transform_up)
|
104
|
+
|
98
105
|
mapping[:transform_up].call(data)
|
99
106
|
end
|
100
107
|
end
|
data/lib/chgk_rating/request.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Request
|
3
5
|
include ChgkRating::Connection
|
@@ -16,17 +18,16 @@ module ChgkRating
|
|
16
18
|
end
|
17
19
|
|
18
20
|
def respond(response)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
21
|
+
body = MultiJson.load response.body
|
22
|
+
raise MultiJson::ParseError if body.respond_to?(:has_key?) && body.key?('error')
|
23
|
+
|
24
|
+
body
|
25
|
+
rescue MultiJson::ParseError
|
26
|
+
respond_with_error response.status, response.body
|
26
27
|
end
|
27
28
|
|
28
29
|
def respond_with_error(code, body)
|
29
|
-
|
30
|
+
raise ChgkRating::Error::ERRORS[code].from_response(body)
|
30
31
|
end
|
31
32
|
end
|
32
|
-
end
|
33
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Initial code taken from Facets gem by Rubyworks
|
2
4
|
# https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
|
3
5
|
|
@@ -6,11 +8,11 @@ class String
|
|
6
8
|
# replaced by underscores.
|
7
9
|
def snakecase_upcase
|
8
10
|
split('::').last.
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
12
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
13
|
+
tr('-', '_').
|
14
|
+
gsub(/\s/, '_').
|
15
|
+
gsub(/__+/, '_').
|
16
|
+
upcase
|
15
17
|
end
|
16
|
-
end
|
18
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Utils
|
3
5
|
module Transformations
|
@@ -6,10 +8,10 @@ module ChgkRating
|
|
6
8
|
up = 'integer' if up.nil? || up.empty?
|
7
9
|
down = 'string' if down.nil? || down.empty?
|
8
10
|
|
9
|
-
%i
|
11
|
+
%i[up down].reduce({}) do |result, t|
|
10
12
|
current_transformer = binding.local_variable_get t
|
11
13
|
result.merge({
|
12
|
-
|
14
|
+
"transform_#{t}".to_sym => send(current_transformer)
|
13
15
|
})
|
14
16
|
end
|
15
17
|
end
|
@@ -24,10 +26,12 @@ module ChgkRating
|
|
24
26
|
private
|
25
27
|
|
26
28
|
def chgk_object(namespace, type = 'Models')
|
27
|
-
|
28
|
-
opts = type == 'Models'
|
29
|
-
|
30
|
-
|
29
|
+
lambda do |d|
|
30
|
+
opts = if type == 'Models'
|
31
|
+
[d, {lazy: true}]
|
32
|
+
else
|
33
|
+
[{collection: d, lazy: true}]
|
34
|
+
end
|
31
35
|
|
32
36
|
Module.const_get("ChgkRating::#{type}::#{namespace}").new(*opts)
|
33
37
|
end
|
@@ -42,43 +46,45 @@ module ChgkRating
|
|
42
46
|
end
|
43
47
|
|
44
48
|
def to_star(method = :to_s, iterate = false)
|
45
|
-
|
46
|
-
iterate
|
47
|
-
|
48
|
-
|
49
|
+
lambda do |d|
|
50
|
+
if iterate
|
51
|
+
d.map { |obj| obj.send method }
|
52
|
+
else
|
53
|
+
d.send(method)
|
54
|
+
end
|
49
55
|
end
|
50
56
|
end
|
51
57
|
end
|
52
58
|
|
53
59
|
TRANSFORMERS = {
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
60
|
+
string: to_star,
|
61
|
+
integer: to_star(:to_i),
|
62
|
+
float: to_star(:to_f),
|
63
|
+
id: to_star(:id),
|
64
|
+
ids: to_star(:id, true),
|
65
|
+
sym: to_star(:to_sym),
|
66
|
+
strdate: to_star(:to_s_chgk),
|
67
|
+
uri: ->(d) { URI.parse_safely d },
|
68
|
+
boolean: to_boolean,
|
69
|
+
binboolean: to_binary_boolean,
|
70
|
+
date: ->(d) { Date.parse_safely d },
|
71
|
+
datetime: ->(d) { DateTime.parse_safely d },
|
72
|
+
splitboolean: lambda do |d|
|
73
|
+
d&.split('')&.map { |result| to_boolean.call(result) }
|
74
|
+
end,
|
75
|
+
arraystrboolean: lambda do |d|
|
76
|
+
d&.map { |result| to_binary_boolean.call(result) }
|
77
|
+
end,
|
78
|
+
arrayboolean: lambda do |d|
|
79
|
+
d&.map { |result| to_boolean.call(result) }
|
80
|
+
end,
|
81
|
+
team: chgk_object('Team'),
|
82
|
+
tournament: chgk_object('Tournament'),
|
83
|
+
player: chgk_object('Player'),
|
84
|
+
players: chgk_object('Players', 'Collections')
|
79
85
|
}.freeze
|
80
86
|
|
81
87
|
load_transformers!
|
82
88
|
end
|
83
89
|
end
|
84
|
-
end
|
90
|
+
end
|
data/lib/chgk_rating/version.rb
CHANGED
data/lib/chgk_rating.rb
CHANGED
data/lib/ext/date.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Date
|
2
4
|
def self.parse_safely(raw_date_time)
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
nil
|
7
|
-
rescue TypeError
|
8
|
-
nil
|
9
|
-
end
|
5
|
+
parse raw_date_time
|
6
|
+
rescue ArgumentError, TypeError
|
7
|
+
nil
|
10
8
|
end
|
11
|
-
end
|
9
|
+
end
|
data/lib/ext/date_time.rb
CHANGED