mlb 0.10.0 → 0.11.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.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +329 -108
  4. data/lib/mlb/affiliates.rb +28 -0
  5. data/lib/mlb/alumni.rb +30 -0
  6. data/lib/mlb/attendance.rb +28 -0
  7. data/lib/mlb/attendance_record.rb +157 -0
  8. data/lib/mlb/award.rb +89 -11
  9. data/lib/mlb/awards.rb +17 -7
  10. data/lib/mlb/baseball_stat.rb +65 -0
  11. data/lib/mlb/baseball_stats.rb +12 -0
  12. data/lib/mlb/boxscore.rb +81 -0
  13. data/lib/mlb/boxscore_team_stats.rb +210 -0
  14. data/lib/mlb/client.rb +71 -0
  15. data/lib/mlb/coaches.rb +28 -0
  16. data/lib/mlb/code_description_type.rb +45 -0
  17. data/lib/mlb/collection.rb +48 -0
  18. data/lib/mlb/comparable_by_attribute.rb +56 -0
  19. data/lib/mlb/conference.rb +8 -1
  20. data/lib/mlb/conferences.rb +18 -10
  21. data/lib/mlb/connection.rb +130 -7
  22. data/lib/mlb/context_metrics.rb +90 -0
  23. data/lib/mlb/division.rb +24 -6
  24. data/lib/mlb/divisions.rb +24 -18
  25. data/lib/mlb/draft.rb +83 -0
  26. data/lib/mlb/draft_pick.rb +155 -0
  27. data/lib/mlb/error_handler.rb +21 -1
  28. data/lib/mlb/errors/bad_gateway.rb +6 -1
  29. data/lib/mlb/errors/bad_request.rb +1 -0
  30. data/lib/mlb/errors/client_error.rb +1 -0
  31. data/lib/mlb/errors/connection_exception.rb +1 -0
  32. data/lib/mlb/errors/error.rb +1 -0
  33. data/lib/mlb/errors/forbidden.rb +1 -0
  34. data/lib/mlb/errors/gateway_timeout.rb +6 -1
  35. data/lib/mlb/errors/gone.rb +1 -0
  36. data/lib/mlb/errors/http_error.rb +22 -2
  37. data/lib/mlb/errors/internal_server_error.rb +1 -0
  38. data/lib/mlb/errors/network_error.rb +6 -1
  39. data/lib/mlb/errors/not_acceptable.rb +1 -0
  40. data/lib/mlb/errors/not_found.rb +1 -0
  41. data/lib/mlb/errors/payload_too_large.rb +1 -0
  42. data/lib/mlb/errors/retryable.rb +15 -0
  43. data/lib/mlb/errors/server_error.rb +1 -0
  44. data/lib/mlb/errors/service_unavailable.rb +6 -1
  45. data/lib/mlb/errors/too_many_redirects.rb +1 -0
  46. data/lib/mlb/errors/too_many_requests.rb +6 -1
  47. data/lib/mlb/errors/unauthorized.rb +1 -0
  48. data/lib/mlb/errors/unprocessable_entity.rb +1 -0
  49. data/lib/mlb/event_type.rb +61 -0
  50. data/lib/mlb/event_types.rb +12 -0
  51. data/lib/mlb/free_agent.rb +78 -0
  52. data/lib/mlb/free_agents.rb +34 -0
  53. data/lib/mlb/game_changes.rb +87 -0
  54. data/lib/mlb/game_content.rb +251 -0
  55. data/lib/mlb/game_data.rb +155 -0
  56. data/lib/mlb/game_pace.rb +173 -0
  57. data/lib/mlb/game_status.rb +94 -0
  58. data/lib/mlb/game_statuses.rb +12 -0
  59. data/lib/mlb/game_type.rb +98 -0
  60. data/lib/mlb/game_types.rb +12 -0
  61. data/lib/mlb/handedness.rb +28 -10
  62. data/lib/mlb/high_low.rb +121 -0
  63. data/lib/mlb/hit_trajectories.rb +12 -0
  64. data/lib/mlb/hit_trajectory.rb +6 -0
  65. data/lib/mlb/home_run_derby.rb +285 -0
  66. data/lib/mlb/id_description_type.rb +44 -0
  67. data/lib/mlb/inning_score.rb +87 -0
  68. data/lib/mlb/job.rb +58 -0
  69. data/lib/mlb/job_type.rb +39 -0
  70. data/lib/mlb/job_types.rb +12 -0
  71. data/lib/mlb/jobs.rb +87 -0
  72. data/lib/mlb/language.rb +48 -0
  73. data/lib/mlb/languages.rb +12 -0
  74. data/lib/mlb/leader.rb +79 -0
  75. data/lib/mlb/leaders.rb +68 -0
  76. data/lib/mlb/league.rb +95 -14
  77. data/lib/mlb/league_leader_type.rb +21 -0
  78. data/lib/mlb/league_leader_types.rb +12 -0
  79. data/lib/mlb/leagues.rb +24 -18
  80. data/lib/mlb/linescore.rb +342 -0
  81. data/lib/mlb/linescore_teams.rb +89 -0
  82. data/lib/mlb/live_feed.rb +130 -0
  83. data/lib/mlb/logical_event.rb +17 -0
  84. data/lib/mlb/logical_events.rb +12 -0
  85. data/lib/mlb/metric.rb +48 -0
  86. data/lib/mlb/metrics.rb +12 -0
  87. data/lib/mlb/people_changes.rb +34 -0
  88. data/lib/mlb/personnel.rb +28 -0
  89. data/lib/mlb/pitch_code.rb +90 -0
  90. data/lib/mlb/pitch_codes.rb +12 -0
  91. data/lib/mlb/pitch_type.rb +6 -0
  92. data/lib/mlb/pitch_types.rb +12 -0
  93. data/lib/mlb/platform.rb +30 -0
  94. data/lib/mlb/platforms.rb +12 -0
  95. data/lib/mlb/play.rb +300 -0
  96. data/lib/mlb/play_by_play.rb +52 -0
  97. data/lib/mlb/player.rb +182 -0
  98. data/lib/mlb/player_game_stats.rb +350 -0
  99. data/lib/mlb/player_stat.rb +70 -0
  100. data/lib/mlb/players.rb +34 -23
  101. data/lib/mlb/position.rb +65 -0
  102. data/lib/mlb/positions.rb +12 -0
  103. data/lib/mlb/postseason_schedule.rb +41 -0
  104. data/lib/mlb/postseason_series.rb +54 -0
  105. data/lib/mlb/redirect_handler.rb +80 -21
  106. data/lib/mlb/request_builder.rb +33 -2
  107. data/lib/mlb/review_reason.rb +6 -0
  108. data/lib/mlb/review_reasons.rb +12 -0
  109. data/lib/mlb/roster.rb +16 -12
  110. data/lib/mlb/roster_entry.rb +35 -0
  111. data/lib/mlb/roster_type.rb +39 -0
  112. data/lib/mlb/roster_types.rb +12 -0
  113. data/lib/mlb/schedule.rb +33 -0
  114. data/lib/mlb/schedule_date.rb +37 -0
  115. data/lib/mlb/schedule_event_type.rb +25 -0
  116. data/lib/mlb/schedule_event_types.rb +12 -0
  117. data/lib/mlb/scheduled_game.rb +321 -0
  118. data/lib/mlb/season.rb +15 -4
  119. data/lib/mlb/season_date_info.rb +1 -0
  120. data/lib/mlb/seasons.rb +24 -18
  121. data/lib/mlb/single_team_stats.rb +33 -0
  122. data/lib/mlb/situation_code.rb +116 -0
  123. data/lib/mlb/situation_codes.rb +12 -0
  124. data/lib/mlb/skies.rb +12 -0
  125. data/lib/mlb/sky.rb +3 -8
  126. data/lib/mlb/sport.rb +16 -5
  127. data/lib/mlb/sport_players.rb +30 -0
  128. data/lib/mlb/sports.rb +17 -7
  129. data/lib/mlb/standings.rb +47 -0
  130. data/lib/mlb/standings_record.rb +108 -0
  131. data/lib/mlb/standings_type.rb +25 -0
  132. data/lib/mlb/standings_types.rb +12 -0
  133. data/lib/mlb/stat_group.rb +21 -0
  134. data/lib/mlb/stat_groups.rb +12 -0
  135. data/lib/mlb/stat_type.rb +21 -0
  136. data/lib/mlb/stat_types.rb +12 -0
  137. data/lib/mlb/stat_values.rb +158 -0
  138. data/lib/mlb/stats.rb +41 -0
  139. data/lib/mlb/status.rb +14 -0
  140. data/lib/mlb/streaks.rb +184 -0
  141. data/lib/mlb/team.rb +62 -7
  142. data/lib/mlb/team_history.rb +28 -0
  143. data/lib/mlb/team_leader.rb +59 -0
  144. data/lib/mlb/team_leaders.rb +45 -0
  145. data/lib/mlb/team_record.rb +285 -0
  146. data/lib/mlb/team_stat.rb +50 -0
  147. data/lib/mlb/team_stats.rb +52 -0
  148. data/lib/mlb/teams.rb +28 -18
  149. data/lib/mlb/tied_games.rb +32 -0
  150. data/lib/mlb/transaction.rb +125 -0
  151. data/lib/mlb/transactions.rb +11 -6
  152. data/lib/mlb/uniforms.rb +97 -0
  153. data/lib/mlb/utils.rb +45 -0
  154. data/lib/mlb/venue.rb +7 -0
  155. data/lib/mlb/venues.rb +28 -18
  156. data/lib/mlb/version.rb +2 -1
  157. data/lib/mlb/win_probability.rb +64 -0
  158. data/lib/mlb/wind_direction.rb +3 -8
  159. data/lib/mlb/wind_directions.rb +12 -0
  160. data/lib/mlb.rb +61 -0
  161. data/sig/equalizer.rbs +3 -0
  162. data/sig/mlb.rbs +2055 -0
  163. data/sig/shale.rbs +29 -0
  164. metadata +107 -2
