Class: EventLog

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

Overview

Parses the MLB Gameday eventLog.xml file

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#away_eventsObject

Returns the value of attribute away_events.



9
10
11
# File 'lib/event_log.rb', line 9

def away_events
  @away_events
end

#away_teamObject

Returns the value of attribute away_team.



8
9
10
# File 'lib/event_log.rb', line 8

def away_team
  @away_team
end

#gidObject

Returns the value of attribute gid.



8
9
10
# File 'lib/event_log.rb', line 8

def gid
  @gid
end

#home_eventsObject

Returns the value of attribute home_events.



9
10
11
# File 'lib/event_log.rb', line 9

def home_events
  @home_events
end

#home_teamObject

Returns the value of attribute home_team.



8
9
10
# File 'lib/event_log.rb', line 8

def home_team
  @home_team
end

#max_inningObject

Returns the value of attribute max_inning.



10
11
12
# File 'lib/event_log.rb', line 10

def max_inning
  @max_inning
end

Instance Method Details

#dumpObject



71
72
73
74
75
# File 'lib/event_log.rb', line 71

def dump
	(1..@max_inning).each do |inning|
		dump_inning(inning.to_s)
	end
end

#dump_inning(inning) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/event_log.rb', line 78

def dump_inning(inning)
	puts 'Inning - ' + inning.to_s
	events = events_by_inning(inning)
	events.each do |event|
		puts event.description
	end
end

#dump_to_fileObject

NOT IMPLEMENTED YET



88
89
90
# File 'lib/event_log.rb', line 88

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

#events_by_inning(inning) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/event_log.rb', line 55

def events_by_inning(inning)
	events = []
	@away_events.each do |event|
		if event.inning == inning
		  events << event
		end
	end
	@home_events.each do |event|
		if event.inning == inning
		  events << event
		end
	end
	events
end

#load_from_id(gid) ⇒ Object

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



14
15
16
17
18
19
20
21
22
# File 'lib/event_log.rb', line 14

def load_from_id(gid)
  @gid = gid
  @xml_data = GamedayFetcher.fetch_eventlog(gid)
  @xml_doc = REXML::Document.new(@xml_data)
  if @xml_doc.root
    set_teams
    set_events
  end
end

#set_eventsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/event_log.rb', line 36

def set_events
  @home_events, @away_events = [], []
  @max_inning = 0
  # set away team events
  @xml_doc.elements.each("game/team[@home_team='false']/event") { |element| 
    event = Event.new
    event.load(element, 'away')
    @max_inning = event.inning.to_i unless event.inning.to_i < @max_inning
    @away_events.push event
  }
  # set home team events
  @xml_doc.elements.each("game/team[@home_team='true']/event") { |element| 
    event = Event.new
    event.load(element, 'home')
    @home_events.push event
  }
end

#set_teamsObject

Sets the team names for the teams involved in this game



26
27
28
29
30
31
32
33
# File 'lib/event_log.rb', line 26

def set_teams
	@xml_doc.elements.each("game/team[@home_team='false']") do |element| 
		@away_team = element.attributes["name"]
	end
	@xml_doc.elements.each("game/team[@home_team='true']") do |element| 
		@home_team = element.attributes["name"]
	end
end

#to_html(template_filename) ⇒ Object

NOT IMPLEMENTED YET Converts the eventlog into a formatted HTML representation. Relies on the eventlog.html.erb template for describing the layout



96
97
98
99
100
# File 'lib/event_log.rb', line 96

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