Class: RequestMeter::Configuration

Inherits:
Object
  • Object
show all
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.

Examples:

Default configuration

config = RequestMeter::Configuration.new
config.quota_limit           #=> 1000
config.quota_period_seconds  #=> 3600
config.api_key_header        #=> "X-API-Key"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

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_headerString

Returns HTTP header name that holds the API key.

Returns:

  • (String)

    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_clientObject?

Returns Cache client (e.g. Redis) used for tracking requests.

Returns:

  • (Object, nil)

    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_limitInteger, Proc

Returns maximum allowed requests per quota period or a Proc returning limit by api_key.

Returns:

  • (Integer, Proc)

    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_secondsInteger, Proc

Returns quota window length in seconds or a Proc returning period by api_key.

Returns:

  • (Integer, Proc)

    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.

Parameters:

  • api_key (String)

    the API key to check

Returns:

  • (Integer)

    the quota limit for that API key



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.

Parameters:

  • api_key (String)

    the API key to check

Returns:

  • (Integer)

    the quota period in seconds for that API key



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