Class: BoxScore

Inherits:
Object
  • Object
show all
Defined in:
lib/box_score.rb

Overview

Parses the MLB Gameday XML representation of a boxscore and returns data in easy to use arrays and hashes. Can be used along with a view template to easily create a displayable box score.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#away_batting_textObject

Returns the value of attribute away_batting_text.



16
17
18
# File 'lib/box_score.rb', line 16

def away_batting_text
  @away_batting_text
end

#away_fnameObject

Returns the value of attribute away_fname.



19
20
21
# File 'lib/box_score.rb', line 19

def away_fname
  @away_fname
end

#away_idObject

Returns the value of attribute away_id.



19
20
21
# File 'lib/box_score.rb', line 19

def away_id
  @away_id
end

#away_lossObject

Returns the value of attribute away_loss.



20
21
22
# File 'lib/box_score.rb', line 20

def away_loss
  @away_loss
end

#away_runsObject

Returns the value of attribute away_runs.



22
23
24
# File 'lib/box_score.rb', line 22

def away_runs
  @away_runs
end

#away_snameObject

Returns the value of attribute away_sname.



19
20
21
# File 'lib/box_score.rb', line 19

def away_sname
  @away_sname
end

#away_team_codeObject

Returns the value of attribute away_team_code.



18
19
20
# File 'lib/box_score.rb', line 18

def away_team_code
  @away_team_code
end

#away_winsObject

Returns the value of attribute away_wins.



20
21
22
# File 'lib/box_score.rb', line 20

def away_wins
  @away_wins
end

#battersObject

complex attributes



27
28
29
# File 'lib/box_score.rb', line 27

def batters
  @batters
end

#citiesObject

complex attributes



27
28
29
# File 'lib/box_score.rb', line 27

def cities
  @cities
end

#dateObject

Returns the value of attribute date.



20
21
22
# File 'lib/box_score.rb', line 20

def date
  @date
end

#gameObject

Returns the value of attribute game.



16
17
18
# File 'lib/box_score.rb', line 16

def game
  @game
end

#game_idObject

Returns the value of attribute game_id.



18
19
20
# File 'lib/box_score.rb', line 18

def game_id
  @game_id
end

#game_infoObject

Returns the value of attribute game_info.



16
17
18
# File 'lib/box_score.rb', line 16

def game_info
  @game_info
end

#game_pkObject

Returns the value of attribute game_pk.



18
19
20
# File 'lib/box_score.rb', line 18

def game_pk
  @game_pk
end

#gidObject

Returns the value of attribute gid.



16
17
18
# File 'lib/box_score.rb', line 16

def gid
  @gid
end

#home_batting_textObject

Returns the value of attribute home_batting_text.



16
17
18
# File 'lib/box_score.rb', line 16

def home_batting_text
  @home_batting_text
end

#home_fnameObject

Returns the value of attribute home_fname.



19
20
21
# File 'lib/box_score.rb', line 19

def home_fname
  @home_fname
end

#home_idObject

Returns the value of attribute home_id.



19
20
21
# File 'lib/box_score.rb', line 19

def home_id
  @home_id
end

#home_lossObject

Returns the value of attribute home_loss.



20
21
22
# File 'lib/box_score.rb', line 20

def home_loss
  @home_loss
end

#home_runsObject

Returns the value of attribute home_runs.



22
23
24
# File 'lib/box_score.rb', line 22

def home_runs
  @home_runs
end

#home_snameObject

Returns the value of attribute home_sname.



19
20
21
# File 'lib/box_score.rb', line 19

def home_sname
  @home_sname
end

#home_sport_codeObject

Returns the value of attribute home_sport_code.



18
19
20
# File 'lib/box_score.rb', line 18

def home_sport_code
  @home_sport_code
end

#home_team_codeObject

Returns the value of attribute home_team_code.



18
19
20
# File 'lib/box_score.rb', line 18

def home_team_code
  @home_team_code
end

#home_winsObject

Returns the value of attribute home_wins.



20
21
22
# File 'lib/box_score.rb', line 20

