Class: Sportradar::Api::Baseball::Event::AtBat
- Defined in:
- lib/sportradar/api/baseball/events/at_bat.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#event ⇒ Object
Returns the value of attribute event.
-
#hitter_hand ⇒ Object
Returns the value of attribute hitter_hand.
-
#hitter_id ⇒ Object
Returns the value of attribute hitter_id.
-
#id ⇒ Object
Returns the value of attribute id.
-
#outcome ⇒ Object
Returns the value of attribute outcome.
-
#pitcher_hand ⇒ Object
Returns the value of attribute pitcher_hand.
-
#pitcher_id ⇒ Object
Returns the value of attribute pitcher_id.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #data_key ⇒ Object
- #double? ⇒ Boolean
- #hit? ⇒ Boolean
- #homerun? ⇒ Boolean
-
#initialize(data, **opts) ⇒ AtBat
constructor
A new instance of AtBat.
-
#outcome_id ⇒ Object
def ==(other) return false if other.nil? @id == other.id && pitches == other.pitches end.
- #over? ⇒ Boolean
- #pitches ⇒ Object
- #runs ⇒ Object
- #runs? ⇒ Boolean
- #single? ⇒ Boolean
- #strikeout? ⇒ Boolean
- #triple? ⇒ Boolean
- #update(data, **opts) ⇒ Object
- #walk? ⇒ Boolean
Methods inherited from Data
#all_attributes, #attributes, #create_data, #parse_into_array, #parse_into_array_with_options, #parse_out_hashes, #structure_links, #update_data
Constructor Details
#initialize(data, **opts) ⇒ AtBat
Returns a new instance of AtBat.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 8 def initialize(data, **opts) @response = data @api = opts[:api] @event = opts[:event] @id = data["id"] @type = data['type'] @pitches_hash = {} update(data) end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def description @description end |
#event ⇒ Object
Returns the value of attribute event.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def event @event end |
#hitter_hand ⇒ Object
Returns the value of attribute hitter_hand.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def hitter_hand @hitter_hand end |
#hitter_id ⇒ Object
Returns the value of attribute hitter_id.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def hitter_id @hitter_id end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def id @id end |
#outcome ⇒ Object
Returns the value of attribute outcome.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def outcome @outcome end |
#pitcher_hand ⇒ Object
Returns the value of attribute pitcher_hand.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def pitcher_hand @pitcher_hand end |
#pitcher_id ⇒ Object
Returns the value of attribute pitcher_id.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def pitcher_id @pitcher_id end |
#response ⇒ Object
Returns the value of attribute response.
6 7 8 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6 def response @response end |
Instance Method Details
#data_key ⇒ Object
83 84 85 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 83 def data_key 'at_bat' end |
#double? ⇒ Boolean
43 44 45 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 43 def double? outcome.to_s.include?('Double') end |
#hit? ⇒ Boolean
33 34 35 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 33 def hit? single? || double? || triple? || homerun? end |
#homerun? ⇒ Boolean
51 52 53 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 51 def homerun? pitches.last&.homerun? end |
#outcome_id ⇒ Object
def ==(other)
return false if other.nil?
@id == other.id && pitches == other.pitches
end
25 26 27 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 25 def outcome_id pitches.last&.outcome_id end |
#over? ⇒ Boolean
87 88 89 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 87 def over? pitches.last&.is_ab_over end |
#pitches ⇒ Object
92 93 94 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 92 def pitches @pitches_hash.values end |
#runs ⇒ Object
67 68 69 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 67 def runs pitches.flat_map {|pitch| pitch.runners&.select {|x| x.ending_base == 4 } }.compact end |
#runs? ⇒ Boolean
55 56 57 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 55 def runs? runs.any? end |
#single? ⇒ Boolean
39 40 41 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 39 def single? outcome.to_s.include?('Single') end |
#strikeout? ⇒ Boolean
59 60 61 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 59 def strikeout? pitches.last&.count.dig('strikes') == 3 end |
#triple? ⇒ Boolean
47 48 49 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 47 def triple? outcome.to_s.include?('Triple') end |
#update(data, **opts) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 71 def update(data, **opts) @description = data['description'] if data['description'] @hitter_id = data['hitter_id'] if data['hitter_id'] @pitcher_id = data['pitcher_id'] if data['pitcher_id'] @hitter_hand = data['hitter_hand'] if data['hitter_hand'] @pitcher_hand = data['pitcher_hand'] if data['pitcher_hand'] # this hasn't been checked yet # pitch events pitches = data.dig('events').select {|pitch| pitch["type"] == 'pitch' } create_data(@pitches_hash, pitches, klass: Pitch, api: @api, at_bat: self) end |
#walk? ⇒ Boolean
63 64 65 |
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 63 def walk? over? && pitches.last&.ball? end |