class NpbApi::PitchingStats

Public Class Methods

new(date: '', id: '') click to toggle source
# File lib/npb_api/pitching_stats.rb, line 2
def initialize(date: '', id: '')
  @date = date
  @game_id = (date.year.to_s + format('%02d', date.month) + format('%02d', date.day) + id).to_i
  @url = "http://baseball.yahoo.co.jp/npb/game/#{@game_id}/stats"
  @doc = TableExtraction.new(url: @url).doc
end

Public Instance Methods

away() click to toggle source
# File lib/npb_api/pitching_stats.rb, line 9
def away
  stats(:away)
end
home() click to toggle source
# File lib/npb_api/pitching_stats.rb, line 13
def home
  stats(:home)
end

Private Instance Methods

innings_pitched(row) click to toggle source
# File lib/npb_api/pitching_stats.rb, line 56
def innings_pitched(row)
  # \xc2\xa0 =>  
  if row.children[7].text =~ /(\d{1,2})\xc2\xa0(\d)\/3\z/
    "#{$1} #{$2}/3"
  else
    row.children[7].text
  end
end
player_id(row) click to toggle source
# File lib/npb_api/pitching_stats.rb, line 51
def player_id(row)
  row.children[3].css('a').attribute('href').value =~ /\A\/npb\/player\?id=(\d+)\z/
  $1.to_i
end
result(row) click to toggle source
# File lib/npb_api/pitching_stats.rb, line 65
def result(row)
  case row.children[1].text
  when '勝'
    'win'
  when '負'
    'lose'
  when 'H'
    'hold'
  when 'S'
    'save'
  else
    nil
  end
end
stats(home_or_away) click to toggle source
# File lib/npb_api/pitching_stats.rb, line 19
def stats(home_or_away)
  doc =
    case home_or_away
    when :home then @doc[-1]
    when :away then @doc[-2]
    end
  rows = doc.css('tr')
  rows.shift
  order = 0
  rows.each_with_object([]) do |row, arr|
    order += 1
    arr << {
      date: @date,
      game_id: @game_id,
      order: order,
      result: result(row),
      player_id: player_id(row),
      earned_run_average: row.children[5].text.to_f,
      innings_pitched: innings_pitched(row),
      batters_faced: row.children[9].text.to_i,
      number_of_pitches_thrown: row.children[11].text.to_i,
      hits: row.children[13].text.to_i,
      home_runs: row.children[15].text.to_i,
      strikeouts: row.children[17].text.to_i,
      hit_batsmen_and_bases_on_balls: row.children[19].text.to_i,
      runs: row.children[21].text.to_i,
      earned_runs: row.children[23].text.to_i,
      team_id: team_id(home_or_away),
    }
  end
end
team_id(home_or_away) click to toggle source
# File lib/npb_api/pitching_stats.rb, line 80
def team_id(home_or_away)
  scoreboard =
    case home_or_away
    when :home
      TableExtraction.new(url: @url, klass: 'scoreboard').doc[1]
    when :away
      TableExtraction.new(url: @url, klass: 'scoreboard').doc[0]
    end
  scoreboard.css('td.tn').children[0].attribute('href').value =~ /\A\/npb\/teams\/(\d+)\/\z/
  $1.to_i
end