def home_wins
  @home_wins
end

#inningsObject

complex attributes



27
28
29
# File 'lib/box_score.rb', line 27

def innings
  @innings
end

#linescoreObject

Returns the value of attribute linescore.



16
17
18
# File 'lib/box_score.rb', line 16

def linescore
  @linescore
end

#linescore_totalsObject

complex attributes



27
28
29
# File 'lib/box_score.rb', line 27

def linescore_totals
  @linescore_totals
end

#pitchersObject

complex attributes



27
28
29
# File 'lib/box_score.rb', line 27

def pitchers
  @pitchers
end

#status_indObject

Returns the value of attribute status_ind.



20
21
22
# File 'lib/box_score.rb', line 20

def status_ind
  @status_ind
end

#tempObject

Returns the value of attribute temp.



24
25
26
# File 'lib/box_score.rb', line 24

def temp
  @temp
end

#wind_dirObject

Returns the value of attribute wind_dir.



24
25
26
# File 'lib/box_score.rb', line 24

def wind_dir
  @wind_dir
end

#wind_speedObject

Returns the value of attribute wind_speed.



24
25
26
# File 'lib/box_score.rb', line 24

def wind_speed
  @wind_speed
end

#xml_dataObject

Returns the value of attribute xml_data.



16
17
18
# File 'lib/box_score.rb', line 16

def xml_data
  @xml_data
end

Instance Method Details

#dump_to_fileObject

Saves an HTML version of the boxscore



54
55
56
# File 'lib/box_score.rb', line 54

def dump_to_file
  GamedayUtil.save_file("boxscore.html", to_html('boxscore.html.erb'))
end

#find_hitters(search_string) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/box_score.rb', line 84

def find_hitters(search_string)
  results = []
  away = @xml_doc.elements["boxscore/batting[@team_flag='away']/#{search_string}"]
  away_batter = BattingAppearance.new
  away_batter.init(away)
  results << away_batter
  home = @xml_doc.elements["boxscore/batting[@team_flag='home']/#{search_string}"]
  home_batter = BattingAppearance.new
  home_batter.init(home)
  results << home_batter
  results
end

#get_cleanup_hittersObject

Returns a 2 element array of cleanup hitters for this game.

[0] = visitor's cleanup hitter
[1] = home's cleanup hitter


79
80
81
# File 'lib/box_score.rb', line 79

def get_cleanup_hitters
  find_hitters("batter[@bo='400']")
end

#get_leadoff_hittersObject

Returns a 2 element array of leadoff hitters for this game.

[0] = visitor's leadoff hitter
[1] = home's leadoff hitter


71
72
73
# File 'lib/box_score.rb', line 71

def get_leadoff_hitters
  find_hitters("batter")
end

#load_from_id(gid) ⇒ Object

Loads the boxscore XML from the MLB gameday server and parses it using REXML



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/box_score.rb', line 31

def load_from_id(gid)
  @gid = gid
  @xml_data = GamedayFetcher.fetch_boxscore(gid)
  @xml_doc = REXML::Document.new(@xml_data)
  if @xml_doc.root
    @game = Game.new(@gid)
    @game.boxscore = self
    set_basic_info
    @linescore = LineScore.new
    @linescore.init(@xml_doc.root.elements["linescore"])
    @home_runs = @linescore.home_team_runs
    @away_runs = @linescore.away_team_runs
    @game_info = @xml_doc.root.elements["game_info"].text
   set_batting_text
    set_cities
    set_pitchers
    set_batters
    set_weather
  end
end

#to_html(template_filename) ⇒ Object

Converts the boxscore into a formatted HTML representation. Relies on the boxscore.html.erb template for describing the layout



61
62
63
64
65
# File 'lib/box_score.rb', line 61

def to_html(template_filename)
  gameday_info = GamedayUtil.parse_gameday_id('gid_' + gid)
  template = ERB.new File.new(File.expand_path(File.dirname(__FILE__) + "/" + template_filename)).read, nil, "%"  
  return template.result(binding)
end