data/lib/mlb/leader.rb ADDED
@@ -0,0 +1,79 @@
1
+ require "equalizer"
2
+ require "shale"
3
+ require_relative "player"
4
+ require_relative "team"
5
+ require_relative "league"
6
+ require_relative "sport"
7
+
8
+ module MLB
9
+ # Represents a league leader
10
+ class Leader < Shale::Mapper
11
+ include Equalizer.new(:rank, :person)
12
+
13
+ # @!attribute [rw] rank
14
+ # Returns the leader rank
15
+ # @api public
16
+ # @example
17
+ # leader.rank #=> 1
18
+ # @return [Integer] the rank
19
+ attribute :rank, Shale::Type::Integer
20
+
21
+ # @!attribute [rw] value
22
+ # Returns the stat value
23
+ # @api public
24
+ # @example
25
+ # leader.value #=> "58"
26
+ # @return [String] the value
27
+ attribute :value, Shale::Type::String
28
+
29
+ # @!attribute [rw] person
30
+ # Returns the player
31
+ # @api public
32
+ # @example
33
+ # leader.person #=> #<MLB::Player>
34
+ # @return [Player] the player
35
+ attribute :person, Player
36
+
37
+ # @!attribute [rw] team
38
+ # Returns the team
39
+ # @api public
40
+ # @example
41
+ # leader.team #=> #<MLB::Team>
42
+ # @return [Team] the team
43
+ attribute :team, Team
44
+
45
+ # @!attribute [rw] league
46
+ # Returns the league
47
+ # @api public
48
+ # @example
49
+ # leader.league #=> #<MLB::League>
50
+ # @return [League] the league
51
+ attribute :league, League
52
+
53
+ # @!attribute [rw] sport
54
+ # Returns the sport
55
+ # @api public
56
+ # @example
57
+ # leader.sport #=> #<MLB::Sport>
58
+ # @return [Sport] the sport
59
+ attribute :sport, Sport
60
+
61
+ # @!attribute [rw] season
62
+ # Returns the season
63
+ # @api public
64
+ # @example
65
+ # leader.season #=> "2024"
66
+ # @return [String] the season
67
+ attribute :season, Shale::Type::String
68
+
69
+ json do
70
+ map "rank", to: :rank
71
+ map "value", to: :value
72
+ map "person", to: :person
73
+ map "team", to: :team
74
+ map "league", to: :league
75
+ map "sport", to: :sport
76
+ map "season", to: :season
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,68 @@
1
+ require "shale"
2
+ require_relative "leader"
3
+
4
+ module MLB
5
+ # Represents a leader category result
6
+ class LeaderCategory < Shale::Mapper
7
+ # @!attribute [rw] leader_category
8
+ # Returns the category name
9
+ # @api public
10
+ # @example
11
+ # category.leader_category #=> "homeRuns"
12
+ # @return [String] the category
13
+ attribute :leader_category, Shale::Type::String
14
+
15
+ # @!attribute [rw] season
16
+ # Returns the season
17
+ # @api public
18
+ # @example
19
+ # category.season #=> "2024"
20
+ # @return [String] the season
21
+ attribute :season, Shale::Type::String
22
+
23
+ # @!attribute [rw] leaders
24
+ # Returns the leaders
25
+ # @api public
26
+ # @example
27
+ # category.leaders #=> [#<MLB::Leader>, ...]
28
+ # @return [Array<Leader>] the leaders
29
+ attribute :leaders, Leader, collection: true
30
+
31
+ json do
32
+ map "leaderCategory", to: :leader_category
33
+ map "season", to: :season
34
+ map "leaders", to: :leaders
35
+ end
36
+ end
37
+
38
+ # Provides methods for fetching league leaders from the API
39
+ class Leaders < Shale::Mapper
40
+ # @!attribute [rw] league_leaders
41
+ # Returns the league leaders
42
+ # @api public
43
+ # @example
44
+ # leaders.league_leaders #=> [#<MLB::LeaderCategory>, ...]
45
+ # @return [Array<LeaderCategory>] the league leaders
46
+ attribute :league_leaders, LeaderCategory, collection: true
47
+
48
+ json do
49
+ map "leagueLeaders", to: :league_leaders
50
+ end
51
+
52
+ # Retrieves league leaders for a category
53
+ #
54
+ # @api public
55
+ # @example Get home run leaders
56
+ # MLB::Leaders.find(category: "homeRuns", season: 2024)
57
+ # @param category [String] the stat category
58
+ # @param season [Integer, nil] the season year (defaults to current year)
59
+ # @param limit [Integer] the number of leaders to return (defaults to 10)
60
+ # @return [Array<Leader>] the leaders
61
+ def self.find(category:, season: nil, limit: 10)
62
+ season ||= Utils.current_season
63
+ params = {leaderCategories: category, season:, limit:}
64
+ response = CLIENT.get("stats/leaders?#{Utils.build_query(params)}")
65
+ from_json(response).league_leaders.first&.leaders || []
66
+ end
67
+ end
68
+ end
data/lib/mlb/league.rb CHANGED
@@ -1,20 +1,34 @@
1
1
  require "equalizer"
