Class: NbaTeamList

Inherits:
Object
  • Object
show all
Includes:
NbaUrls
Defined in:
lib/espnscrape/NbaTeamList.rb

Overview

Access NBA team list

Instance Method Summary collapse

Methods included from NbaUrls

#boxScoreUrl, #formatTeamUrl, #getTid, #teamListUrl, #teamRosterUrl, #teamScheduleUrl

Constructor Details

#initialize[String]

Populate class attributes with Team Data



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/espnscrape/NbaTeamList.rb', line 8

def initialize()
	url = teamListUrl 							# Open static URL
	doc = Nokogiri::HTML(open(url))
	if doc.nil?
		puts "NbaTeamList URL Unreachable: " + url
		return nil
	end

	# Define XPATH variables
	xpath_tname = "//h5/a/text()"
	xpath_division = "//div/div/div/div/div[2]"

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

	# Check if Division layout has changed
	divCheck = 10
	divAct = doc.xpath(xpath_division).size
	if divAct != divCheck
		# puts "Warning: Found %i out of %i divisions via xpath" % [divAct, divCheck]
	end

	@team_list = []
	h = 0 # Head of teamNames range
	west_conf = ['Northwest','Pacific','Southwest'] # Western Conference Divs
	# Process Teams by Division
	divs = ['Atlantic', 'Pacific','Central','Southwest','Southeast','Northwest']
	divs.each do |div|
		processTeams(div, team_names[h,5], west_conf, @team_list) # Store Team Data
		h += 5
	end
	# Validate teamList
	if !@team_list.nil? && @team_list.size != 30
		puts "NbaTeamList: %i teams collected!" % [@team_list.size]
	end
	return @team_list
end

Instance Method Details

#getHeader[String]

Return header Example:

getHeader => "NBA Teams"

Returns:

  • ([String])

    header



130
131
132
# File 'lib/espnscrape/NbaTeamList.rb', line 130

def getHeader()
	return @header
end

#getTeamList[String]

Return team_list

Examples:

tl.getTeamList =>  [Team 0][TeamID, TeamName, TeamDiv, TeamConf]

Returns:

  • ([String])

    team_list



121
122
123
# File 'lib/espnscrape/NbaTeamList.rb', line 121

def getTeamList()
	return @team_list
end

#processTeams(division, team_names, west_conf, tl) ⇒ Object

Derive TeamID, Division, Conference

Examples:

processTeams("Atlantic", ["Boston Celtics"], [...], result)
#result[n] = [TeamID, TeamName, TeamDiv, TeamConf]
result[0] = ["BOS", "Boston Celtics", "Atlatic", "Eastern"]

Parameters:

  • division (String)

    Division Name

  • team_names ([String])

    List of Team Names

  • west_conf ([String])

    List of Divisions in the Western Conference

  • tl ([String])

    List to which rows of TeamList are appended



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/espnscrape/NbaTeamList.rb', line 67

def processTeams(division, team_names, west_conf, tl)
	# Derive Team Abbrevation
	team_names.each do |tname|
		t = tname.text # Extract text
		# Identify Team Abbreviation
		name_words = t.split
		abbr = ""
		if name_words.size > 2
			name_words.each do |x|
				abbr << x[0]
			end
			abbr.upcase
		else
			abbr = name_words[0][0..2].upcase
		end

		# Adjust Outlier Abbreviations
		case abbr
		when 'OCT'
			abbr = 'OKC'
		when 'PTB'
			abbr = 'POR'
		when 'BRO'
			abbr = 'BKN'
		end

		# Stage Team Data
		tmp = []
		tmp << abbr
		tmp << t.strip
		tmp << division

		# Derive Conference from Division
		tmp << (west_conf.include?(division) ? 'Western' : 'Eastern')

		if tmp.nil? || tmp.size != 4
			puts "Error: Unable to process full data for #{tname}"
		end

		# Save Team Data to global @team_list[]
		tl << tmp
		# There may be additional listings which are not for NBA teams.
		# Stop processing data when TeamID == Utah Jazz (UTA).
		if abbr == 'UTA'
			break
		end
	end
end

#toStringString

Generate String representation of Team List

Returns:

  • (String)

    Resulting team list string



50
51
52
53
54
55
56
# File 'lib/espnscrape/NbaTeamList.rb', line 50

def toString
	res = ""
	@team_list.each do |t|
		res += t.join(',') + "\n"
	end
	return res
end