Class: NbaRoster

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

Overview

Access NBA roster data

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NbaUrls

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

Constructor Details

#initialize(team_id) ⇒ NbaRoster

Scrape Roster Data

Examples:

r = NbaRoster.new("UTA")

Parameters:

  • team_id (String)

    Three letter TeamID



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
46
47
48
49
50
51
52
53
54
# File 'lib/espnscrape/NbaRoster.rb', line 12

def initialize(team_id)
	unless (team_id == '')
		url = formatTeamUrl(team_id, teamRosterUrl)  # Generate URL
		doc = Nokogiri::HTML(open(url))				   		 # Get DOM
		if doc.nil?																	 # Error gracefully
			return []
		end
		list = doc.xpath('//div/div/table/tr')
		pList = list[2,list.length-3]				  	     # Get Player Nodes

		@coach = list[list.length - 1].children[0].text.split(':')[1].strip  # Read Coach Name
		@players = []
		pList.each do |row|
			cnt = 0
			tmp = []
			tmp << team_id  									  #Team ID
			row.children.each do |cell|
				txt = cell.text.chomp.strip
				case cnt
				when 0, 2, 3, 5	 									# 0 Player No, 2 Position, 3 Age, 5 Weight
					tmp << txt
				when 1 														# Player Name
					tmp << txt.gsub("'","\'")
					tmp << cell.children[0].attribute("href").text[/id\/(\d+)/, 1] # Player ID
				when 4													  # Player Height
					tmp.concat(txt.split('-'))
				when 6 														# College
					tmp << txt.gsub("'","\'").strip
				when 7 														# Salary
					# Remove extraneous symbols
					txt = txt.gsub('$','')
					txt = txt.gsub(',','').strip()
					tmp << (txt.length > 1 ? txt : 0)  # Store Positive Salary (Default: 0)
				end
				cnt += 1
			end
			if !tmp.nil?
				@players << tmp
			end
		end
		return @players
	end
end

Instance Attribute Details

#coachObject (readonly)

Returns the value of attribute coach.



5
6
7
# File 'lib/espnscrape/NbaRoster.rb', line 5

def coach
  @coach
end

#playersObject (readonly)

Returns the value of attribute players.



5
6
7
# File 'lib/espnscrape/NbaRoster.rb', line 5

def players
  @players
end

Instance Method Details

#getCoachString

Return coach Name

Examples:

r.getCoach("UTA") #=> "Jerry Sloan"

Returns:

  • (String)

    Coach Name



69
70
71
# File 'lib/espnscrape/NbaRoster.rb', line 69

def getCoach()
	return @coach
end

#getRoster[object]

Note:

Roster row [TeamId, Jersey #, Player Name, ESPN Player ID, Position, Age, Height ft, Height in, Weight, College, Salary]

Returns Team Roster

Returns:

  • ([object])


60
61
62
# File 'lib/espnscrape/NbaRoster.rb', line 60

def getRoster()
	return @players
end

#toStringString

String representation

Returns:

  • (String)


76
77
78
79
80
81
# File 'lib/espnscrape/NbaRoster.rb', line 76

def toString()
	res = ''
	@players.each do |p|
		res = res + p.join(',')
	end
end