Class: Sportradar::Api::Baseball::Event::AtBat

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/baseball/events/at_bat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6

def description
  @description
end

#eventObject

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_handObject

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_idObject

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

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 6

def id
  @id
end

#outcomeObject

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_handObject

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_idObject

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

#responseObject

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_keyObject



83
84
85
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 83

def data_key
  'at_bat'
end

#double?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


33
34
35
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 33

def hit?
  single? || double? || triple? || homerun?
end

#homerun?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 51

def homerun?
  pitches.last&.homerun?
end

#outcome_idObject

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

Returns:

  • (Boolean)


87
88
89
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 87

def over?
  pitches.last&.is_ab_over
end

#pitchesObject



92
93
94
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 92

def pitches
  @pitches_hash.values
end

#runsObject



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

Returns:

  • (Boolean)


55
56
57
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 55

def runs?
  runs.any?
end

#single?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


63
64
65
# File 'lib/sportradar/api/baseball/events/at_bat.rb', line 63

def walk?
  over? && pitches.last&.ball?
end