co-limit 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72909f9bdd98c89548d9513f2431095a7edf110f0b2b62a3f928cb5f2f65ddba
4
- data.tar.gz: a9be065e44bedb462c7595cf35bbb2a7f1feb9097f97581e210130891089d21e
3
+ metadata.gz: 5f54e6c9ae0b841090b1fe2eae972242c4a416eb504de4940cc807a500f7d27a
4
+ data.tar.gz: ebc7e45c7c29b425a4391230173f8db3a44c288482a917f2ef8e57be75552a31
5
5
  SHA512:
6
- metadata.gz: 20aee9b9a5a274424f880119f9dd7c59eb82624ceef1d3194e0c527b1a029370628563bb22885d5b688210504a02b244a744f60d2565825f7ad1970ac5e1f8ee
7
- data.tar.gz: 3992bd40ac746931e6a797e0838cc36318029ca394cb425d84598636597761a461964857ce6ad35dc21ae79be8e434522758a7f2eb7ec1974a17c74447f51b1e
6
+ metadata.gz: 8535547535b239b227de6f78dfa0c37380e33c77f74db7c85ad91792068847b44f9074b02b3e034f9d94c8c89d517ac884ab18f111aaaca8bdeee02fc774b2ba
7
+ data.tar.gz: b37ee6f384eed0728a8b4a29b3271475bbe42607c0d879471081947ec4d2c34ef7d14be28f95b305094cf2cb8c548c6aa3a5a1b77fdc5fbf0eac52711fe0ac53
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem that provides flexible, Redis-backed rate limiting utilities. It supports both Fixed Window and Rolling Window (Sliding Log) strategies, to easily control the number of allowed requests for a given identifier within a time window.
5
5
 
6
- You can define rate-limiting rules dynamically using a Proc, and configure Redis via environment variables (REDIS_HOST, REDIS_PORT, REDIS_PASSWORD) or by passing connection details directly.
6
+ You can define rate-limiting rules dynamically using a lambda, and configure Redis via environment variables (REDIS_HOST, REDIS_PORT, REDIS_PASSWORD) or by passing connection details directly.
7
7
 
8
8
  This gem is ideal for APIs, background jobs, or any system that needs simple, efficient throttling logic.
9
9
 
@@ -12,13 +12,13 @@ This gem is ideal for APIs, background jobs, or any system that needs simple, ef
12
12
  To install the gem and add it to your application's Gemfile, execute:
13
13
 
14
14
  ```bash
15
- $ bundle add limit
15
+ $ bundle add co-limit
16
16
  ```
17
17
 
18
18
  If you are not using Bundler, you can install the gem directly by running:
19
19
 
20
20
  ```bash
21
- $ gem install limit
21
+ $ gem install co-limit
22
22
  ```
23
23
 
24
24
  ## Usage
@@ -85,7 +85,7 @@ You can configure the Redis connection either by passing the connection details
85
85
  ### Key Points:
86
86
 
87
87
  - **identifier_prefix**: A namespace prefix for Redis keys (e.g., `"access"`).
88
- - **limit_calculator**: A `Proc` that takes a key (e.g., `"user_id:site_name"`) and returns a hash with `max_requests` and `window_seconds`.
88
+ - **limit_calculator**: A `Lambda` that takes a key (e.g., `"user_id:site_name"`) and returns a hash with `max_requests` and `window_seconds`.
89
89
 
90
90
  ### Supported Rate Limiters:
91
91
 
data/lib/limit/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limit
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/limit.rb CHANGED
@@ -43,14 +43,14 @@ module Limit
43
43
 
44
44
  def initialize(identifier_prefix:, limit_calculator:, host: nil, port: nil, password: nil)
45
45
 
46
- # @param identifier_prefix: [String] A namespace prefix for redis keys for this limiter instance
47
- # @param limit_calculator: [Proc] A method that takes a key(String) and returns hash: {max_requests: Integer, window_seconds: Integer}
46
+ # @param identifier_prefix: [String] A namespace prefix for redis keys for this limiter instance
47
+ # @param limit_calculator: [Lambda] A method that takes a key(String) and returns hash: {max_requests: Integer, window_seconds: Integer}
48
48
 
