bb_analytics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "BaseballPlayer" do
4
+ describe 'save' do
5
+ it 'should save a new record' do
6
+ @db.execute("select * from baseball_players where external_id = 'foo'").length.should == 0
7
+
8
+ player = BaseballPlayer.new()
9
+ player.external_id = "foo"
10
+ player.save
11
+
12
+ @db.execute("select * from baseball_players where external_id = 'foo'").length.should == 1
13
+ end
14
+
15
+ it 'should update an existing record' do
16
+ @db.execute("select * from baseball_players where external_id = 'foo'").length.should == 0
17
+
18
+ player = BaseballPlayer.new()
19
+ player.external_id = "foo"
20
+ player.first_name = "barney"
21
+ player.save
22
+
23
+ player2 = BaseballPlayer.new()
24
+ player2.external_id = "foo"
25
+ player2.last_name = "rubble"
26
+ player2.save
27
+
28
+ player2.reload.first_name.should == "barney"
29
+ player.reload.last_name.should == "rubble"
30
+
31
+ @db.execute("select * from baseball_players where external_id = 'foo'").length.should == 1
32
+ end
33
+ end
34
+
35
+ describe 'find_by_external_id' do
36
+ it 'locates and loads the correct baseball player' do
37
+ player = BaseballPlayer.new()
38
+ player.external_id = "foo"
39
+ player.save
40
+
41
+ BaseballPlayer.find_by_external_id("foo").external_id.should == "foo"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,117 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Importer" do
4
+
5
+ describe 'new' do
6
+ before(:each) do
7
+ @baseball_master_list = {}
8
+ CSV.foreach(File.dirname(__FILE__) + '/../support/Master-small.csv', :headers => true, :header_converters => :symbol, :converters => :all) do |row|
9
+ @baseball_master_list[row.fields[0]] ||= []
10
+ @baseball_master_list[row.fields[0]] << Hash[row.headers[1..-1].zip(row.fields[1..-1])]
11
+ end
12
+ end
13
+
14
+ it 'loads a csv file' do
15
+ importer = Importer.new(File.dirname(__FILE__) + '/../support/Master-small.csv')
16
+ importer.csv.should == @baseball_master_list
17
+ end
18
+ end
19
+
20
+ describe "parsing baseball players from master list" do
21
+ before(:each) do
22
+ @baseball_master_list = {}
23
+ CSV.foreach(File.dirname(__FILE__) + '/../support/Master-small.csv', :headers => true, :header_converters => :symbol, :converters => :all) do |row|
24
+ @baseball_master_list[row.fields[0]] ||= []
25
+ @baseball_master_list[row.fields[0]] << Hash[row.headers[1..-1].zip(row.fields[1..-1])]
26
+ end
27
+
28
+ @importer = Importer.new
29
+ @importer.csv = @baseball_master_list
30
+ end
31
+
32
+ it 'parses master csv data into baseball players' do
33
+ baseball_players = @importer.save_baseball_players
34
+ @db.execute("select * from baseball_players").length.should == 18124
35
+ end
36
+
37
+ end
38
+
39
+ describe "parsing baseball players from statistics list" do
40
+ before(:each) do
41
+ @statistics_list = {}
42
+ CSV.foreach(File.dirname(__FILE__) + '/../support/Batting-07-12.csv', :headers => true, :header_converters => :symbol, :converters => :all) do |row|
43
+ @statistics_list[row.fields[0]] ||= []
44
+ @statistics_list[row.fields[0]] << Hash[row.headers[1..-1].zip(row.fields[1..-1])]
45
+ end
46
+
47
+ @importer = Importer.new
48
+ @importer.csv = @statistics_list
49
+ end
50
+
51
+ it 'parses statistics csv data into baseball players' do
52
+ baseball_players = @importer.save_baseball_players
53
+
54
+ @db.execute("select * from baseball_players").length.should == 2447
55
+ end
56
+ end
57
+
58
+ describe "parsing statistics from players list" do
59
+ before(:each) do
60
+ @baseball_master_list = {}
61
+ CSV.foreach(File.dirname(__FILE__) + '/../support/Master-small.csv', :headers => true, :header_converters => :symbol, :converters => :all) do |row|
62
+ @baseball_master_list[row.fields[0]] ||= []
63
+ @baseball_master_list[row.fields[0]] << Hash[row.headers[1..-1].zip(row.fields[1..-1])]
64
+ end
65
+
66
+ @importer = Importer.new
67
+ @importer.csv = @baseball_master_list
68
+ end
69
+
70
+ it 'has no side effects' do
71
+ @importer.save_stats_for_year
72
+ @db.execute("select * from stats_for_years").length.should == 0
73
+ @db.execute("select * from baseball_players").length.should == 0
74
+ end
75
+ end
76
+
77
+ describe "parsing statistics from statistics list" do
78
+ before(:each) do
79
+ @statistics_list = {}
80
+ CSV.foreach(File.dirname(__FILE__) + '/../support/Batting-07-12.csv', :headers => true, :header_converters => :symbol, :converters => :all) do |row|
81
+ @statistics_list[row.fields[0]] ||= []
82
+ @statistics_list[row.fields[0]] << Hash[row.headers[1..-1].zip(row.fields[1..-1])]
83
+ end
84
+ @importer = Importer.new
85
+ @importer.csv = @statistics_list
86
+ end
87
+
88
+ it 'parses statistics csv data into stats for years' do
89
+ @importer.save_stats_for_year
90
+ @db.execute("select * from stats_for_years").length.should == 7908
91
+ end
92
+
93
+ it 'calculates batting average' do
94
+ @importer.save_stats_for_year
95
+ @db.execute("select * from stats_for_years where player_external_id = ? and year = ? and team = ?", ["abercre01", 2008, "HOU"]).length.should == 1
96
+ @db.execute("select * from stats_for_years where player_external_id = ? and year = ? and team = ?", ["abercre01", 2008, "HOU"]) do |row|
97
+ row[13].should == 309
98
+ end
99
+ end
100
+
101
+ it 'calculates slugging percentage' do
102
+ @importer.save_stats_for_year
103
+ @db.execute("select * from stats_for_years where player_external_id = ? and year = ? and team = ?", ["abercre01", 2008, "HOU"]).length.should == 1
104
+ @db.execute("select * from stats_for_years where player_external_id = ? and year = ? and team = ?", ["abercre01", 2008, "HOU"]) do |row|
105
+ row[14].should == 509
106
+ end
107
+ end
108
+
109
+ it 'calculates fantasy points' do
110
+ @importer.save_stats_for_year
111
+ @db.execute("select * from stats_for_years where player_external_id = ? and year = ? and team = ?", ["abercre01", 2008, "HOU"]).length.should == 1
112
+ @db.execute("select * from stats_for_years where player_external_id = ? and year = ? and team = ?", ["abercre01", 2008, "HOU"]) do |row|
113
+ row[15].should == 16
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,258 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "StatsForYear" do
4
+ describe 'save' do
5
+ it 'should save a new record' do
6
+ @db.execute("select * from stats_for_years where player_external_id = 'foo'").length.should == 0
7
+
8
+ stats = StatsForYear.new()
9
+ stats.player_external_id = "foo"
10
+ stats.save
11
+
12
+ @db.execute("select * from stats_for_years where player_external_id = 'foo'").length.should == 1
13
+ end
14
+
15
+ it 'should update an existing record' do
16
+ @db.execute("select * from stats_for_years where player_external_id = 'foo'").length.should == 0
17
+
18
+ stats = StatsForYear.new()
19
+ stats.player_external_id = "foo"
20
+ stats.year = 12
21
+ stats.team = "OAK"
22
+ stats.at_bats = 20
23
+ stats.batting_average = 400
24
+ stats.save
25
+
26
+ stats2 = StatsForYear.new()
27
+ stats2.player_external_id = "foo"
28
+ stats2.year = 12
29
+ stats2.team = "OAK"
30
+ stats2.games = 30
31
+ stats2.slugging_percentage = 500
32
+ stats2.save
33
+
34
+ stats2.reload.at_bats.should == 20
35
+ stats2.reload.batting_average.should == 400
36
+ stats.reload.slugging_percentage.should == 500
37
+ stats.reload.games.should == 30
38
+
39
+ @db.execute("select * from stats_for_years where player_external_id = 'foo'").length.should == 1
40
+ end
41
+ end
42
+
43
+ describe 'find_highest_rbi' do
44
+ it 'should find the record with the highet rbi for a given year' do
45
+ stats = StatsForYear.new()
46
+ stats.player_external_id = "foo"
47
+ stats.year = 12
48
+ stats.team = "OAK"
49
+ stats.at_bats = 20
50
+ stats.runs_batted_in = 400
51
+ stats.save
52
+
53
+ stats2 = StatsForYear.new()
54
+ stats2.player_external_id = "bar"
55
+ stats2.year = 12
56
+ stats2.team = "OAK"
57
+ stats2.games = 30
58
+ stats2.at_bats = 200
59
+ stats2.runs_batted_in = 500
60
+ stats2.save
61
+
62
+ stats3 = StatsForYear.new()
63
+ stats3.player_external_id = "zoo"
64
+ stats3.year = 12
65
+ stats3.team = "OAK"
66
+ stats3.games = 30
67
+ stats3.at_bats = 1
68
+ stats3.runs_batted_in = 5000
69
+ stats3.save
70
+
71
+ StatsForYear.find_highest_rbi(12, 2).first.player_external_id.should == stats2.reload.player_external_id
72
+ end
73
+ end
74
+
75
+ describe 'find_highest_home_runs' do
76
+ it 'should find the record with the highet home runs for a given year' do
77
+ stats = StatsForYear.new()
78
+ stats.player_external_id = "foo"
79
+ stats.year = 12
80
+ stats.team = "OAK"
81
+ stats.at_bats = 20
82
+ stats.home_runs = 400
83
+ stats.save
84
+
85
+ stats2 = StatsForYear.new()
86
+ stats2.player_external_id = "bar"
87
+ stats2.year = 12
88
+ stats2.team = "OAK"
89
+ stats2.games = 30
90
+ stats2.at_bats = 200
91
+ stats2.home_runs = 500
92
+ stats2.save
93
+
94
+ stats3 = StatsForYear.new()
95
+ stats3.player_external_id = "zoo"
96
+ stats3.year = 12
97
+ stats3.team = "OAK"
98
+ stats3.games = 30
99
+ stats3.at_bats = 1
100
+ stats3.home_runs = 5000
101
+ stats3.save
102
+
103
+ StatsForYear.find_highest_home_runs(12, 2).first.player_external_id.should == stats2.reload.player_external_id
104
+ StatsForYear.find_highest_home_runs(12, 2).length.should == 1
105
+ end
106
+ end
107
+
108
+ describe 'find_highest_batting_average' do
109
+ it 'should find the record with the highet home runs for a given year' do
110
+ stats = StatsForYear.new()
111
+ stats.player_external_id = "foo"
112
+ stats.year = 12
113
+ stats.team = "OAK"
114
+ stats.at_bats = 20
115
+ stats.batting_average = 400
116
+ stats.save
117
+
118
+ stats2 = StatsForYear.new()
119
+ stats2.player_external_id = "bar"
120
+ stats2.year = 12
121
+ stats2.team = "OAK"
122
+ stats2.games = 30
123
+ stats2.at_bats = 200
124
+ stats2.batting_average = 500
125
+ stats2.save
126
+
127
+ stats3 = StatsForYear.new()
128
+ stats3.player_external_id = "zoo"
129
+ stats3.year = 12
130
+ stats3.team = "OAK"
131
+ stats3.games = 30
132
+ stats3.at_bats = 1
133
+ stats3.batting_average = 5000
134
+ stats3.save
135
+
136
+ StatsForYear.find_highest_batting_average(12, 2).first.player_external_id.should == stats2.reload.player_external_id
137
+ StatsForYear.find_highest_batting_average(12, 2).length.should == 1
138
+ end
139
+ end
140
+
141
+ describe 'find_triple_crown_winner' do
142
+ it 'find the winner' do
143
+ stats = StatsForYear.new()
144
+ stats.player_external_id = "foo"
145
+ stats.year = 12
146
+ stats.team = "OAK"
147
+ stats.at_bats = 20
148
+ stats.batting_average = 500
149
+ stats.home_runs = 1
150
+ stats.runs_batted_in = 5
151
+ stats.save
152
+
153
+ stats2 = StatsForYear.new()
154
+ stats2.player_external_id = "bar"
155
+ stats2.year = 12
156
+ stats2.team = "OAK"
157
+ stats2.games = 30
158
+ stats2.at_bats = 200
159
+ stats2.batting_average = 500
160
+ stats2.home_runs = 100
161
+ stats2.runs_batted_in = 500
162
+ stats2.save
163
+
164
+ stats3 = StatsForYear.new()
165
+ stats3.player_external_id = "zoo"
166
+ stats3.year = 12
167
+ stats3.team = "OAK"
168
+ stats3.games = 30
169
+ stats3.at_bats = 1
170
+ stats3.batting_average = 5000
171
+ stats.home_runs = 1000
172
+ stats.runs_batted_in = 50
173
+ stats3.save
174
+
175
+ StatsForYear.find_triple_crown_winner(12, 2).player_external_id.should == stats2.reload.player_external_id
176
+ end
177
+ end
178
+
179
+ describe 'baseball_player' do
180
+ it 'loads the associated baseball player' do
181
+ stats = StatsForYear.new()
182
+ stats.player_external_id = "foo"
183
+ stats.save
184
+
185
+ player = BaseballPlayer.new()
186
+ player.external_id = "foo"
187
+ player.save
188
+
189
+ stats.reload.baseball_player.external_id.should == "foo"
190
+ end
191
+ end
192
+
193
+ describe 'most_improved_batting_average' do
194
+ before(:each) do
195
+ stats = StatsForYear.new()
196
+ stats.player_external_id = "foo"
197
+ stats.batting_average = 500
198
+ stats.year = 2010
199
+ stats.save
200
+
201
+ stats2 = StatsForYear.new()
202
+ stats2.player_external_id = "foo"
203
+ stats2.batting_average = 600
204
+ stats2.year = 2011
205
+ stats2.save
206
+
207
+ stats3 = StatsForYear.new()
208
+ stats3.player_external_id = "bar"
209
+ stats3.batting_average = 500
210
+ stats3.year = 2010
211
+ stats3.save
212
+
213
+ stats4 = StatsForYear.new()
214
+ stats4.player_external_id = "bar"
215
+ stats4.batting_average = 599
216
+ stats4.year = 2011
217
+ stats4.save
218
+ end
219
+
220
+ it 'return the external id of the most improved player' do
221
+ StatsForYear.most_improved_batting_average(2010, 2011).should == ["foo"]
222
+ end
223
+
224
+ end
225
+
226
+ describe 'most_improved_fantasy points' do
227
+ before(:each) do
228
+ stats = StatsForYear.new()
229
+ stats.player_external_id = "foo"
230
+ stats.fantasy_points = 500
231
+ stats.year = 2010
232
+ stats.save
233
+
234
+ stats2 = StatsForYear.new()
235
+ stats2.player_external_id = "foo"
236
+ stats2.fantasy_points = 600
237
+ stats2.year = 2011
238
+ stats2.save
239
+
240
+ stats3 = StatsForYear.new()
241
+ stats3.player_external_id = "bar"
242
+ stats3.fantasy_points = 500
243
+ stats3.year = 2010
244
+ stats3.save
245
+
246
+ stats4 = StatsForYear.new()
247
+ stats4.player_external_id = "bar"
248
+ stats4.fantasy_points = 599
249
+ stats4.year = 2011
250
+ stats4.save
251
+ end
252
+
253
+ it 'return the external id of the most improved player' do
254
+ StatsForYear.most_improved_fantasy_points(2010, 2011).should == ["foo"]
255
+ end
256
+
257
+ end
258
+ end
@@ -0,0 +1,39 @@
1
+ require 'simplecov'
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_adapter 'test_frameworks'
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+
20
+ require 'rspec'
21
+ require 'bb_analytics'
22
+ require 'csv'
23
+
24
+ # Requires supporting files with custom matchers and macros, etc,
25
+ # in ./support/ and its subdirectories.
26
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
27
+
28
+ RSpec.configure do |config|
29
+
30
+ config.before(:each) do
31
+ @db = SQLite3::Database.new File.dirname(__FILE__) + '/support/bba.db'
32
+ DataStore.instance.db = @db
33
+ end
34
+
35
+ config.after(:each) do
36
+ File.delete("#{File.dirname(__FILE__)}/support/bba.db") rescue nil
37
+ end
38
+
39
+ end
@@ -0,0 +1 @@
1
+ playerID,yearID,teamID,G,AB,R,H,2B,3B,HR,RBI,SB,CS
@@ -0,0 +1 @@
1
+ playerID,birthYear,nameFirst,nameLast
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bb_analytics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Beddingfield
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aruba
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: gli
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 2.9.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 2.9.0
111
+ description: a simple baseball analytics tool
112
+ email: kevin.beddingfield@gmail.com
113
+ executables:
114
+ - bb_analytics
115
+ extensions: []
116
+ extra_rdoc_files:
117
+ - README.rdoc
118
+ - bb_analytics.rdoc
119
+ files:
120
+ - .gitignore
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - README.md
124
+ - README.rdoc
125
+ - Rakefile
126
+ - bb_analytics.gemspec
127
+ - bb_analytics.rdoc
128
+ - bin/bb_analytics
129
+ - lib/bb_analytics.rb
130
+ - lib/bb_analytics/models/baseball_player.rb
131
+ - lib/bb_analytics/models/data_store.rb
132
+ - lib/bb_analytics/models/importer.rb
133
+ - lib/bb_analytics/models/stats_for_year.rb
134
+ - lib/bb_analytics/version.rb
135
+ - results.html
136
+ - spec/models/baseball_player_spec.rb
137
+ - spec/models/importer_spec.rb
138
+ - spec/models/stats_for_year_spec.rb
139
+ - spec/spec_helper.rb
140
+ - spec/support/Batting-07-12.csv
141
+ - spec/support/Master-small.csv
142
+ homepage: http://mambasystems.net
143
+ licenses: []
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options:
147
+ - --title
148
+ - bb_analytics
149
+ - --main
150
+ - README.rdoc
151
+ - -ri
152
+ require_paths:
153
+ - lib
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.2.2
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: A simple baseball analytics tool
171
+ test_files: []