mlb 0.9.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 (165) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -1
  3. data/LICENSE.txt +1 -1
  4. data/README.md +329 -32
  5. data/lib/mlb/affiliates.rb +28 -0
  6. data/lib/mlb/alumni.rb +30 -0
  7. data/lib/mlb/attendance.rb +28 -0
  8. data/lib/mlb/attendance_record.rb +157 -0
  9. data/lib/mlb/award.rb +126 -0
  10. data/lib/mlb/awards.rb +32 -0
  11. data/lib/mlb/baseball_stat.rb +65 -0
  12. data/lib/mlb/baseball_stats.rb +12 -0
  13. data/lib/mlb/boxscore.rb +81 -0
  14. data/lib/mlb/boxscore_team_stats.rb +210 -0
  15. data/lib/mlb/client.rb +71 -1
  16. data/lib/mlb/coaches.rb +28 -0
  17. data/lib/mlb/code_description_type.rb +45 -0
  18. data/lib/mlb/collection.rb +48 -0
  19. data/lib/mlb/comparable_by_attribute.rb +56 -0
  20. data/lib/mlb/conference.rb +39 -0
  21. data/lib/mlb/conferences.rb +33 -0
  22. data/lib/mlb/connection.rb +130 -7
  23. data/lib/mlb/context_metrics.rb +90 -0
  24. data/lib/mlb/division.rb +26 -6
  25. data/lib/mlb/divisions.rb +24 -18
  26. data/lib/mlb/draft.rb +83 -0
  27. data/lib/mlb/draft_pick.rb +155 -0
  28. data/lib/mlb/error_handler.rb +21 -1
  29. data/lib/mlb/errors/bad_gateway.rb +6 -1
  30. data/lib/mlb/errors/bad_request.rb +1 -0
  31. data/lib/mlb/errors/client_error.rb +1 -0
  32. data/lib/mlb/errors/connection_exception.rb +1 -0
  33. data/lib/mlb/errors/error.rb +1 -0
  34. data/lib/mlb/errors/forbidden.rb +1 -0
  35. data/lib/mlb/errors/gateway_timeout.rb +6 -1
  36. data/lib/mlb/errors/gone.rb +1 -0
  37. data/lib/mlb/errors/http_error.rb +22 -2
  38. data/lib/mlb/errors/internal_server_error.rb +1 -0
  39. data/lib/mlb/errors/network_error.rb +6 -1
  40. data/lib/mlb/errors/not_acceptable.rb +1 -0
  41. data/lib/mlb/errors/not_found.rb +1 -0
  42. data/lib/mlb/errors/payload_too_large.rb +1 -0
  43. data/lib/mlb/errors/retryable.rb +15 -0
  44. data/lib/mlb/errors/server_error.rb +1 -0
  45. data/lib/mlb/errors/service_unavailable.rb +6 -1
  46. data/lib/mlb/errors/too_many_redirects.rb +1 -0
  47. data/lib/mlb/errors/too_many_requests.rb +6 -1
  48. data/lib/mlb/errors/unauthorized.rb +1 -0
  49. data/lib/mlb/errors/unprocessable_entity.rb +1 -0
  50. data/lib/mlb/event_type.rb +61 -0
  51. data/lib/mlb/event_types.rb +12 -0
  52. data/lib/mlb/free_agent.rb +78 -0
  53. data/lib/mlb/free_agents.rb +34 -0
  54. data/lib/mlb/game_changes.rb +87 -0
  55. data/lib/mlb/game_content.rb +251 -0
  56. data/lib/mlb/game_data.rb +155 -0
  57. data/lib/mlb/game_pace.rb +173 -0
  58. data/lib/mlb/game_status.rb +94 -0
  59. data/lib/mlb/game_statuses.rb +12 -0
  60. data/lib/mlb/game_type.rb +98 -0
  61. data/lib/mlb/game_types.rb +12 -0
  62. data/lib/mlb/handedness.rb +30 -9
  63. data/lib/mlb/high_low.rb +121 -0
  64. data/lib/mlb/hit_trajectories.rb +12 -0
  65. data/lib/mlb/hit_trajectory.rb +6 -0
  66. data/lib/mlb/home_run_derby.rb +285 -0
  67. data/lib/mlb/id_description_type.rb +44 -0
  68. data/lib/mlb/inning_score.rb +87 -0
  69. data/lib/mlb/job.rb +58 -0
  70. data/lib/mlb/job_type.rb +39 -0
  71. data/lib/mlb/job_types.rb +12 -0
  72. data/lib/mlb/jobs.rb +87 -0
  73. data/lib/mlb/language.rb +48 -0
  74. data/lib/mlb/languages.rb +12 -0
  75. data/lib/mlb/leader.rb +79 -0
  76. data/lib/mlb/leaders.rb +68 -0
  77. data/lib/mlb/league.rb +97 -14
  78. data/lib/mlb/league_leader_type.rb +21 -0
  79. data/lib/mlb/league_leader_types.rb +12 -0
  80. data/lib/mlb/leagues.rb +24 -18
  81. data/lib/mlb/linescore.rb +342 -0
  82. data/lib/mlb/linescore_teams.rb +89 -0
  83. data/lib/mlb/live_feed.rb +130 -0
  84. data/lib/mlb/logical_event.rb +17 -0
  85. data/lib/mlb/logical_events.rb +12 -0
  86. data/lib/mlb/metric.rb +48 -0
  87. data/lib/mlb/metrics.rb +12 -0
  88. data/lib/mlb/people_changes.rb +34 -0
  89. data/lib/mlb/personnel.rb +28 -0
  90. data/lib/mlb/pitch_code.rb +90 -0
  91. data/lib/mlb/pitch_codes.rb +12 -0
  92. data/lib/mlb/pitch_type.rb +6 -0
  93. data/lib/mlb/pitch_types.rb +12 -0
  94. data/lib/mlb/platform.rb +30 -0
  95. data/lib/mlb/platforms.rb +12 -0
  96. data/lib/mlb/play.rb +300 -0
  97. data/lib/mlb/play_by_play.rb +52 -0
  98. data/lib/mlb/player.rb +186 -0
  99. data/lib/mlb/player_game_stats.rb +350 -0
  100. data/lib/mlb/player_stat.rb +70 -0
  101. data/lib/mlb/players.rb +42 -11
  102. data/lib/mlb/position.rb +68 -0
  103. data/lib/mlb/positions.rb +12 -0
  104. data/lib/mlb/postseason_schedule.rb +41 -0
  105. data/lib/mlb/postseason_series.rb +54 -0
  106. data/lib/mlb/redirect_handler.rb +80 -21
  107. data/lib/mlb/request_builder.rb +33 -2
  108. data/lib/mlb/review_reason.rb +6 -0
  109. data/lib/mlb/review_reasons.rb +12 -0
  110. data/lib/mlb/roster.rb +16 -12
  111. data/lib/mlb/roster_entry.rb +38 -0
  112. data/lib/mlb/roster_type.rb +39 -0
  113. data/lib/mlb/roster_types.rb +12 -0
  114. data/lib/mlb/schedule.rb +33 -0
  115. data/lib/mlb/schedule_date.rb +37 -0
  116. data/lib/mlb/schedule_event_type.rb +25 -0
  117. data/lib/mlb/schedule_event_types.rb +12 -0
  118. data/lib/mlb/scheduled_game.rb +321 -0
  119. data/lib/mlb/season.rb +74 -0
  120. data/lib/mlb/season_date_info.rb +4 -0
  121. data/lib/mlb/seasons.rb +36 -0
  122. data/lib/mlb/single_team_stats.rb +33 -0
  123. data/lib/mlb/situation_code.rb +116 -0
  124. data/lib/mlb/situation_codes.rb +12 -0
  125. data/lib/mlb/skies.rb +12 -0
  126. data/lib/mlb/sky.rb +6 -0
  127. data/lib/mlb/sport.rb +18 -5
  128. data/lib/mlb/sport_players.rb +30 -0
  129. data/lib/mlb/sports.rb +17 -7
  130. data/lib/mlb/standings.rb +47 -0
  131. data/lib/mlb/standings_record.rb +108 -0
  132. data/lib/mlb/standings_type.rb +25 -0
  133. data/lib/mlb/standings_types.rb +12 -0
  134. data/lib/mlb/stat_group.rb +21 -0
  135. data/lib/mlb/stat_groups.rb +12 -0
  136. data/lib/mlb/stat_type.rb +21 -0
  137. data/lib/mlb/stat_types.rb +12 -0
  138. data/lib/mlb/stat_values.rb +158 -0
  139. data/lib/mlb/stats.rb +41 -0
  140. data/lib/mlb/status.rb +17 -0
  141. data/lib/mlb/streaks.rb +184 -0
  142. data/lib/mlb/team.rb +66 -7
  143. data/lib/mlb/team_history.rb +28 -0
  144. data/lib/mlb/team_leader.rb +59 -0
  145. data/lib/mlb/team_leaders.rb +45 -0
  146. data/lib/mlb/team_record.rb +285 -0
  147. data/lib/mlb/team_stat.rb +50 -0
  148. data/lib/mlb/team_stats.rb +52 -0
  149. data/lib/mlb/teams.rb +28 -18
  150. data/lib/mlb/tied_games.rb +32 -0
  151. data/lib/mlb/transaction.rb +128 -0
  152. data/lib/mlb/transactions.rb +11 -6
  153. data/lib/mlb/uniforms.rb +97 -0
  154. data/lib/mlb/utils.rb +45 -0
  155. data/lib/mlb/venue.rb +10 -0
  156. data/lib/mlb/venues.rb +28 -18
  157. data/lib/mlb/version.rb +2 -1
  158. data/lib/mlb/win_probability.rb +64 -0
  159. data/lib/mlb/wind_direction.rb +6 -0
  160. data/lib/mlb/wind_directions.rb +12 -0
  161. data/lib/mlb.rb +64 -0
  162. data/sig/equalizer.rbs +3 -0
  163. data/sig/mlb.rbs +1972 -103
  164. data/sig/shale.rbs +29 -0
  165. metadata +137 -14
