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			end
12		@variant = variant
13	end
14
15	def self.from_form(form)
16		new(form.field("sim_kind")&.value)
17	end
18
19	# @param [Customer] customer
20	# @raise [ArgumentError] If matching `CONFIG[:sims]`
21	# 	key not found for `@variant` and customer
22	def get_processor(customer)
23		cfg = cfg(customer.currency)
24		raise ArgumentError, "Invalid config" unless cfg
25
26		@klass.new(
27			customer,
28			**cfg
29		)
30	end
31
32	# @return [String] The result of either
33	# 	SIMOrder.label or SIMOrder::ESIM.label
34	def label
35		@klass.label
36	end
37
38	# @param [Customer] customer
39	# @return [Float, NilClass] Returns nil if `customer.currency`
40	# 	is nil, or if @variant is malformed.
41	def price(customer)
42		cfg = cfg(customer.currency)
43		return nil unless cfg
44
45		cfg[:price] / 100.to_d
46	end
47
48protected
49
50	# @param [Symbol] currency One of the currencies from
51	# 	CONFIG[:plans] entries
52	def cfg(currency)
53		CONFIG.dig(:sims, @variant.to_sym, currency)
54	end
55end