limiter-ruby 0.2.4 → 0.2.6
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 +4 -4
- data/README.md +43 -2
- data/lib/limiter/points.rb +1 -1
- data/lib/limiter/response_handler.rb +5 -5
- data/lib/limiter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ba0ddf343988f20e25bf09cd9e8d79d07df1012733709dca66742098f9f31a1
|
4
|
+
data.tar.gz: 35418ed42f3e2d435f872e964f5738343d2b2b25c9cccf270c7060161bddca0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b54697bbfecb6e03df15fb14e6e4ed35de4a4c56c69f2d1197e47e1d31616b00a8f4f305e1d6af81c90fdfb39e6181d25a8c05e2b12bc920459060202a0054d
|
7
|
+
data.tar.gz: 223a007534acba31fd0ae706d5ba8d17be190b898e1963b2a436dc130f0c86a5191b8ca5f3d0f97ebd78e1c9074495ca5bc2f4b4d1cfe94b60f8804cccfe20d3
|
data/README.md
CHANGED
@@ -20,6 +20,17 @@ In the `Gemfile` add the following
|
|
20
20
|
gem "limiter-ruby"
|
21
21
|
```
|
22
22
|
|
23
|
+
### Configure the gem
|
24
|
+
|
25
|
+
In the `config/initializers/limiter.rb` file add the following
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Limiter.configure do |config|
|
29
|
+
config.api_token = ENV["LIMITER_TOKEN"]
|
30
|
+
config.raise_errors = true
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
23
34
|
## Simple Rate Limit Example
|
24
35
|
|
25
36
|
Assuming this is a Ruby on Rails app within ActiveJob
|
@@ -34,15 +45,45 @@ class ApiController < ApplicationController
|
|
34
45
|
|
35
46
|
private
|
36
47
|
def rate_limit
|
37
|
-
|
48
|
+
# check the rate limit for the current user and increment the request count
|
49
|
+
rate_limit = limiter.check(current_user.id)
|
50
|
+
|
51
|
+
if rate_limit.exceeded?
|
52
|
+
render json: { error: "Rate limit exceeded" }, status: :too_many_requests
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Allow 120 requests per minute
|
57
|
+
def limiter
|
58
|
+
Limiter::Client.new(namespace: "openai", limit: 120, interval: 1.minute)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
## Points Rate Limit Example
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class ApiController < ApplicationController
|
67
|
+
before_action :rate_limit
|
68
|
+
|
69
|
+
def index
|
70
|
+
# continue the action
|
71
|
+
|
72
|
+
limiter.used(100) # mark 100 points used
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
def rate_limit
|
77
|
+
rate_limit = limiter.check(current_user.id)
|
38
78
|
|
39
79
|
if rate_limit.exhausted?
|
40
80
|
render json: { error: "Rate limit exceeded" }, status: :too_many_requests
|
41
81
|
end
|
42
82
|
end
|
43
83
|
|
84
|
+
# Allow 1000 points per minute
|
44
85
|
def limiter
|
45
|
-
Limiter::
|
86
|
+
Limiter::Points.new(namespace: "shopify", limit: 1000, interval: 1.minute)
|
46
87
|
end
|
47
88
|
end
|
48
89
|
```
|
data/lib/limiter/points.rb
CHANGED
@@ -20,12 +20,16 @@ module Limiter
|
|
20
20
|
|
21
21
|
def resets_in
|
22
22
|
if signed_request? && !resets_at.nil?
|
23
|
-
(Time.parse(resets_at) - Time.now)
|
23
|
+
(Time.parse(resets_at) - Time.now)
|
24
24
|
else
|
25
25
|
0
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
def resets_at
|
30
|
+
response_data["resetsAt"]
|
31
|
+
end
|
32
|
+
|
29
33
|
def remaining
|
30
34
|
response_data["remaining"]
|
31
35
|
end
|
@@ -42,9 +46,5 @@ module Limiter
|
|
42
46
|
def response_data
|
43
47
|
@_body ||= @response.parse
|
44
48
|
end
|
45
|
-
|
46
|
-
def resets_at
|
47
|
-
response_data["resetsAt"]
|
48
|
-
end
|
49
49
|
end
|
50
50
|
end
|
data/lib/limiter/version.rb
CHANGED