Class: MLBGameday::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAPI



26
27
28
29
30
# File 'lib/mlb_gameday.rb', line 26

def initialize
  @leagues = Psych.load File.open File.join(
    File.dirname(File.expand_path(__FILE__)), '../resources/data.yml'
  )
end

Instance Attribute Details

#leaguesObject (readonly)

Returns the value of attribute leagues.



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

def leagues
  @leagues
end

Instance Method Details

#batter(id, year: nil) ⇒ Object



64
65
66
67
68
# File 'lib/mlb_gameday.rb', line 64

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) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/mlb_gameday.rb', line 121

def batter_xml(id, year: nil)
  # We only really want one piece of data from this file...
  year_data = fetch_xml BATTER, id: id, year: (year || Date.today.year)

  gid = year_data.xpath('//batting/@game_id').text
  year, month, day, = gid.split '/'

  fetch_xml "/year_#{year}/month_#{month}/day_#{day}/" \
            "gid_#{gid.gsub(/[^a-z0-9]/, '_')}/batters/#{id}"
end

#division(league, name) ⇒ Object



49
50
51
# File 'lib/mlb_gameday.rb', line 49

def division(league, name)
  @leagues[league][name]
end

#divisionsObject



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

def divisions
  @divisions ||= @leagues[:AL].divisions.values +
                 @leagues[:NL].divisions.values
end

#find_games(team: nil, date: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mlb_gameday.rb', line 80

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) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/mlb_gameday.rb', line 70

def game(gid)
  MLBGameday::Game.new(
    self,
    gid,
    gamecenter: gamecenter_xml(gid),
    linescore: linescore_xml(gid),
    rawboxscore: rawboxscore_xml(gid)
  )
end

#gamecenter_xml(gid) ⇒ Object



115
116
117
118
119
# File 'lib/mlb_gameday.rb', line 115

def gamecenter_xml(gid)
  year, month, day, = gid.split '_'

  fetch_xml GAMECENTER, year: year, month: month, day: day, gid: gid
end

#league(name) ⇒ Object



32
33
34
35
36
# File 'lib/mlb_gameday.rb', line 32

def league(name)
  return name if name.is_a? MLBGameday::League

  @leagues[name]
end

#linescore_xml(gid) ⇒ Object



103
104
105
106
107
# File 'lib/mlb_gameday.rb', line 103

def linescore_xml(gid)
  year, month, day, = gid.split '_'

  fetch_xml LINESCORE, year: year, month: month, day: day, gid: gid
end

#pitcher(id, year: nil) ⇒ Object



58
59
60
61
62
# File 'lib/mlb_gameday.rb', line 58

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) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/mlb_gameday.rb', line 132

def pitcher_xml(id, year: nil)
  # We only really want one piece of data from this file...
  year_data = fetch_xml PITCHER, id: id, year: (year || Date.today.year)

  gid = year_data.xpath('//pitching/@game_id').text
  year, month, day, = gid.split '/'

  fetch_xml "/year_#{year}/month_#{month}/day_#{day}/" \
            "gid_#{gid.gsub(/[^a-z0-9]/, '_')}/pitchers/#{id}"
end

#rawboxscore_xml(gid) ⇒ Object



109
110
111
112
113
# File 'lib/mlb_gameday.rb', line 109

def rawboxscore_xml(gid)
  year, month, day, = gid.split '_'

  fetch_xml RAWBOXSCORE, year: year, month: month, day: day, gid: gid
end

#scoreboard_xml(date) ⇒ Object



99
100
101
# File 'lib/mlb_gameday.rb', line 99

def scoreboard_xml(date)
  fetch_xml date.strftime SCOREBOARD
end

#team(name) ⇒ Object



38
39
40
41
42
# 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

#teamsObject



44
45
46
47
# File 'lib/mlb_gameday.rb', line 44

def teams
  @teams ||= divisions.map(&:teams).map(&:values).flatten +
             @leagues[:misc][:teams].values
end