-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg_batting.rb
49 lines (43 loc) · 1.36 KB
/
avg_batting.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
require 'csv'
require 'docopt'
require 'json'
require_relative 'batting_helper'
require_relative 'generator/avg_batting_json_generator'
require_relative 'generator/team_mapping'
doc = <<~DOCOPT
Average Batting.
Usage:
#{__FILE__} [--year=<year>] [--team=<team>]
#{__FILE__} -h | --help
#{__FILE__} --version
Options:
-h --help Show this screen.
--version Show version.
--year=<year> Year the game was in.
--team=<team> Team name the player is in.
DOCOPT
argv = Docopt.docopt(doc)
year_id = argv['--year']
team_name = argv['--team']
year_ids = []
team_name_to_id = get_team_name_to_id
# set team id and year in which those team exist (there are many teams with the same id but different names through the years)
team_id = if team_name.nil?
year_ids = [year_id.to_i] unless year_id.nil?
nil
else
id = team_name_to_id[team_name]
if id.nil?
puts "Team name does not exist. Please enter a correct name."
return
else
year_ids = year_id.nil? ? id[1] : [year_id.to_i]
id[0]
end
end
# read average batting json from the file
batting_file = read_avg_batting(team_id)
batting_json = JSON.parse(batting_file)
res = batting_avg_from_json(batting_json, team_id, year_ids)
print_avg(res)