Class: NbaDrilldown::Team

Inherits:
Object
  • Object
show all
Defined in:
lib/nba_drilldown/team.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTeam

Returns a new instance of Team.



6
7
8
# File 'lib/nba_drilldown/team.rb', line 6

def initialize
  @players = []
end

Instance Attribute Details

#conferenceObject

Returns the value of attribute conference.



2
3
4
# File 'lib/nba_drilldown/team.rb', line 2

def conference
  @conference
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/nba_drilldown/team.rb', line 2

def name
  @name
end

#playersObject

Returns the value of attribute players.



2
3
4
# File 'lib/nba_drilldown/team.rb', line 2

def players
  @players
end

#recordObject

Returns the value of attribute record.



2
3
4
# File 'lib/nba_drilldown/team.rb', line 2

def record
  @record
end

#roster_urlObject

Returns the value of attribute roster_url.



2
3
4
# File 'lib/nba_drilldown/team.rb', line 2

def roster_url
  @roster_url
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/nba_drilldown/team.rb', line 2

def url
  @url
end

Class Method Details

.allObject



48
49
50
# File 'lib/nba_drilldown/team.rb', line 48

def self.all
  @@all
end

.create_teamsObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nba_drilldown/team.rb', line 11

def self.create_teams
  doc = Nokogiri::HTML(open("http://espn.go.com/nba/teams"))
  doc.search("a.bi").each do |team| 
    new_team = NbaDrilldown::Team.new
    new_team.name = team.text
    new_team.url = team["href"]
    new_team.add_record_to_team
    @@all << new_team
  end

end

.find_team(input) ⇒ Object



36
37
38
39
# File 'lib/nba_drilldown/team.rb', line 36

def self.find_team(input)
  team = @@all[input-1]
  team
end

.list_teamsObject



41
42
43
44
45
46
# File 'lib/nba_drilldown/team.rb', line 41

def self.list_teams
    puts "Team Name --- Team Record"
  @@all.each_with_index do |team, index|
    puts "#{index + 1}. #{team.name} --- #{team.record}"
  end
end

Instance Method Details

#add_record_to_teamObject



23
24
25
26
27
# File 'lib/nba_drilldown/team.rb', line 23

def add_record_to_team
  doc = Nokogiri::HTML(open(self.url))
  self.record = doc.search("li.record").first.text

end

#create_players_from_teamObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nba_drilldown/team.rb', line 53

def create_players_from_team
  self.roster_url = self.format_team_url
    
    doc = Nokogiri::HTML(open(self.roster_url))
    doc.search("td.sortcell a").each do |player|
    new_player = NbaDrilldown::Player.create_from_data(player)
    
    new_player.add_player_info
    new_player.team = self
    
    self.players << new_player
    end

end

#format_team_urlObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nba_drilldown/team.rb', line 68

def format_team_url
  split_array = self.url.split("/")
  new_array = []
  split_array.each do |text|
    if text == "_"
      text = "roster"
      new_array << text
    else 
      new_array << text
    end
  end
  new_array.pop
  new_array.insert(6,"_")
  new_array.join("/")
end

#list_playersObject



29
30
31
32
33
34
# File 'lib/nba_drilldown/team.rb', line 29

def list_players
  puts "Name|Number|Position"
  self.players.each do |player|
    puts "#{player.name}|#{player.number}|#{player.position}"
  end
end