1# frozen_string_literal: true
2
3class Plan
4 def self.for(plan_name)
5 plan = CONFIG[:plans].find { |p| p[:name] == plan_name }
6 raise "No plan by that name" unless plan
7
8 new(plan)
9 end
10
11 def initialize(plan)
12 @plan = plan
13 end
14
15 def name
16 @plan[:name]
17 end
18
19 def currency
20 @plan[:currency]
21 end
22
23 def monthly_price
24 BigDecimal(@plan[:monthly_price]) / 10000
25 end
26
27 def merchant_account
28 CONFIG[:braintree][:merchant_accounts].fetch(currency) do
29 raise "No merchant account for this currency"
30 end
31 end
32
33 def minute_limit
34 Limit.for("minute", @plan[:minutes])
35 end
36
37 def message_limit
38 Limit.for("message", @plan[:messages])
39 end
40
41 class Limit
42 def self.for(unit, from_config)
43 case from_config
44 when :unlimited
45 Unlimited.new(unit)
46 else
47 new(unit: unit, **from_config)
48 end
49 end
50
51 value_semantics do
52 unit String
53 included Integer
54 price Integer
55 end
56
57 def to_s
58 "#{included} #{unit}s " \
59 "(overage $#{'%.4f' % (price.to_d / 10000)} / #{unit})"
60 end
61
62 class Unlimited
63 def initialize(unit)
64 @unit = unit
65 end
66
67 def to_s
68 "unlimited #{@unit}s"
69 end
70 end
71 end
72end