Class: BattingLeaders::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/batting_leaders/player.rb

Constant Summary collapse

PlayerDetails =
{
#  @team => ".general-info .last"
  team: [".general-info .last", 0, ""],
  number_position: [".general-info .first", 0, ""],
  birth_date: [".player-metadata li", 0, "Birth Date"],
  homerun: [".header-stats td", 1, ""],
  experience: [".player-metadata li", 2, "Experience"],
  college: [".player-metadata li", 3, "College"],
  ht_wt: [".player-metadata li", 4, "Ht/Wt"],
  rbi: [".header-stats td", 2, ""],
  obp: [".header-stats td", 3, ""]
}
@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player_hash) ⇒ Player

Returns a new instance of Player.



23
24
25
26
27
28
# File 'lib/batting_leaders/player.rb', line 23

def initialize(player_hash)
  @name = player_hash[:name]
  @batting_ave = player_hash[:batting_ave]
  @url = player_hash[:url]
  @@all << self
end

Instance Attribute Details

#batting_aveObject

Returns the value of attribute batting_ave.



7
8
9
# File 'lib/batting_leaders/player.rb', line 7

def batting_ave
  @batting_ave
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/batting_leaders/player.rb', line 7

def name
  @name
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/batting_leaders/player.rb', line 7

def url
  @url
end

Class Method Details

.allObject



30
31
32
# File 'lib/batting_leaders/player.rb', line 30

def self.all
  @@all
end

.batting_leadersObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/batting_leaders/player.rb', line 38

def self.batting_leaders
#    oddEven =="odd" ? odd_or_even_row = "oddrow" : odd_or_even_row = "evenrow"
  current_year = Date.today.year
  get_page = Nokogiri::HTML(open"http://espn.go.com/mlb/stats/batting/_/#{current_year}/2016/seasontype/2")
  get_page.css("tr.oddrow,.evenrow").collect do |player|
    player_attributes = {:name => player.css("a").text,
                         :url => player.css("a").attribute("href").value,
                         :batting_ave => player.css(".sortcell").text}
    player = self.new(player_attributes)
  end
end

.find_player_by_url(player_url) ⇒ Object



50
51
52
# File 'lib/batting_leaders/player.rb', line 50

def self.find_player_by_url(player_url)
  @@all.detect{|player| player.url == player_url}
end

.sorted_battersObject



34
35
36
# File 'lib/batting_leaders/player.rb', line 34

def self.sorted_batters
  self.batting_leaders.sort {|player, player2| player2.batting_ave <=> player.batting_ave} #sort by attribute (batting_ave) of Player's instance
end

Instance Method Details

#docObject



54
55
56
# File 'lib/batting_leaders/player.rb', line 54

def doc
  @doc ||= Nokogiri::HTML(open"#{@url}").css(".mod-content")
end

#other_detailsObject



58
59
60
61
62
63
64
# File 'lib/batting_leaders/player.rb', line 58

def other_details
  player_detail_hash = {}
  PlayerDetails.each do |attribute, values|  #values = ["css selector", index, "erased text using gsub" ]
    player_detail_hash[attribute] ||= doc.css(values[0])[values[1]].text.gsub(values[2], "")
  end
  player_detail_hash
end