2
2
  require "shale"
3
+ require_relative "comparable_by_attribute"
3
4
  require_relative "season_date_info"
4
5
  require_relative "sport"
5
6
 
6
7
  module MLB
8
+ # Represents a league (e.g., American League, National League)
7
9
  class League < Shale::Mapper
8
10
  include Comparable
11
+ include ComparableByAttribute
9
12
  include Equalizer.new(:id)
10
13
 
14
+ SEASON_PRESEASON = "preseason".freeze
15
+ SEASON_INSEASON = "inseason".freeze
16
+ SEASON_POSTSEASON = "postseason".freeze
17
+ SEASON_OFFSEASON = "offseason".freeze
18
+
19
+ # Returns the attribute used for sorting
20
+ #
21
+ # @api private
22
+ # @return [Symbol] the attribute used for comparison
23
+ def comparable_attribute = :sort_order
24
+
11
25
  attribute :id, Shale::Type::Integer
12
26
  attribute :name, Shale::Type::String
13
27
  attribute :link, Shale::Type::String
14
28
  attribute :abbreviation, Shale::Type::String
15
29
  attribute :name_short, Shale::Type::String
16
30
  attribute :season_state, Shale::Type::String
17
- attribute :has_wild_card, Shale::Type::Boolean
31
+ attribute :has_wildcard, Shale::Type::Boolean
18
32
  attribute :has_split_season, Shale::Type::Boolean
