Class: Sportradar::Api::Baseball::Scoring

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/baseball/scoring.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) ⇒ Scoring

Returns a new instance of Scoring.



7
8
9
10
11
12
13
14
15
# File 'lib/sportradar/api/baseball/scoring.rb', line 7

def initialize(data, **opts)
  @api      = opts[:api]
  @game     = opts[:game]
  
  @scores = {}
  @id = data['id']
  
  update(data, **opts)
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



5
6
7
# File 'lib/sportradar/api/baseball/scoring.rb', line 5

def api
  @api
end

#awayObject

Returns the value of attribute away.



5
6
7
# File 'lib/sportradar/api/baseball/scoring.rb', line 5

def away
  @away
end

#homeObject

Returns the value of attribute home.



5
6
7
# File 'lib/sportradar/api/baseball/scoring.rb', line 5

def home
  @home
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/sportradar/api/baseball/scoring.rb', line 5

def id
  @id
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/sportradar/api/baseball/scoring.rb', line 5

def response
  @response
end

#scoresObject

Returns the value of attribute scores.



5
6
7
# File 'lib/sportradar/api/baseball/scoring.rb', line 5

def scores
  @scores
end

Instance Method Details

#errors(team_id) ⇒ Object



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

def errors(team_id)
  @scores.dig(team_id, 'errors').to_i
end

#hits(team_id) ⇒ Object



48
49
50
# File 'lib/sportradar/api/baseball/scoring.rb', line 48

def hits(team_id)
  @scores.dig(team_id, 'hits').to_i
end

#runs(team_id) ⇒ Object Also known as: points



45
46
47
# File 'lib/sportradar/api/baseball/scoring.rb', line 45

def runs(team_id)
  @scores.dig(team_id, 'runs').to_i
end

#update(data, source: nil, **opts) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sportradar/api/baseball/scoring.rb', line 17

def update(data, source: nil, **opts)
  new_scores = case source
  when :box
    parse_from_box(data)
  when :pbp
    parse_from_pbp(data)
  when :summary
    parse_from_box(data)
  when :rhe
    data
  else
    # if data['quarter'] || data['half']
    #   parse_from_pbp(data)
    # elsif data['team']
    #   parse_from_box(data)
    # else # schedule requests
    #   {}
    # end
    {}
  end
  # parse data structure
  # handle data from team (all quarters)
  # handle data from quarter (both teams)
  # handle data from game?
  @scores.each { |k, v| v.merge!(new_scores.delete(k) || {} ) }
  new_scores.each { |k, v| @scores.merge!(k => v) }
end