Skip to content

Commit

Permalink
Merge pull request #5 from seibii/feature/unkwnon_option_error
Browse files Browse the repository at this point in the history
add: validation for unknown options in ThrottleParams initialization
  • Loading branch information
hazi committed Apr 14, 2024
2 parents 3c6360f + 0cbc932 commit dbeb120
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/speed_limiter/throttle_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
module SpeedLimiter
# Throttle params model
class ThrottleParams
KNOWN_OPTIONS = %i[on_throttled retry].freeze

def initialize(config:, key:, limit:, period:, **options)
@config = config
@key = key
@limit = limit
@period = period
@options = options

return unless (unknown_options = options.keys - KNOWN_OPTIONS).any?

raise ArgumentError, "Unknown options: #{unknown_options.join(', ')}"
end

attr_reader :config, :key, :limit, :period
Expand Down
16 changes: 16 additions & 0 deletions spec/speed_limiter/throttle_params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe SpeedLimiter::Throttle do
let!(:config) { SpeedLimiter::Config.new }

describe "#new" do
context "with unknown options" do
it "raises ArgumentError" do
expect { described_class.new(config: config, key: SecureRandom.uuid, limit: 1, period: 1, unknown: true) }
.to raise_error(ArgumentError)
end
end
end
end

0 comments on commit dbeb120

Please sign in to comment.