-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation_reputation.rb
More file actions
62 lines (50 loc) · 1.54 KB
/
location_reputation.rb
File metadata and controls
62 lines (50 loc) · 1.54 KB
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
require 'json'
require 'pry'
class LocationReputation
def initialize
sample = IO.read("/media/dilum/96648732-125e-4b47-aa00-74d25da99130/Data/Users.json")
puts "loaded"
hash = JSON.parse(sample)
puts "hashed"
calculation = calculate_reputation(hash)
File.write('/media/dilum/96648732-125e-4b47-aa00-74d25da99130/Data/location_reputation.json', JSON.pretty_generate(calculation))
end
def calculate_reputation(hash)
locations = ["Lanka", "Kandy", "Colombo"]
mapping = {}
sortedmapping = {}
count = 0
hash.each_with_index do |element, index|
p count
if (element["Location"])
if (element["Location"].match(Regexp.union(locations)))
reputation = element["Reputation"]
name = element["DisplayName"]
location = "Sri Lanka"
if mapping.key?(location)
mapping[location][name] = reputation.to_i
else
mapping[location] = {}
mapping[location][name] = reputation.to_i
end
end
end
count = count + 1
end
mapping.each do |element, values|
sorted_values = values.sort_by {|_key, value| -value}
chunked_values = sorted_values.each_slice(20).to_a
count = 1
chunked_values.each_with_index do |element, index|
name = "#{(count)} to #{count + 19}"
sortedmapping[name] = element.to_h
count = count + 20
if index == 20
break
end
end
end
sortedmapping
end
end
location_reputation = LocationReputation.new