Class: RequestMeter::Configuration
- Inherits:
-
Object
- Object
- RequestMeter::Configuration
- Defined in:
- lib/request_meter/configuration.rb
Overview
Configuration class holds settings for the RequestMeter middleware.
It manages API request quota limits, time windows, header keys, and the cache client.
Instance Attribute Summary collapse
-
#api_key_header ⇒ String
HTTP header name that holds the API key.
-
#cache_client ⇒ Object?
Cache client (e.g. Redis) used for tracking requests.
-
#quota_limit ⇒ Integer, Proc
Maximum allowed requests per quota period or a Proc returning limit by api_key.
-
#quota_period_seconds ⇒ Integer, Proc
Quota window length in seconds or a Proc returning period by api_key.
Instance Method Summary collapse
-
#get_quota_limit(api_key) ⇒ Integer
Returns the quota limit for a given API key.
-
#get_quota_period_seconds(api_key) ⇒ Integer
Returns the quota period (in seconds) for a given API key.
-
#initialize ⇒ void
constructor
Initializes a new Configuration with default values.
Constructor Details
#initialize ⇒ void
Initializes a new Configuration with default values.
Defaults:
-
quota_limit: 1000
-
quota_period_seconds: 3600 (1 hour)
-
api_key_header: “X-API-Key”
-
cache_client: nil
34 35 36 37 38 39 |
# File 'lib/request_meter/configuration.rb', line 34 def initialize @quota_limit = 1000 @quota_period_seconds = 3600 @api_key_header = "X-API-Key" @cache_client = nil end |
Instance Attribute Details
#api_key_header ⇒ String
Returns HTTP header name that holds the API key.
21 22 23 |
# File 'lib/request_meter/configuration.rb', line 21 def api_key_header @api_key_header end |
#cache_client ⇒ Object?
Returns Cache client (e.g. Redis) used for tracking requests.
23 24 25 |
# File 'lib/request_meter/configuration.rb', line 23 def cache_client @cache_client end |
#quota_limit ⇒ Integer, Proc
Returns maximum allowed requests per quota period or a Proc returning limit by api_key.
17 18 19 |
# File 'lib/request_meter/configuration.rb', line 17 def quota_limit @quota_limit end |
#quota_period_seconds ⇒ Integer, Proc
Returns quota window length in seconds or a Proc returning period by api_key.
19 20 21 |
# File 'lib/request_meter/configuration.rb', line 19 def quota_period_seconds @quota_period_seconds end |
Instance Method Details
#get_quota_limit(api_key) ⇒ Integer
Returns the quota limit for a given API key.
If quota_limit is a Proc, it is called with the api_key, otherwise returns the fixed limit.
47 48 49 50 51 52 53 |
# File 'lib/request_meter/configuration.rb', line 47 def get_quota_limit(api_key) if @quota_limit.is_a?(Proc) @quota_limit.call(api_key) else @quota_limit end end |
#get_quota_period_seconds(api_key) ⇒ Integer
Returns the quota period (in seconds) for a given API key.
If quota_period_seconds is a Proc, it is called with the api_key, otherwise returns the fixed period.
61 62 63 64 65 66 67 |
# File 'lib/request_meter/configuration.rb', line 61 def get_quota_period_seconds(api_key) if @quota_period_seconds.is_a?(Proc) @quota_period_seconds.call(api_key) else @quota_period_seconds end end |