19
33
  attribute :num_games, Shale::Type::Integer
20
34
  attribute :has_playoff_points, Shale::Type::Boolean
@@ -29,14 +43,85 @@ module MLB
29
43
  attribute :sort_order, Shale::Type::Integer
30
44
  attribute :active, Shale::Type::Boolean
31
45
 
32
- alias_method :active?, :active
33
- alias_method :wild_card?, :has_wild_card
34
- alias_method :has_wildcard, :has_wild_card
35
- alias_method :wildcard?, :has_wild_card
36
- alias_method :split_season?, :has_split_season
37
- alias_method :playoff_points?, :has_playoff_points
38
- alias_method :conferences?, :conferences_in_use
39
- alias_method :divisions?, :divisions_in_use
46
+ # Checks if the league is active
47
+ #
48
+ # @api public
49
+ # @example
50
+ # league.active? #=> true
51
+ # @return [Boolean] whether the league is active
52
+ def active? = active
53
+
54
+ # Checks if the league has a wildcard
55
+ #
56
+ # @api public
57
+ # @example
58
+ # league.wildcard? #=> true
59
+ # @return [Boolean] whether the league has a wildcard
60
+ def wildcard? = has_wildcard
61
+
62
+ # Checks if the league has a split season
63
+ #
64
+ # @api public
65
+ # @example
66
+ # league.split_season? #=> false
67
+ # @return [Boolean] whether the league has a split season
68
+ def split_season? = has_split_season
69
+
70
+ # Checks if the league uses playoff points
71
+ #
72
+ # @api public
73
+ # @example
74
+ # league.playoff_points? #=> false
75
+ # @return [Boolean] whether the league uses playoff points
76
+ def playoff_points? = has_playoff_points
77
+
78
+ # Checks if the league uses conferences
79
+ #
80
+ # @api public
81
+ # @example
82
+ # league.conferences? #=> false
83
+ # @return [Boolean] whether the league uses conferences
84
+ def conferences? = conferences_in_use
85
+
86
+ # Checks if the league uses divisions
87
+ #
88
+ # @api public
89
+ # @example
90
+ # league.divisions? #=> true
91
+ # @return [Boolean] whether the league uses divisions
92
+ def divisions? = divisions_in_use
93
+
94
+ # Checks if the league is in preseason
95
+ #
96
+ # @api public
97
+ # @example
98
+ # league.preseason? #=> false
99
+ # @return [Boolean] whether the league is in preseason
100
+ def preseason? = season_state.eql?(SEASON_PRESEASON)
101
+
102
+ # Checks if the league is in season
103
+ #
104
+ # @api public
105
+ # @example
106
+ # league.in_season? #=> true
107
+ # @return [Boolean] whether the league is in season
108
+ def in_season? = season_state.eql?(SEASON_INSEASON)
109
+
110
+ # Checks if the league is in postseason
111
+ #
112
+ # @api public
113
+ # @example
114
+ # league.postseason? #=> false
115
+ # @return [Boolean] whether the league is in postseason
116
+ def postseason? = season_state.eql?(SEASON_POSTSEASON)
117
+
118
+ # Checks if the league is in offseason
119
+ #
120
+ # @api public
121
+ # @example
122
+ # league.offseason? #=> false
123
+ # @return [Boolean] whether the league is in offseason
124
+ def offseason? = season_state.eql?(SEASON_OFFSEASON)
40
125
 
