-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonthly_calculator.rb
More file actions
41 lines (32 loc) · 884 Bytes
/
monthly_calculator.rb
File metadata and controls
41 lines (32 loc) · 884 Bytes
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
require 'json'
require 'pry'
class MonthlyCalculator
def initialize
sample = IO.read("/media/dilum/96648732-125e-4b47-aa00-74d25da99130/Data/Posts.json")
hash = JSON.parse(sample)
calculation = calculate_monthly(hash)
File.write('/media/dilum/96648732-125e-4b47-aa00-74d25da99130/Data/monthly.json', JSON.pretty_generate(calculation))
end
def calculate_monthly(hash)
mapping = {}
count = 0
hash.each_with_index do |element, index|
p count
year = element["CreationDate"].split("-").first
month = element["CreationDate"].split("-")[1]
if mapping.key?(year)
if mapping[year].key?(month)
mapping[year][month] = mapping[year][month] + 1
else
mapping[year][month] = 1
end
else
mapping[year] = {}
mapping[year][month] = 1
end
count = count + 1
end
mapping
end
end
monthly_calculator = MonthlyCalculator.new