class NbaTeamList

Access list of NBA teams

Attributes

header[RW]

@return [String] Table Title

teamList[RW]

@return [[[String]]] Table of NBA Teams @note (see TEAM_L)

Public Class Methods

new(args = {}) click to toggle source

Scrape Team Data @return [[String]] Resulting Team List

# File lib/hoopscrape/NbaTeamList.rb, line 15
def initialize(args = {})
  doc = args[:file] ? Nokogiri::HTML(open(args[:file])) : Nokogiri::HTML(open(teamListUrl))
  return if doc.nil?

  # Collect
  @header = doc.xpath('//h2')[0].text.strip # Table Header
  team_names = doc.xpath('//h5/a/text()')   # Team Names

  @teamList = []
  h = 0 # Head of teamNames range
  west_conf = %w[Northwest Pacific Southwest] # Western Conference Divs
  # Process Teams by Division
  divs = %w[Atlantic Pacific Central Southwest Southeast Northwest]
  divs.each do |div|
    @teamList += processTeams(div, team_names[h, 5], west_conf) # Store Team Data
    h += 5
  end
  # puts "Converting to #{args[:format]}"
  @teamList = @teamList.send(args[:format], S_TEAM) if args[:format]
  @teamList = Navigator.new(@teamList)
end

Private Instance Methods

processTeams(division, team_names, west_conf) click to toggle source

Derive TeamID, Division, Conference @param division [String] Division Name @param team_names [[String]] List of Team Names @param west_conf [[String]] List of Divisions in the Western Conference @param tl [[String]] List to which rows of TeamList are appended @example

processTeams("Atlantic", ["Boston Celtics"], [...], result)
#result[n] = [TeamID, TeamName, TeamDiv, TeamConf]
result[0] = ["BOS", "Boston Celtics", "Atlatic", "Eastern"]
# File lib/hoopscrape/NbaTeamList.rb, line 48
def processTeams(division, team_names, west_conf)
  result = []
  team_names.each do |tname|
    tmp  = [] # Stage Team Data
    full = adjustTeamName(tname.text.strip)   # Full Team Name
    tmp << getTid(full)       # Derive Team Abbreviation
    tmp << full.strip
    tmp << division

    # Derive Conference from Division
    tmp << (west_conf.include?(division) ? 'Western' : 'Eastern')
    result << tmp # Save Team Data to global @teamList[]
  end
  result
end