41
126
  json do
42
127
  map "id", to: :id
@@ -45,7 +130,7 @@ module MLB
45
130
  map "abbreviation", to: :abbreviation
46
131
  map "nameShort", to: :name_short
47
132
  map "seasonState", to: :season_state
48
- map "hasWildCard", to: :has_wild_card
133
+ map "hasWildCard", to: :has_wildcard
49
134
  map "hasSplitSeason", to: :has_split_season
50
135
  map "numGames", to: :num_games
51
136
  map "hasPlayoffPoints", to: :has_playoff_points
@@ -60,9 +145,5 @@ module MLB
60
145
  map "sortOrder", to: :sort_order
61
146
  map "active", to: :active
62
147
  end
63
-
64
- def <=>(other)
65
- sort_order <=> other.sort_order
66
- end
67
148
  end
68
149
  end
@@ -0,0 +1,21 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a league leader statistic type
6
+ class LeagueLeaderType < Shale::Mapper
7
+ include Equalizer.new(:display_name)
8
+
9
+ # @!attribute [rw] display_name
10
+ # Returns the display name for the leader type
11
+ # @api public
12
+ # @example
13
+ # league_leader_type.display_name #=> "homeRuns"
14
+ # @return [String] the display name for the leader type
15
+ attribute :display_name, Shale::Type::String
16
+
17
+ json do
18
+ map "displayName", to: :display_name
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "league_leader_type"
3
+
4
+ module MLB
5
+ # Provides methods for fetching league leader types from the API
6
+ #
7
+ # @example Fetch all league leader types
8
+ # MLB::LeagueLeaderTypes.all #=> [#<MLB::LeagueLeaderType>, ...]
9
+ class LeagueLeaderTypes < Collection
10
+ collection endpoint: "leagueLeaderTypes", item_type: LeagueLeaderType, collection_name: :league_leader_types
11
+ end
12
+ end
data/lib/mlb/leagues.rb CHANGED
@@ -1,30 +1,36 @@
1
1
  require "shale"
