Start ThrottlePolicy class

pull/860/head
Rick Carlino 2018-05-18 11:46:02 -05:00
parent 4bdb6713d2
commit 1de2121554
2 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,18 @@
#
class ThrottlePolicy
attr_reader :rules
# A throttler object paired with a max limit
class Rule
attr_reader :throttle, :limit
def initialize(throttle, limit)
@throttle, @limit = throttle, limit
end
end
# Dictionary<Throttler, number>
def initialize(policy_rules)
@rules = policy_rules.map { |rule_set| Rule.new(*rule_set) }
end
end

View File

@ -0,0 +1,15 @@
require "spec_helper"
NOW = Time.new("2018-05-18T09:38:02.259-05:00")
describe Throttler do
it "initializes" do
policy = ThrottlePolicy.new(Throttler.new(1.minute, NOW) => 1_000,
Throttler.new(1.hour, NOW) => 10_000,
Throttler.new(1.day, NOW) => 100_000)
expect(policy.rules).to be
expect(policy.rules.map(&:limit).sort).to eq([1000, 10000, 100000])
actual = policy.rules.map(&:throttle).map(&:time_unit_in_seconds).sort
expected = [1.minute, 1.hour, 1.day]
expect(actual).to eq(expected)
end
end