class Player

Public Class Methods

all() click to toggle source
# File lib/player.rb, line 51
def self.all
  return @@all
end
allowable_attributes() click to toggle source
# File lib/player.rb, line 36
def self.allowable_attributes
  @@allowable_attributes
end
create_or_update(data_hash) click to toggle source
# File lib/player.rb, line 96
def self.create_or_update(data_hash)
  name = data_hash[:name]
  team = data_hash[:team]
  find_player = Player.find(name, team)
  if(find_player == nil)
    find_player = Player.new(data_hash)
  else 
    data_hash.each do |key, value| 
      current_value = find_player.instance_variable_get("@#{key}")
      if(current_value == 0 || current_value = "" || current_value == nil || current_value == 0.0)#ensures not overiding good data
        find_player.send(("#{key}="), value)
      end
    end
  end
  return find_player
end
delete_all() click to toggle source
# File lib/player.rb, line 55
def self.delete_all 
  all.clear 
end
display_all() click to toggle source
# File lib/player.rb, line 113
def self.display_all
  all.each do|player| 
    if(player.team = "SEA")
      puts "#{player.name}, #{player.position}, #{player.team}" 
    end
  end
end
find(name, team) click to toggle source
# File lib/player.rb, line 83
def self.find(name, team)
  out = nil
  all.each do |player|
    if(player.name == name && player.team == team)
      out = player
    end 
  end
  return out

end
find_by_name(name) click to toggle source
# File lib/player.rb, line 59
def self.find_by_name (name) #returns array of results if multipe are found
  out = nil
  all.select do |player|
    player.name == name 
  end
  if(out == nil)
    out_data =  {
      :type => "No Results",
      :data => nil
    } 
  elsif(out.size == 1)
    out_data = {
      :type => "Unique Result",
      :data => out[0]
    }
  elsif(out.size > 1)
    out_data = {
      :type => "Multiple Results",
      :data => out
    }
  end
  out_data
end
import(year) click to toggle source
# File lib/player.rb, line 121
def self.import(year)
  url_passing = "https://www.pro-football-reference.com/years/#{year}/passing.htm"
  url_rushing = "https://www.pro-football-reference.com/years/#{year}/rushing.htm"
  url_receiving = "https://www.pro-football-reference.com/years/#{year}/receiving.htm"
  url_defense = "https://www.pro-football-reference.com/years/#{year}/defense.htm"

  data_passing = DataScraper.scrape_data(url_passing, "passing")
  data_rushing = DataScraper.scrape_data(url_rushing, "rushing")
  data_receiving = DataScraper.scrape_data(url_receiving, "receiving")
  data_defense = DataScraper.scrape_data(url_defense, "defense")

  data_passing.each{|entry| Player.create_or_update(entry)}
  data_rushing.each{|entry| Player.create_or_update(entry)}
  data_receiving.each{|entry| Player.create_or_update(entry)}
  data_defense.each{|entry| Player.create_or_update(entry)}
end
new(data_hash) click to toggle source
# File lib/player.rb, line 46
def initialize(data_hash)
  data_hash.each {|key, value| self.send(("#{key}="), value)}
  @@all.push(self)
end

Public Instance Methods

has_attribute?(attribute) click to toggle source
# File lib/player.rb, line 40
def has_attribute?(attribute)
  self.class.allowable_attributes.include?(attribute)
end