2
- require "uri"
3
- require_relative "sport"
4
2
  require_relative "league"
5
3
 
6
4
  module MLB
5
+ # Collection of leagues from the MLB Stats API
7
6
  class Leagues < Shale::Mapper
8
- attribute :copyright, Shale::Type::String
9
7
  attribute :leagues, League, collection: true
10
8
 
11
- def self.all(sport: Sport.new(id: 1))
12
- sport_id = sport.respond_to?(:id) ? sport.id : sport
13
- params = {sportId: sport_id}
14
- query_string = URI.encode_www_form(params)
15
- response = CLIENT.get("leagues?#{query_string}")
16
- leagues = from_json(response)
17
- leagues.leagues.sort!
9
+ # Retrieves all leagues
10
+ #
11
+ # @api public
12
+ # @example
13
+ # MLB::Leagues.all
14
+ # @param sport [Integer, Sport] the sport ID or Sport object
15
+ # @return [Array<League>] list of all leagues
16
+ def self.all(sport: Utils::DEFAULT_SPORT_ID)
17
+ params = {sportId: Utils.extract_id(sport)}
18
+ response = CLIENT.get("leagues?#{Utils.build_query(params)}")
19
+ from_json(response).leagues.sort
18
20
  end
19
21
 
20
- def self.find(league, sport: Sport.new(id: 1))
21
- id = league.respond_to?(:id) ? league.id : league
22
- sport_id = sport.respond_to?(:id) ? sport.id : sport
23
- params = {sportId: sport_id}
24
- query_string = URI.encode_www_form(params)
25
- response = CLIENT.get("leagues/#{id}?#{query_string}")
26
- leagues = from_json(response)
27
- leagues.leagues.sort!.first
22
+ # Finds a league by ID
23
+ #
24
+ # @api public
25
+ # @example
26
+ # MLB::Leagues.find(103)
27
+ # @param league [Integer, League] the league ID or League object
28
+ # @param sport [Integer, Sport] the sport ID or Sport object
29
+ # @return [League, nil] the league if found
30
+ def self.find(league, sport: Utils::DEFAULT_SPORT_ID)
31
+ params = {sportId: Utils.extract_id(sport)}
32
+ response = CLIENT.get("leagues/#{Utils.extract_id(league)}?#{Utils.build_query(params)}")
33
+ from_json(response).leagues.min_by { |l| l.sort_order || 0 }
28
34
  end
29
35
  end
30
36
  end