Class: MLBTerminal::Pitcher

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gameday, pitcher_id) ⇒ Pitcher

Returns a new instance of Pitcher.



13
14
15
16
17
18
# File 'lib/mlb_terminal/pitcher.rb', line 13

def initialize(gameday, pitcher_id)
  @base_url = "#{MLBTerminal::Game.parse_gameday_id_to_url gameday}/premium/pitchers/#{pitcher_id}"
  pitcher = Pitcher.list(gameday)[pitcher_id]
  @team = pitcher[:team]
  @name = pitcher[:name]
end

Class Method Details

.list(gameday) ⇒ Object



7
8
9
10
11
# File 'lib/mlb_terminal/pitcher.rb', line 7

def self.list(gameday)
  base_url = MLBTerminal::Game.parse_gameday_id_to_url gameday
  doc = Nokogiri::HTML(open( "#{base_url}/players.xml"))
  Hash[doc.xpath("//game/team/player[@position='P']").map{|x| [x["id"], {:team => x.xpath("..").first["name"], :name => "#{x["first"]} #{x["last"]}"}]}]
end

Instance Method Details

#pitch_tendency_history(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mlb_terminal/pitcher.rb', line 20

def pitch_tendency_history(&block)
  doc = Nokogiri::HTML(open( "#{@base_url}/pitchtendencies_history.xml"))

  Enumerator.new do |y|
    doc.xpath("//pitchtendencies/games/game").each do |game|
      game.xpath("types/type").each do |type|
        y.yield({
          :pitcher_name => @name,
          :pitcher_team => @team,
          :gameday_id => game["id"],
          :pitch_count => game["num"],
          :avg_speed => game["vel"],
          :pitch_type => PITCH_TYPES[type["id"]],
          :pitch_number => type["num"],
          :movement => type["movement"],
          :pfx => type["pfx"],
          :vel => type["vel"],
          :avg_x0 => type["avg_x0"],
          :avg_z0 => type["avg_z0"] })
      end
    end
  end.each(&block)
end