data/lib/mlb/metric.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a statcast metric
6
+ class Metric < Shale::Mapper
7
+ include Equalizer.new(:metric_id)
8
+
9
+ # @!attribute [rw] name
10
+ # Returns the metric name
11
+ # @api public
12
+ # @example
13
+ # metric.name #=> "launchSpeed"
14
+ # @return [String] the metric name
15
+ attribute :name, Shale::Type::String
16
+
17
+ # @!attribute [rw] metric_id
18
+ # Returns the unique metric identifier
19
+ # @api public
20
+ # @example
21
+ # metric.metric_id #=> 1
22
+ # @return [Integer] the unique metric identifier
23
+ attribute :metric_id, Shale::Type::Integer
24
+
25
+ # @!attribute [rw] group
26
+ # Returns the metric group
27
+ # @api public
28
+ # @example
29
+ # metric.group #=> "hitting"
30
+ # @return [String] the metric group
31
+ attribute :group, Shale::Type::String
32
+
33
+ # @!attribute [rw] unit
34
+ # Returns the measurement unit
35
+ # @api public
36
+ # @example
37
+ # metric.unit #=> "MPH"
38
+ # @return [String] the measurement unit
39
+ attribute :unit, Shale::Type::String
40
+
41
+ json do
42
+ map "name", to: :name
43
+ map "metricId", to: :metric_id
44
+ map "group", to: :group
45
+ map "unit", to: :unit
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "metric"
3
+
4
+ module MLB
5
+ # Provides methods for fetching metrics from the API
6
+ #
7
+ # @example Fetch all metrics
8
+ # MLB::Metrics.all #=> [#<MLB::Metric>, ...]
9
+ class Metrics < Collection
10
+ collection endpoint: "metrics", item_type: Metric, collection_name: :metrics
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ require "shale"
2
+ require "uri"
3
+ require_relative "player"
4
+
5
+ module MLB
6
+ # Provides methods for fetching people changes from the API
7
+ class PeopleChanges < Shale::Mapper
8
+ # @!attribute [rw] people
9
+ # Returns the changed people
10
+ # @api public
11
+ # @example
12
+ # people_changes.people #=> [#<MLB::Player>, ...]
13
+ # @return [Array<Player>] the changed people
14
+ attribute :people, Player, collection: true
15
+
16
+ json do
17
+ map "people", to: :people
18
+ end
19
+
20
+ # Retrieves people changes since a date
21
+ #
22
+ # @api public
23
+ # @example Get people changes since a date
24
+ # MLB::PeopleChanges.since(date: Date.new(2024, 6, 1))
25
+ # @param date [Date] the date to check changes from
26
+ # @return [Array<Player>] the changed people
27
+ def self.since(date:)
28
+ params = {updatedSince: date.to_s}
29
+ query_string = URI.encode_www_form(params)
30
+ response = CLIENT.get("people/changes?#{query_string}")
31
+ from_json(response).people
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ require "shale"
2
+ require_relative "job"
3
+
4
+ module MLB
5
+ # Provides methods for fetching team personnel from the API
6
+ class Personnel < Shale::Mapper
7
+ attribute :roster, Job, collection: true
8
+
9
+ json do
10
+ map "roster", to: :roster
11
+ end
12
+
13
+ # Retrieves personnel for a team
14
+ #
15
+ # @api public
16
+ # @example Get personnel for a team
17
+ # MLB::Personnel.find(team: 147, season: 2024)
18
+ # @param team [Team, Integer] the team or team ID
19
+ # @param season [Integer, nil] the season year (defaults to current year)
20
+ # @return [Array<Job>] the personnel
21
+ def self.find(team:, season: nil)
22
+ season ||= Utils.current_season
23
+ team_id = Utils.extract_id(team)
24
+ response = CLIENT.get("teams/#{team_id}/personnel?#{Utils.build_query(season:)}")
25
+ from_json(response).roster
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,90 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a pitch code (result of a pitch)
6
+ class PitchCode < Shale::Mapper
7
+ include Equalizer.new(:code)
8
+
9
+ # @!attribute [rw] code
10
+ # Returns the pitch code
11
+ # @api public
12
+ # @example
13
+ # pitch_code.code #=> "B"
14
+ # @return [String] the pitch code
15
+ attribute :code, Shale::Type::String
16
+
17
+ # @!attribute [rw] description
18
+ # Returns the description
19
+ # @api public
20
+ # @example
21
+ # pitch_code.description #=> "Ball"
22
+ # @return [String] the description
23
+ attribute :description, Shale::Type::String
24
+
25
+ # @!attribute [rw] swing_status
26
+ # Returns whether the batter swung
27
+ # @api public
28
+ # @example
29
+ # pitch_code.swing_status #=> false
30
+ # @return [Boolean] whether the batter swung
31
+ attribute :swing_status, Shale::Type::Boolean
32
+
33
+ # @!attribute [rw] strike_status
34
+ # Returns whether this counts as a strike
35
+ # @api public
36
+ # @example
37
+ # pitch_code.strike_status #=> false
38
+ # @return [Boolean] whether this counts as a strike
39
+ attribute :strike_status, Shale::Type::Boolean
40
+
41
+ # @!attribute [rw] ball_status
42
+ # Returns whether this counts as a ball
43
+ # @api public
44
+ # @example
45
+ # pitch_code.ball_status #=> true
46
+ # @return [Boolean] whether this counts as a ball
47
+ attribute :ball_status, Shale::Type::Boolean
48
+
49
+ # @!attribute [rw] sort_order
50
+ # Returns the sort order
51
+ # @api public
52
+ # @example
53
+ # pitch_code.sort_order #=> 1
54
+ # @return [Integer] the sort order
55
+ attribute :sort_order, Shale::Type::Integer
56
+
57
+ # Returns whether the batter swung
58
+ #
59
+ # @api public
60
+ # @example
61
+ # pitch_code.swing? #=> false
62
+ # @return [Boolean] whether the batter swung
63
+ def swing? = swing_status
64
+
65
+ # Returns whether this counts as a strike
66
+ #
67
+ # @api public
68
+ # @example
69
+ # pitch_code.strike? #=> false
70
+ # @return [Boolean] whether this counts as a strike
71
+ def strike? = strike_status
72
+
73
+ # Returns whether this counts as a ball
74
+ #
75
+ # @api public
76
+ # @example
77
+ # pitch_code.ball? #=> true
78
+ # @return [Boolean] whether this counts as a ball
79
+ def ball? = ball_status
80
+
81
+ json do
82
+ map "code", to: :code
83
+ map "description", to: :description
84
+ map "swingStatus", to: :swing_status
85
+ map "strikeStatus", to: :strike_status
86
+ map "ballStatus", to: :ball_status
87
+ map "sortOrder", to: :sort_order
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "pitch_code"
3
+
4
+ module MLB
5
+ # Provides methods for fetching pitch codes from the API
6
+ #
7
+ # @example Fetch all pitch codes
8
+ # MLB::PitchCodes.all #=> [#<MLB::PitchCode>, ...]
9
+ class PitchCodes < Collection
10
+ collection endpoint: "pitchCodes", item_type: PitchCode, collection_name: :pitch_codes
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ require_relative "code_description_type"
2
+
3
+ module MLB
4
+ # Represents a pitch type (e.g., Fastball, Slider, Curveball)
5
+ class PitchType < CodeDescriptionType; end
6
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "pitch_type"
3
+
4
+ module MLB
5
+ # Provides methods for fetching pitch types from the API
6
+ #
7
+ # @example Fetch all pitch types
8
+ # MLB::PitchTypes.all #=> [#<MLB::PitchType>, ...]
9
+ class PitchTypes < Collection
10
+ collection endpoint: "pitchTypes", item_type: PitchType, collection_name: :pitch_types
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a platform
6
+ class Platform < Shale::Mapper
7
+ include Equalizer.new(:platform_code)
8
+
9
+ # @!attribute [rw] platform_code
10
+ # Returns the platform code
11
+ # @api public
12
+ # @example
13
+ # platform.platform_code #=> "web"
14
+ # @return [String] the platform code
15
+ attribute :platform_code, Shale::Type::String
16
+
17
+ # @!attribute [rw] platform_description
18
+ # Returns the platform description
19
+ # @api public
20
+ # @example
21
+ # platform.platform_description #=> "Web"
22
+ # @return [String] the platform description
23
+ attribute :platform_description, Shale::Type::String
24
+
25
+ json do
26
+ map "platformCode", to: :platform_code
27
+ map "platformDescription", to: :platform_description
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "platform"
3
+
4
+ module MLB
5
+ # Provides methods for fetching platforms from the API
6
+ #
7
+ # @example Fetch all platforms
8
+ # MLB::Platforms.all #=> [#<MLB::Platform>, ...]
9
+ class Platforms < Collection
10
+ collection endpoint: "platforms", item_type: Platform, collection_name: :platforms
11
+ end
12
+ end
data/lib/mlb/play.rb ADDED
@@ -0,0 +1,300 @@
1
+ require "shale"
2
+ require_relative "player"
3
+ require_relative "handedness"
4
+
5
+ module MLB
6
+ # Represents the result of a play
7
+ class PlayResult < Shale::Mapper
8
+ # @!attribute [rw] type
9
+ # Returns the result type
10
+ # @api public
11
+ # @example
12
+ # result.type #=> "atBat"
13
+ # @return [String] the result type
14
+ attribute :type, Shale::Type::String
15
+
16
+ # @!attribute [rw] event
17
+ # Returns the event name
18
+ # @api public
19
+ # @example
20
+ # result.event #=> "Groundout"
21
+ # @return [String] the event name
22
+ attribute :event, Shale::Type::String
23
+
24
+ # @!attribute [rw] event_type
25
+ # Returns the event type code
26
+ # @api public
27
+ # @example
28
+ # result.event_type #=> "field_out"
29
+ # @return [String] the event type code
30
+ attribute :event_type, Shale::Type::String
31
+
32
+ # @!attribute [rw] description
33
+ # Returns the description
34
+ # @api public
35
+ # @example
36
+ # result.description #=> "Francisco Lindor grounds out..."
37
+ # @return [String] the description
38
+ attribute :description, Shale::Type::String
39
+
40
+ # @!attribute [rw] rbi
41
+ # Returns the RBIs on this play
42
+ # @api public
43
+ # @example
44
+ # result.rbi #=> 0
45
+ # @return [Integer] the RBIs
46
+ attribute :rbi, Shale::Type::Integer
47
+
48
+ # @!attribute [rw] away_score
49
+ # Returns the away score after this play
50
+ # @api public
51
+ # @example
52
+ # result.away_score #=> 0
53
+ # @return [Integer] the away score
54
+ attribute :away_score, Shale::Type::Integer
55
+
56
+ # @!attribute [rw] home_score
57
+ # Returns the home score after this play
58
+ # @api public
59
+ # @example
60
+ # result.home_score #=> 0
61
+ # @return [Integer] the home score
62
+ attribute :home_score, Shale::Type::Integer
63
+
64
+ # @!attribute [rw] is_out
65
+ # Returns whether this play resulted in an out
66
+ # @api public
67
+ # @example
68
+ # result.out? #=> true
69
+ # @return [Boolean] whether an out occurred
70
+ attribute :is_out, Shale::Type::Boolean
71
+
72
+ # Returns whether this play resulted in an out
73
+ #
74
+ # @api public
75
+ # @example
76
+ # result.out? #=> true
77
+ # @return [Boolean] whether an out occurred
78
+ def out?
79
+ is_out
80
+ end
81
+
82
+ json do
83
+ map "type", to: :type
84
+ map "event", to: :event
85
+ map "eventType", to: :event_type
86
+ map "description", to: :description
87
+ map "rbi", to: :rbi
88
+ map "awayScore", to: :away_score
89
+ map "homeScore", to: :home_score
90
+ map "isOut", to: :is_out
91
+ end
92
+ end
93
+
94
+ # Represents information about a play
95
+ class PlayAbout < Shale::Mapper
96
+ # @!attribute [rw] at_bat_index
97
+ # Returns the at bat index
98
+ # @api public
99
+ # @example
100
+ # about.at_bat_index #=> 0
101
+ # @return [Integer] the at bat index
102
+ attribute :at_bat_index, Shale::Type::Integer
103
+
104
+ # @!attribute [rw] half_inning
105
+ # Returns the half inning
106
+ # @api public
107
+ # @example
108
+ # about.half_inning #=> "top"
109
+ # @return [String] the half inning
110
+ attribute :half_inning, Shale::Type::String
111
+
112
+ # @!attribute [rw] is_top_inning
113
+ # Returns whether it's the top of the inning
114
+ # @api public
115
+ # @example
116
+ # about.top_inning? #=> true
117
+ # @return [Boolean] whether top of inning
118
+ attribute :is_top_inning, Shale::Type::Boolean
119
+
120
+ # @!attribute [rw] inning
121
+ # Returns the inning number
122
+ # @api public
123
+ # @example
124
+ # about.inning #=> 1
125
+ # @return [Integer] the inning
126
+ attribute :inning, Shale::Type::Integer
127
+
128
+ # @!attribute [rw] is_complete
129
+ # Returns whether the play is complete
130
+ # @api public
131
+ # @example
132
+ # about.complete? #=> true
133
+ # @return [Boolean] whether complete
134
+ attribute :is_complete, Shale::Type::Boolean
135
+
136
+ # @!attribute [rw] is_scoring_play
137
+ # Returns whether this is a scoring play
138
+ # @api public
139
+ # @example
140
+ # about.scoring_play? #=> false
141
+ # @return [Boolean] whether a scoring play
142
+ attribute :is_scoring_play, Shale::Type::Boolean
143
+
144
+ # Returns whether it's the top of the inning
145
+ #
146
+ # @api public
147
+ # @example
148
+ # about.top_inning? #=> true
149
+ # @return [Boolean] whether top of inning
150
+ def top_inning?
151
+ is_top_inning
152
+ end
153
+
154
+ # Returns whether the play is complete
155
+ #
156
+ # @api public
157
+ # @example
158
+ # about.complete? #=> true
159
+ # @return [Boolean] whether complete
160
+ def complete?
161
+ is_complete
162
+ end
163
+
164
+ # Returns whether this is a scoring play
165
+ #
166
+ # @api public
167
+ # @example
168
+ # about.scoring_play? #=> false
169
+ # @return [Boolean] whether a scoring play
170
+ def scoring_play?
171
+ is_scoring_play
172
+ end
173
+
174
+ json do
175
+ map "atBatIndex", to: :at_bat_index
176
+ map "halfInning", to: :half_inning
177
+ map "isTopInning", to: :is_top_inning
178
+ map "inning", to: :inning
179
+ map "isComplete", to: :is_complete
180
+ map "isScoringPlay", to: :is_scoring_play
181
+ end
182
+ end
183
+
184
+ # Represents the count during a play
185
+ class PlayCount < Shale::Mapper
186
+ # @!attribute [rw] balls
187
+ # Returns the ball count
188
+ # @api public
189
+ # @example
190
+ # count.balls #=> 1
191
+ # @return [Integer] the balls
192
+ attribute :balls, Shale::Type::Integer
193
+
194
+ # @!attribute [rw] strikes
195
+ # Returns the strike count
196
+ # @api public
197
+ # @example
198
+ # count.strikes #=> 1
199
+ # @return [Integer] the strikes
200
+ attribute :strikes, Shale::Type::Integer
201
+
202
+ # @!attribute [rw] outs
203
+ # Returns the out count
204
+ # @api public
205
+ # @example
206
+ # count.outs #=> 1
207
+ # @return [Integer] the outs
208
+ attribute :outs, Shale::Type::Integer
209
+
210
+ json do
211
+ map "balls", to: :balls
212
+ map "strikes", to: :strikes
213
+ map "outs", to: :outs
214
+ end
215
+ end
216
+
217
+ # Represents the matchup during a play
218
+ class PlayMatchup < Shale::Mapper
219
+ # @!attribute [rw] batter
220
+ # Returns the batter
221
+ # @api public
222
+ # @example
223
+ # matchup.batter #=> #<MLB::Player>
224
+ # @return [Player] the batter
225
+ attribute :batter, Player
226
+
227
+ # @!attribute [rw] bat_side
228
+ # Returns the bat side
229
+ # @api public
230
+ # @example
231
+ # matchup.bat_side #=> #<MLB::Handedness>
232
+ # @return [Handedness] the bat side
233
+ attribute :bat_side, Handedness
234
+
235
+ # @!attribute [rw] pitcher
236
+ # Returns the pitcher
237
+ # @api public
238
+ # @example
239
+ # matchup.pitcher #=> #<MLB::Player>
240
+ # @return [Player] the pitcher
241
+ attribute :pitcher, Player
242
+
243
+ # @!attribute [rw] pitch_hand
244
+ # Returns the pitch hand
245
+ # @api public
246
+ # @example
247
+ # matchup.pitch_hand #=> #<MLB::Handedness>
248
+ # @return [Handedness] the pitch hand
249
+ attribute :pitch_hand, Handedness
250
+
251
+ json do
252
+ map "batter", to: :batter
253
+ map "batSide", to: :bat_side
254
+ map "pitcher", to: :pitcher
255
+ map "pitchHand", to: :pitch_hand
256
+ end
257
+ end
258
+
259
+ # Represents a play in a game
260
+ class Play < Shale::Mapper
261
+ # @!attribute [rw] result
262
+ # Returns the play result
263
+ # @api public
264
+ # @example
265
+ # play.result #=> #<MLB::PlayResult>
266
+ # @return [PlayResult] the result
267
+ attribute :result, PlayResult
268
+
269
+ # @!attribute [rw] about
270
+ # Returns the play info
271
+ # @api public
272
+ # @example
273
+ # play.about #=> #<MLB::PlayAbout>
274
+ # @return [PlayAbout] the info
275
+ attribute :about, PlayAbout
276
+
277
+ # @!attribute [rw] count
278
+ # Returns the count
279
+ # @api public
280
+ # @example
281
+ # play.count #=> #<MLB::PlayCount>
282
+ # @return [PlayCount] the count
283
+ attribute :count, PlayCount
284
+
285
+ # @!attribute [rw] matchup
286
+ # Returns the matchup
287
+ # @api public
288
+ # @example
289
+ # play.matchup #=> #<MLB::PlayMatchup>
290
+ # @return [PlayMatchup] the matchup
291
+ attribute :matchup, PlayMatchup
292
+
293
+ json do
294
+ map "result", to: :result
295
+ map "about", to: :about
296
+ map "count", to: :count
297
+ map "matchup", to: :matchup
298
+ end
299
+ end
300
+ end
@@ -0,0 +1,52 @@
1
+ require "shale"
2
+ require_relative "play"
3
+
4
+ module MLB
5
+ # Provides methods for fetching play-by-play data from the API
6
+ class PlayByPlay < Shale::Mapper
7
+ # @!attribute [rw] all_plays
8
+ # Returns all plays in the game
9
+ # @api public
10
+ # @example
11
+ # pbp.all_plays #=> [#<MLB::Play>, ...]
12
+ # @return [Array<Play>] all plays
13
+ attribute :all_plays, Play, collection: true
14
+
15
+ # @!attribute [rw] current_play
16
+ # Returns the current play
17
+ # @api public
18
+ # @example
19
+ # pbp.current_play #=> #<MLB::Play>
20
+ # @return [Play] the current play
21
+ attribute :current_play, Play
22
+
23
+ # @!attribute [rw] scoring_plays
24
+ # Returns indices of scoring plays
25
+ # @api public
26
+ # @example
27
+ # pbp.scoring_plays #=> [5, 12, 23]
28
+ # @return [Array<Integer>] scoring play indices
29
+ attribute :scoring_plays, Shale::Type::Integer, collection: true
30
+
31
+ json do
32
+ map "allPlays", to: :all_plays
33
+ map "currentPlay", to: :current_play
34
+ map "scoringPlays", to: :scoring_plays
35
+ end
36
+
37
+ # Retrieves play-by-play data for a game
38
+ #
39
+ # @api public
40
+ # @example Get play-by-play for a game
41
+ # MLB::PlayByPlay.find(game: 745571)
42
+ # @example Get play-by-play for a scheduled game
43
+ # MLB::PlayByPlay.find(game: ScheduledGame.new(game_pk: 745571))
44
+ # @param game [Integer, ScheduledGame] the game ID or game object
45
+ # @return [PlayByPlay] the play-by-play data
46
+ def self.find(game:)
47
+ game_pk = game.respond_to?(:game_pk) ? game.game_pk : game
48
+ response = CLIENT.get("game/#{game_pk}/playByPlay")
49
+ from_json(response)
50
+ end
51
+ end
52
+ end