sim_kind.rb

 1# frozen_string_literal: true
 2
 3class SIMKind
 4	def initialize(variant)
 5		@klass =
 6			case variant
 7			when "sim"
 8				SIMOrder
 9			when "esim"
10				SIMOrder::ESIM
11			else
12				raise "Unknown SIM kind"
13			end
14		@variant = variant
15	end
16
17	def self.from_form(form)
18		new(form.field("sim_kind")&.value)
19	end
20
21	# @param [Customer] customer
22	# @raise [ArgumentError] If matching `CONFIG[:sims]`
23	# 	key not found for `@variant` and customer
24	def get_processor(customer)
25		cfg = cfg(customer.currency)
26		raise ArgumentError, "Invalid config" unless cfg
27
28		@klass.for(customer, **cfg)
29	end
30
31	# @return [String] The result of either
32	# 	SIMOrder.label or SIMOrder::ESIM.label
33	def label
34		@klass.label
35	end
36
37	# @param [Customer] customer
38	# @return [Float, NilClass] Returns nil if `customer.currency`
39	# 	is nil, or if @variant is malformed.
40	def price(customer)
41		cfg = cfg(customer.currency || :USD)
42		cfg[:price] / 100.to_d
43	end
44
45protected
46
47	# @param [Symbol] currency One of the currencies from
48	# 	CONFIG[:plans] entries
49	def cfg(currency)
50		CONFIG.dig(:sims, @variant.to_sym, currency)
51	end
52end