Class: BattingLeaders::Player
- Inherits:
-
Object
- Object
- BattingLeaders::Player
- 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
-
#batting_ave ⇒ Object
Returns the value of attribute batting_ave.
-
#name ⇒ Object
Returns the value of attribute name.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .all ⇒ Object
- .batting_leaders ⇒ Object
- .find_player_by_url(player_url) ⇒ Object
- .sorted_batters ⇒ Object
Instance Method Summary collapse
- #doc ⇒ Object
-
#initialize(player_hash) ⇒ Player
constructor
A new instance of Player.
- #other_details ⇒ Object
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_ave ⇒ Object
Returns the value of attribute batting_ave.
7 8 9 |
# File 'lib/batting_leaders/player.rb', line 7 def batting_ave @batting_ave end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/batting_leaders/player.rb', line 7 def name @name end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/batting_leaders/player.rb', line 7 def url @url end |
Class Method Details
.all ⇒ Object
30 31 32 |
# File 'lib/batting_leaders/player.rb', line 30 def self.all @@all end |
.batting_leaders ⇒ Object
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_batters ⇒ Object
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
#doc ⇒ Object
54 55 56 |
# File 'lib/batting_leaders/player.rb', line 54 def doc @doc ||= Nokogiri::HTML(open"#{@url}").css(".mod-content") end |
#other_details ⇒ Object
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 |