class MLBGameday::API
Attributes
leagues[R]
Public Class Methods
new()
click to toggle source
# File lib/mlb_gameday.rb, line 24 def initialize @data = Psych.load File.open File.join( File.dirname(File.expand_path(__FILE__)), '../resources/data.yml' ) @leagues = @data[:leagues] end
Public Instance Methods
batter(id, year: nil)
click to toggle source
# File lib/mlb_gameday.rb, line 68 def batter(id, year: nil) return if id.empty? MLBGameday::Batter.new id: id, xml: batter_xml(id, year: year) end
batter_xml(id, year: nil)
click to toggle source
# File lib/mlb_gameday.rb, line 119 def batter_xml(id, year: nil) # We only really want one piece of data from this file. This gives us # the GID of their most recent appearance. gid = fetch_xml(BATTER, id: id, year: (year || Date.today.year)) .xpath('//batting/@game_id').text.gsub(/[^a-z0-9]/, '_') fetch_xml "#{GAME_FOLDER}/batters/%<batter>s", gid: gid, batter: id end
division(league, name)
click to toggle source
# File lib/mlb_gameday.rb, line 53 def division(league, name) @leagues[league][name] end
divisions()
click to toggle source
# File lib/mlb_gameday.rb, line 57 def divisions @divisions ||= @leagues[:AL].divisions.values + @leagues[:NL].divisions.values end
find_games(team: nil, date: nil)
click to toggle source
# File lib/mlb_gameday.rb, line 84 def find_games(team: nil, date: nil) doc = scoreboard_xml(date || Date.today) if team code = team(team).code doc.xpath('//games/game').map do |game| next unless [game.xpath('@home_name_abbrev').text, game.xpath('@away_name_abbrev').text].include? code game game.xpath('@gameday_link').text end.compact! else doc.xpath('//games/game').map do |game| game game.xpath('@gameday_link').to_s end end end
game(gid)
click to toggle source
# File lib/mlb_gameday.rb, line 74 def game(gid) MLBGameday::Game.new( self, gid, gamecenter: gamecenter_xml(gid), linescore: linescore_xml(gid), rawboxscore: rawboxscore_xml(gid) ) end
gamecenter_xml(gid)
click to toggle source
# File lib/mlb_gameday.rb, line 115 def gamecenter_xml(gid) fetch_xml "#{GAME_FOLDER}/gamecenter", gid: gid end
league(name)
click to toggle source
# File lib/mlb_gameday.rb, line 32 def league(name) return name if name.is_a? MLBGameday::League @leagues[name] end
linescore_xml(gid)
click to toggle source
# File lib/mlb_gameday.rb, line 107 def linescore_xml(gid) fetch_xml "#{GAME_FOLDER}/linescore", gid: gid end
miscellaneous_teams()
click to toggle source
# File lib/mlb_gameday.rb, line 49 def miscellaneous_teams @data[:miscellaneous_teams].values end
pitcher(id, year: nil)
click to toggle source
# File lib/mlb_gameday.rb, line 62 def pitcher(id, year: nil) return if id.empty? MLBGameday::Pitcher.new id: id, xml: pitcher_xml(id, year: year) end
pitcher_xml(id, year: nil)
click to toggle source
# File lib/mlb_gameday.rb, line 128 def pitcher_xml(id, year: nil) # We only really want one piece of data from this file. This gives us # the GID of their most recent appearance. gid = fetch_xml(PITCHER, id: id, year: (year || Date.today.year)) .xpath('//pitching/@game_id').text.gsub(/[^a-z0-9]/, '_') fetch_xml "#{GAME_FOLDER}/pitchers/%<pitcher>s", gid: gid, pitcher: id end
rawboxscore_xml(gid)
click to toggle source
# File lib/mlb_gameday.rb, line 111 def rawboxscore_xml(gid) fetch_xml "#{GAME_FOLDER}/rawboxscore", gid: gid end
scoreboard_xml(date)
click to toggle source
# File lib/mlb_gameday.rb, line 103 def scoreboard_xml(date) fetch_xml date.strftime SCOREBOARD end
team(name)
click to toggle source
# File lib/mlb_gameday.rb, line 38 def team(name) return name if name.is_a? MLBGameday::Team teams.find { |team| team.called?(name) } end
teams()
click to toggle source
# File lib/mlb_gameday.rb, line 44 def teams @teams ||= divisions.map(&:teams).map(&:values).flatten + miscellaneous_teams end
Protected Instance Methods
fetch_xml(path, interpolations = {})
click to toggle source
# File lib/mlb_gameday.rb, line 139 def fetch_xml(path, interpolations = {}) full_path = "#{API_URL}#{path}.xml" if interpolations[:gid] year, month, day, = interpolations[:gid].split '_' interpolations[:year] = year interpolations[:month] = month interpolations[:day] = day end full_path = format(full_path, interpolations) if interpolations.any? Nokogiri::XML open full_path rescue nil end