espn_pub 0.1.6 → 0.1.8
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 +4 -4
- data/lib/espn_pub/entities/base.rb +4 -0
- data/lib/espn_pub/entities/game.rb +16 -2
- data/lib/espn_pub/entities/league.rb +31 -12
- data/lib/espn_pub/entities/player.rb +7 -7
- data/lib/espn_pub/entities/team.rb +9 -9
- data/lib/espn_pub/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2313dc03f24eb1a9ece5883a51a186acd8497790d2d6b2b4291cdaa997c4c977
|
|
4
|
+
data.tar.gz: f2238e3abe174ade734ac70e183d2741e9f11f5fd49a96d6a77000b82086cd75
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 32011085498d278e9076c1332e53bc6aee395345f30adc547ded45705713e7e358e2c3da8663f3abd0c1ec7f179fc6b4f703f4d84e27c4f6f9ccc3dd79d4eb3f
|
|
7
|
+
data.tar.gz: 2b4bbf38c0aa43eeee07b4848c7eaaf9bde8d167cacb2890c657ea668ebd5132042a51b815c5485eaece594d7f8a3cab89ae8290f3eec5c99a6e29dce15de9f6
|
|
@@ -4,7 +4,19 @@ module EspnPub
|
|
|
4
4
|
module Entities
|
|
5
5
|
# Represents a game between two teams.
|
|
6
6
|
class Game < Base
|
|
7
|
-
|
|
7
|
+
module Type
|
|
8
|
+
PRESEASON = 'preseason'
|
|
9
|
+
REGULAR = 'regular'
|
|
10
|
+
POSTSEASON = 'postseason'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
GAME_TYPES = {
|
|
14
|
+
1 => Type::PRESEASON,
|
|
15
|
+
2 => Type::REGULAR,
|
|
16
|
+
3 => Type::POSTSEASON
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
attr_reader :id, :home_team, :away_team, :date, :type
|
|
8
20
|
|
|
9
21
|
# Initialize a Game entity.
|
|
10
22
|
#
|
|
@@ -12,11 +24,13 @@ module EspnPub
|
|
|
12
24
|
# @param home_team [EspnPub::Entities::Team] The home team.
|
|
13
25
|
# @param away_team [EspnPub::Entities::Team] The away team.
|
|
14
26
|
# @param date [DateTime] The scheduled game date.
|
|
15
|
-
|
|
27
|
+
# @param type [String] The type of the game.
|
|
28
|
+
def initialize(id:, home_team:, away_team:, date:, type: nil)
|
|
16
29
|
@id = id
|
|
17
30
|
@home_team = home_team
|
|
18
31
|
@away_team = away_team
|
|
19
32
|
@date = date
|
|
33
|
+
@type = type
|
|
20
34
|
super()
|
|
21
35
|
end
|
|
22
36
|
end
|
|
@@ -20,13 +20,31 @@ module EspnPub
|
|
|
20
20
|
'nfl' => 'football'
|
|
21
21
|
}.freeze
|
|
22
22
|
|
|
23
|
-
attr_reader :
|
|
23
|
+
attr_reader :league_name
|
|
24
|
+
|
|
25
|
+
def self.fetch_season_details(league_name, season_year)
|
|
26
|
+
raise ArgumentError, "Unknown league name: #{league_name}" unless NAME_TO_SPORT.key?(league_name)
|
|
27
|
+
raise ArgumentError, 'Season year must be an integer' unless season_year.to_i.positive?
|
|
28
|
+
|
|
29
|
+
path = format GAMES_PATH, EspnPub::Client::API_VERSION, NAME_TO_SPORT[league_name], league_name
|
|
30
|
+
path += "?dates=#{Time.new(season_year).strftime('%Y%m%d')}"
|
|
31
|
+
resp = EspnPub::Client.new.send_request(path)
|
|
32
|
+
season = resp.dig('leagues', 0, 'season')
|
|
33
|
+
|
|
34
|
+
return {} unless season
|
|
35
|
+
|
|
36
|
+
{
|
|
37
|
+
year: season['year'],
|
|
38
|
+
start_date: Date.parse(season['startDate']),
|
|
39
|
+
end_date: Date.parse(season['endDate'])
|
|
40
|
+
}
|
|
41
|
+
end
|
|
24
42
|
|
|
25
43
|
# Initialize a League instance.
|
|
26
44
|
#
|
|
27
|
-
# @param
|
|
28
|
-
def initialize(
|
|
29
|
-
@
|
|
45
|
+
# @param league_name [String] The league identifier string.
|
|
46
|
+
def initialize(league_name)
|
|
47
|
+
@league_name = normalize_name(league_name)
|
|
30
48
|
super()
|
|
31
49
|
end
|
|
32
50
|
|
|
@@ -36,7 +54,7 @@ module EspnPub
|
|
|
36
54
|
def teams
|
|
37
55
|
unless defined?(@teams)
|
|
38
56
|
begin
|
|
39
|
-
path = format TEAMS_PATH, client.version, sport,
|
|
57
|
+
path = format TEAMS_PATH, client.version, sport, league_name
|
|
40
58
|
teams_resp = client.send_request(path)
|
|
41
59
|
@teams = (teams_resp.dig('sports', 0, 'leagues', 0, 'teams') || []).map do |team_data|
|
|
42
60
|
EspnPub::Entities::Team.new(
|
|
@@ -45,11 +63,11 @@ module EspnPub
|
|
|
45
63
|
location: team_data.dig('team', 'location'),
|
|
46
64
|
abbreviation: team_data.dig('team', 'abbreviation'),
|
|
47
65
|
sport: sport,
|
|
48
|
-
|
|
66
|
+
league_name: league_name
|
|
49
67
|
)
|
|
50
68
|
end
|
|
51
69
|
rescue Client::UnexpectedResponseCodeError => e
|
|
52
|
-
warn "Failed to fetch teams for league #{
|
|
70
|
+
warn "Failed to fetch teams for league #{league_name}: #{e.message}"
|
|
53
71
|
return []
|
|
54
72
|
end
|
|
55
73
|
end
|
|
@@ -62,27 +80,28 @@ module EspnPub
|
|
|
62
80
|
# @param date [Date, DateTime, nil] An optional date to filter games.
|
|
63
81
|
# @return [Array<EspnPub::Entities::Game>]
|
|
64
82
|
def games(date: nil)
|
|
65
|
-
path = format GAMES_PATH, client.version, sport,
|
|
83
|
+
path = format GAMES_PATH, client.version, sport, league_name
|
|
66
84
|
path += "?dates=#{date.strftime('%Y%m%d')}" if date
|
|
67
85
|
games_resp = client.send_request(path)
|
|
68
86
|
(games_resp['events'] || []).map do |game_data|
|
|
69
87
|
EspnPub::Entities::Game.new(
|
|
70
88
|
id: game_data['id'],
|
|
89
|
+
type: Game::GAME_TYPES[game_data.dig('season', 'type')],
|
|
71
90
|
home_team: EspnPub::Entities::Team.fetch_by_id(
|
|
72
91
|
id: game_data.dig('competitions', 0, 'competitors', 0, 'id'),
|
|
73
92
|
sport: sport,
|
|
74
|
-
|
|
93
|
+
league_name: league_name
|
|
75
94
|
),
|
|
76
95
|
away_team: EspnPub::Entities::Team.fetch_by_id(
|
|
77
96
|
id: game_data.dig('competitions', 0, 'competitors', 1, 'id'),
|
|
78
97
|
sport: sport,
|
|
79
|
-
|
|
98
|
+
league_name: league_name
|
|
80
99
|
),
|
|
81
100
|
date: DateTime.parse(game_data['date'])
|
|
82
101
|
)
|
|
83
102
|
end
|
|
84
103
|
rescue Client::UnexpectedResponseCodeError => e
|
|
85
|
-
warn "Failed to fetch games for league #{
|
|
104
|
+
warn "Failed to fetch games for league #{league_name}: #{e.message}"
|
|
86
105
|
[]
|
|
87
106
|
end
|
|
88
107
|
|
|
@@ -90,7 +109,7 @@ module EspnPub
|
|
|
90
109
|
#
|
|
91
110
|
# @return [String, nil] The sport name or nil when unknown.
|
|
92
111
|
def sport
|
|
93
|
-
NAME_TO_SPORT[
|
|
112
|
+
NAME_TO_SPORT[league_name]
|
|
94
113
|
end
|
|
95
114
|
end
|
|
96
115
|
end
|
|
@@ -10,7 +10,7 @@ module EspnPub
|
|
|
10
10
|
|
|
11
11
|
attr_reader :id,
|
|
12
12
|
:sport,
|
|
13
|
-
:
|
|
13
|
+
:league_name,
|
|
14
14
|
:first_name,
|
|
15
15
|
:last_name,
|
|
16
16
|
:position,
|
|
@@ -27,7 +27,7 @@ module EspnPub
|
|
|
27
27
|
#
|
|
28
28
|
# @param id [String] The player identifier.
|
|
29
29
|
# @param sport [String] The sport name.
|
|
30
|
-
# @param
|
|
30
|
+
# @param league_name [String] The league identifier.
|
|
31
31
|
# @param first_name [String, nil] The player's first name.
|
|
32
32
|
# @param last_name [String, nil] The player's last name.
|
|
33
33
|
# @param position [String, nil] The player's position abbreviation.
|
|
@@ -39,11 +39,11 @@ module EspnPub
|
|
|
39
39
|
# @param height [String, nil] The player's listed height.
|
|
40
40
|
# @param weight [String, nil] The player's listed weight.
|
|
41
41
|
# @param debut_year [Integer, nil] The year the player made their debut.
|
|
42
|
-
def initialize(id:, sport:,
|
|
42
|
+
def initialize(id:, sport:, league_name:, first_name: nil, last_name: nil, position: nil, team_id: nil,
|
|
43
43
|
date_of_birth: nil, birth_city: nil, birth_state: nil, birth_country: nil, height: nil, weight: nil, debut_year: nil)
|
|
44
44
|
@id = id
|
|
45
45
|
@sport = sport
|
|
46
|
-
@
|
|
46
|
+
@league_name = league_name
|
|
47
47
|
@first_name = first_name
|
|
48
48
|
@last_name = last_name
|
|
49
49
|
@position = position
|
|
@@ -64,9 +64,9 @@ module EspnPub
|
|
|
64
64
|
# @param sport [String] The sport name.
|
|
65
65
|
# @param league [String] The league identifier.
|
|
66
66
|
# @return [Player, nil] The player, or nil when the request fails or data is missing.
|
|
67
|
-
def self.fetch_by_id(id:, sport:,
|
|
67
|
+
def self.fetch_by_id(id:, sport:, league_name:)
|
|
68
68
|
client = EspnPub::Client.new
|
|
69
|
-
path = format ATHLETE_PATH, sport,
|
|
69
|
+
path = format ATHLETE_PATH, sport, league_name, id
|
|
70
70
|
begin
|
|
71
71
|
athlete_data = client.send_request(path)['athlete']
|
|
72
72
|
rescue Client::UnexpectedResponseCodeError => e
|
|
@@ -79,7 +79,7 @@ module EspnPub
|
|
|
79
79
|
new(
|
|
80
80
|
id: athlete_data['id'],
|
|
81
81
|
sport: sport,
|
|
82
|
-
|
|
82
|
+
league_name: league_name,
|
|
83
83
|
first_name: athlete_data['firstName'],
|
|
84
84
|
last_name: athlete_data['lastName'],
|
|
85
85
|
position: athlete_data.dig('position', 'abbreviation'),
|
|
@@ -12,7 +12,7 @@ module EspnPub
|
|
|
12
12
|
:location,
|
|
13
13
|
:abbreviation,
|
|
14
14
|
:sport,
|
|
15
|
-
:
|
|
15
|
+
:league_name,
|
|
16
16
|
:venue
|
|
17
17
|
|
|
18
18
|
# Initialize a Team entity.
|
|
@@ -22,24 +22,24 @@ module EspnPub
|
|
|
22
22
|
# @param location [String] The team location.
|
|
23
23
|
# @param abbreviation [String] The team abbreviation.
|
|
24
24
|
# @param sport [String] The sport name.
|
|
25
|
-
# @param
|
|
25
|
+
# @param league_name [String] The league identifier.
|
|
26
26
|
# @param venue [EspnPub::Entities::Venue, nil] The team's home venue.
|
|
27
|
-
def initialize(id:, name:, location:, abbreviation:, sport:,
|
|
27
|
+
def initialize(id:, name:, location:, abbreviation:, sport:, league_name:, venue: nil)
|
|
28
28
|
@id = id
|
|
29
29
|
@name = name
|
|
30
30
|
@location = location
|
|
31
31
|
@abbreviation = abbreviation
|
|
32
32
|
@sport = sport
|
|
33
|
-
@
|
|
33
|
+
@league_name = league_name
|
|
34
34
|
@venue = venue
|
|
35
35
|
super()
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
def self.fetch_by_id(id:, sport:,
|
|
38
|
+
def self.fetch_by_id(id:, sport:, league_name:, user_agent: nil)
|
|
39
39
|
client = EspnPub::Client.new
|
|
40
40
|
client.user_agent = user_agent if user_agent
|
|
41
41
|
|
|
42
|
-
path = format TEAM_PATH, client.version, sport,
|
|
42
|
+
path = format TEAM_PATH, client.version, sport, league_name, id
|
|
43
43
|
begin
|
|
44
44
|
team_data = client.send_request(path)['team']
|
|
45
45
|
rescue Client::UnexpectedResponseCodeError => e
|
|
@@ -55,7 +55,7 @@ module EspnPub
|
|
|
55
55
|
location: team_data['location'],
|
|
56
56
|
abbreviation: team_data['abbreviation'],
|
|
57
57
|
sport: sport,
|
|
58
|
-
|
|
58
|
+
league_name: league_name,
|
|
59
59
|
venue: EspnPub::Entities::Venue.from_api(team_data.dig('franchise', 'venue'))
|
|
60
60
|
)
|
|
61
61
|
end
|
|
@@ -66,13 +66,13 @@ module EspnPub
|
|
|
66
66
|
def players
|
|
67
67
|
unless defined?(@roster)
|
|
68
68
|
begin
|
|
69
|
-
path = format ROSTER_PATH, client.version, sport,
|
|
69
|
+
path = format ROSTER_PATH, client.version, sport, league_name, id
|
|
70
70
|
roster_resp = client.send_request(path)
|
|
71
71
|
@roster = (roster_resp['athletes'] || []).map do |athlete_data|
|
|
72
72
|
EspnPub::Entities::Player.new(
|
|
73
73
|
id: athlete_data['id'],
|
|
74
74
|
sport: sport,
|
|
75
|
-
|
|
75
|
+
league_name: league_name,
|
|
76
76
|
first_name: athlete_data['firstName'],
|
|
77
77
|
last_name: athlete_data['lastName'],
|
|
78
78
|
position: athlete_data['position']['abbreviation'],
|
data/lib/espn_pub/version.rb
CHANGED