This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtap-styles.rb
158 lines (122 loc) · 3.53 KB
/
tap-styles.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'json'
require 'optparse'
ARGV << '-h' if ARGV.empty?
options = {}
OptionParser.new do |opts|
opts.banner = "Returns counts of each style in a TapCellar backup.\nUsage: tap-styles.rb [options] [file to parse]"
options[:csv] = false
options[:filename] = ""
# options[:filter] = false
opts.on("-c", "--csv", "Output CSV") do
options[:csv] = true
end
# opts.on("-f", "--filter STRING", String, "Filter results by [STRING]") do |filter_string|
# options[:filter] = true
# options[:filter_string] = filter_string
# end
opts.on('-h', '--help', 'Display help for options' ) do
puts opts
exit
end
end.parse!
options[:filename] = ARGV[ARGV.length - 1]
if File.exists?(options[:filename])
file_hash = JSON.parse(File.open(options[:filename], 'r').read, :max_nesting => 100)
else
puts "Error: File " + options[:filename] + " does not exist."
exit
end
# Keep a list of style encountered as we read through records
beerStyles = Array.new
# Read through each record in the JSON and do stuff
file_hash["tapcellarbeers"].each do |beer_record|
grade = beer_record["grade"].to_f.round(3)
# Only process records with valid grades
if grade > 0
# Stash that style away
if beer_record["bdb_style"].to_s != ""
beer_style = beer_record["bdb_style"]
elsif beer_record["style"].to_s != ""
beer_style = beer_record["style"]
else
beer_style = "None"
end
beerStyles << beer_style
end
end
# Hash to hold counts for styles
style_counts = Hash.new 0
# Counts for each unique style
beerStyles.each do |style|
style_counts[style] += 1
end
sorted_styles = style_counts.sort_by { |style, count| style }
longest_style = sorted_styles.max_by { |x| x[0].length }
width = longest_style[0].length
if options[:csv]
puts "Style, Rated Beers"
sorted_styles.each do |style|
puts style[0] + "," + style[1].to_s
end
belgians = 0
ipas = 0
lagers = 0
stouts = 0
wheats = 0
sorted_styles.each do |style|
if style[0].include?("Belgian")
belgians += style[1]
end
if style[0].include?("India Pale Ale")
ipas += style[1]
end
if style[0].include?("Lager") || style[0].include?("Pilsner")
lagers += style[1]
end
if style[0].include?("Stout") || style[0].include?("Porter")
stouts += style[1]
end
if style[0].include?("Wit") || style[0].include?("Wheat")
wheats += style[1]
end
end
puts "Belgians, " + belgians.to_s
puts "India Pale Ales," + ipas.to_s
puts "Lagers and Pilsners," + lagers.to_s
puts "Stouts and Porters," + stouts.to_s
puts "Wheat and Wit," + wheats.to_s
exit
end
puts "Style".rjust(width) + " Rated Beers"
sorted_styles.each do |style|
puts style[0].rjust(width) + " " + ('|' * style[1])
end
puts ""
belgians = 0
ipas = 0
lagers = 0
stouts = 0
wheats = 0
sorted_styles.each do |style|
if style[0].include?("Belgian")
belgians += style[1]
end
if style[0].include?("India Pale Ale")
ipas += style[1]
end
if style[0].include?("Lager") || style[0].include?("Pilsner")
lagers += style[1]
end
if style[0].include?("Stout") || style[0].include?("Porter")
stouts += style[1]
end
if style[0].include?("Wit") || style[0].include?("Wheat")
wheats += style[1]
end
end
puts "Belgians".rjust(width) + " " + ('|' * belgians)
puts "India Pale Ales".rjust(width) + " " + ('|' * ipas)
puts "Lagers and Pilsners".rjust(width) + " " + ('|' * lagers)
puts "Stouts and Porters".rjust(width) + " " + ('|' * stouts)
puts "Wheat and Wit".rjust(width) + " " + ('|' * wheats)
puts ""