~singpolyma/sgx-jmp

ref: 023c87a11bbaa6bd3eaeb3977330b8ea80ded0cd sgx-jmp/lib/usage_report.rb -rw-r--r-- 940 bytes
023c87a1Stephen Paul Weber Quieter voicemail beep 7 months ago
                                                                                
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
# frozen_string_literal: true

require_relative "./form_table"

class UsageReport
	def initialize(report_for, messages, minutes)
		@report_for = report_for
		@messages = messages
		@minutes = minutes
	end

	def ==(other)
		report_for == other.report_for &&
			messages == other.messages &&
			minutes == other.minutes
	end

	def form
		form = Blather::Stanza::X.new(:result)
		form.title =
			form.instructions =
				"Usage from #{report_for.first} to #{report_for.last}"
		form_table.add_to_form(form)
		form
	end

	def form_table
		total_messages = 0
		total_minutes = 0

		FormTable.new(
			@report_for.first.downto(@report_for.last).map { |day|
				total_messages += @messages[day]
				total_minutes += @minutes[day]
				[day, @messages[day], @minutes[day]]
			} + [["Total", total_messages, total_minutes]],
			day: "Day", messages: "Messages", minutes: "Minutes"
		)
	end

protected

	attr_reader :report_for, :messages, :minutes
end