1# frozen_string_literal: true
2
3require_relative "sim_pricing"
4
5module MonthlyDataLimit
6 MAX_MULTIPLIER = 10
7
8 def self.for_sims(sims, currency, config)
9 sims.empty? ? {} : for_currency(currency, config)
10 end
11
12 def self.for_currency(currency, config)
13 step = SIMPricing.five_gb_refill_price(currency, config).ceil
14 {
15 step: step,
16 max: step * MAX_MULTIPLIER
17 }
18 end
19
20 def self.from_form(form, step:, max:)
21 limit = form.field("monthly_data_limit")&.value
22 return if limit.to_s.empty?
23
24 parse(limit, step: step, max: max)
25 end
26
27 def self.parse(limit, step:, max:)
28 limit = Integer(limit, 10)
29 unless (0..max).cover?(limit)
30 raise "Monthly data limit must be between 0 and #{max}"
31 end
32 unless (limit % step).zero?
33 raise "Monthly data limit must be a multiple of #{step}"
34 end
35
36 limit
37 end
38end