monthly_data_limit.rb

 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.whole_dollars(
14			SIMPricing.five_gb_refill_price(currency, config)
15		)
16		{
17			step: step,
18			max: step * MAX_MULTIPLIER
19		}
20	end
21
22	def self.from_form(form, step:, max:)
23		limit = form.field("monthly_data_limit")&.value
24		return if limit.to_s.empty?
25
26		parse(limit, step: step, max: max)
27	end
28
29	def self.parse(limit, step:, max:)
30		limit = Integer(limit, 10)
31		unless (0..max).cover?(limit)
32			raise "Monthly data limit must be between 0 and #{max}"
33		end
34		unless (limit % step).zero?
35			raise "Monthly data limit must be a multiple of #{step}"
36		end
37
38		limit
39	end
40end