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