49
49
  unless identifier_prefix.is_a?(String) && !identifier_prefix.empty?
50
50
  raise ArgumentError, 'identifier_prefix must be a non-empty String'
51
51
  end
52
52
 
53
- raise ArgumentError, 'limit_calculator must be a Proc' unless limit_calculator.is_a?(Proc)
53
+ raise ArgumentError, 'limit_calculator must be a Lambda' unless limit_calculator.lambda?
54
54
 
55
55
  # Will be using the same connection across all instance unless wanted to connect to diff instance of redis
56
56
 
@@ -78,7 +78,7 @@ module Limit
78
78
  raise NotImplementedError "#{self.class.name} must implement the allowed? method"
79
79
  end
80
80
 
81
- def get_key(prefix)
81
+ def get_key(key)
82
82
  raise NotImplementedError "#{self.class.name} must implement the get_key() method"
83
83
  end
84
84
 
@@ -136,9 +136,9 @@ module Limit
136
136
  results[0] <= max_requests
137
137
  end
138
138
 
139
- def get_key(prefix, window_seconds)
139
+ def get_key(key, window_seconds)
140
140
  time_window = (Time.now.to_i / window_seconds) * window_seconds
141
- "#{@identifier_prefix}:#{prefix}:#{time_window.to_s}"
141
+ "#{@identifier_prefix}:#{key}:#{time_window.to_s}"
142
142
  end
143
143
  end
144
144
 
@@ -167,8 +167,8 @@ module Limit
167
167
  results[2] <= max_requests
168
168
  end
169
169
 
170
- def get_key(prefix)
171
- "#{@identifier_prefix}:#{prefix}"
170
+ def get_key(key)
171
+ "#{@identifier_prefix}:#{key}"
172
172
  end
173
173
  end
174
174
 
data/sig/limit/limit.rbs CHANGED
@@ -1,37 +1,37 @@
1
1
  type redis = untyped
2
2
 
3
3
  module Limit
4
- VERSION: String
5
4
 
6
- class BaseLimiter
7
- self.@redis: redis
8
- @logger: Logger
5
+ class BaseLimiter
6
+ self.@logger: Logger
7
+ self.@redis: redis
8
+ @logger: Logger
9
9
 
10
- @redis: redis | nil
10
+ @redis: redis | nil
11
11
 
12
- def self.connection: -> nil
12
+ def self.connection: -> nil
13
13
 
14
- def self.create_connection: (String, Integer, String) -> redis
14
+ def self.create_connection: (String, Integer, String) -> redis
15
15
 
16
- def self.log: (String, String) -> nil
16
+ def self.log: (String, String) -> nil
17
17
 
18
- def self.logger: -> Logger
18
+ def self.logger: -> Logger
19
19
 
20
- attr_reader identifier_prefix: String
21
- attr_reader limit_calculator: Proc
20
+ attr_reader identifier_prefix: String
21
+ attr_reader limit_calculator: Proc
22
22
 
23
- def initialize: -> untyped
23
+ def initialize: -> untyped
24
24
 
25
- def allowed?: (String) -> bool
25
+ def allowed?: (String) -> bool
26
26
 
27
- def get_current_limit: (String) -> Hash[String, Integer]
27
+ def get_current_limit: (String) -> Hash[String, Integer]
28
28
 
29
- def get_key: (String) -> String
29
+ def get_key: (String) -> String
30
30
 
31
- def current_micros: (nil) -> Integer
31
+ def current_micros: (nil) -> Integer
32
32
 
33
- def log: (String, String) -> nil
33
+ def log: (String, String) -> nil
34
34
 
35
- def redis_pipeline: () { (redis) -> untyped } -> untyped
35
+ def redis_pipeline: () { (redis) -> untyped } -> untyped
36
36
  end
37
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: co-limit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - CosmicOppai
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-04-29 00:00:00.000000000 Z
11
+ date: 2025-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis