Class: FootballStatsFilterTool::Player

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

Constant Summary collapse

@@allowable_attributes =
[
  "name", 
  "age", 
  "position", 
  "team", 
  "pass_completions", 
  "pass_attempts", 
  "pass_completion_percentage",
  "pass_touchdowns",
  "interceptions_thrown",
  "pass_yards",
  "yards_per_pass_attempt",
  "carries",
  "rush_yards",
  "rush_yards_per_attempt",
  "rush_touchdowns",
  "receptions",
  "receiving_yards",
  "receiving_yards_per_catch",
  "receiving_touchdowns",
  "sacks",
  "interceptions_caught",
  "passes_defended",
  "tackles",
  "tackles_for_loss",
  "quarterback_hits",
]
@@all =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_hash) ⇒ Player

Returns a new instance of Player.



46
47
48
49
# File 'lib/football_stats_filter_tool/player.rb', line 46

def initialize(data_hash)
  data_hash.each {|key, value| self.send(("#{key}="), value)}
  @@all.push(self)
end

Class Method Details

.allObject



51
52
53
# File 'lib/football_stats_filter_tool/player.rb', line 51

def self.all
  return @@all
end

.allowable_attributesObject



36
37
38
# File 'lib/football_stats_filter_tool/player.rb', line 36

def self.allowable_attributes
  @@allowable_attributes
end

.create_or_update(data_hash) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/football_stats_filter_tool/player.rb', line 96

def self.create_or_update(data_hash)
  name = data_hash[:name]
  team = data_hash[:team]
  find_player = FootballStatsFilterTool::Player.find(name, team)
  if(find_player == nil)
    find_player = FootballStatsFilterTool::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_allObject



55
56
57
# File 'lib/football_stats_filter_tool/player.rb', line 55

def self.delete_all 
  all.clear 
end

.display_allObject



113
114
115
116
117
118
119
# File 'lib/football_stats_filter_tool/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) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/football_stats_filter_tool/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) ⇒ Object

returns array of results if multipe are found



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/football_stats_filter_tool/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) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/football_stats_filter_tool/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 = FootballStatsFilterTool::DataScraper.scrape_data(url_passing, "passing")
  data_rushing = FootballStatsFilterTool::DataScraper.scrape_data(url_rushing, "rushing")
  data_receiving = FootballStatsFilterTool::DataScraper.scrape_data(url_receiving, "receiving")
  data_defense = FootballStatsFilterTool::DataScraper.scrape_data(url_defense, "defense")

  data_passing.each{|entry| FootballStatsFilterTool::Player.create_or_update(entry)}
  data_rushing.each{|entry| FootballStatsFilterTool::Player.create_or_update(entry)}
  data_receiving.each{|entry| FootballStatsFilterTool::Player.create_or_update(entry)}
  data_defense.each{|entry| FootballStatsFilterTool::Player.create_or_update(entry)}
end

Instance Method Details

#has_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/football_stats_filter_tool/player.rb', line 40

def has_attribute?(attribute)
  self.class.allowable_attributes.include?(attribute)
end