limiter-ruby 0.1.0 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -2
- data/lib/limiter/client.rb +3 -3
- data/lib/limiter/points.rb +14 -0
- data/lib/limiter/version.rb +1 -1
- data/lib/limiter.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 718993a35bb1f7a95a16d2a6a17af233d9d0909fc070895172c80e5afbfbb7d0
|
4
|
+
data.tar.gz: '026853b283dddcd996539c3d255250e25ee4871e4cccf8ac424707ffac18ea77'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a45d10cd11eb9500f9dc7fb77f379e509d49d1f91bd59ffe0f0bba698b88fba2f3a9edf57a1e5020ffa803032cf7d436d3bf166768c3e79fa8a65ae4a0159ca9
|
7
|
+
data.tar.gz: 9c3d29e2a42422b4bc02767e7c7f3248f462bab8d228fee536732481e5a737081a8aa99e512f37a199031ec2fbd139f904f05bdc6a5c4fbb84c0bd208ba211e9
|
data/README.md
CHANGED
@@ -17,10 +17,10 @@ LIMITER_TOKEN: [GENERATED TOKEN]
|
|
17
17
|
In the `Gemfile` add the following
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
gem "limiter"
|
20
|
+
gem "limiter-ruby"
|
21
21
|
```
|
22
22
|
|
23
|
-
##
|
23
|
+
## Simple Rate Limit Example
|
24
24
|
|
25
25
|
Assuming this is a Ruby on Rails app within ActiveJob
|
26
26
|
|
@@ -45,3 +45,29 @@ class ExpensiveJob < ApplicationJob
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
```
|
48
|
+
|
49
|
+
## Point Based Rate Limit Example
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class ExpensiveJob < ApplicationJob
|
53
|
+
|
54
|
+
def perform
|
55
|
+
rate_limit = limiter.check(shop.id) # A unique identifier
|
56
|
+
|
57
|
+
if rate_limit.exhausted? # Rate limit got hit
|
58
|
+
self.class.set(wait: rate_limit.resets_in).perform_later(..args)
|
59
|
+
return
|
60
|
+
else
|
61
|
+
# Good to continue expensive operation
|
62
|
+
# ...
|
63
|
+
# Perform query
|
64
|
+
limiter.used(250) # Points deducted for last query
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def limiter
|
70
|
+
Limiter::Points.new(namespace: "shopify", limit: 1000, period: 1.minute)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
data/lib/limiter/client.rb
CHANGED
@@ -35,11 +35,11 @@ module Limiter
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def url
|
38
|
-
"#{BASE_DOMAIN}/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
|
38
|
+
"#{BASE_DOMAIN}/ns/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
|
39
39
|
end
|
40
40
|
|
41
|
-
def request
|
42
|
-
HTTP.get(url, params: { token: token})
|
41
|
+
def request(params = {})
|
42
|
+
HTTP.get(url, params: { token: token}.merge(params))
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Limiter
|
4
|
+
class Points < Client
|
5
|
+
|
6
|
+
def url
|
7
|
+
"#{BASE_DOMAIN}/points/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def used(points)
|
11
|
+
RateLimitResponse.new(request({ used: points.to_i }))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/limiter/version.rb
CHANGED
data/lib/limiter.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: limiter-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bilal Budhani
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-05-
|
11
|
+
date: 2024-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- lib/limiter.rb
|
41
41
|
- lib/limiter/client.rb
|
42
42
|
- lib/limiter/error_handler.rb
|
43
|
+
- lib/limiter/points.rb
|
43
44
|
- lib/limiter/rate_limit_response.rb
|
44
45
|
- lib/limiter/version.rb
|
45
46
|
- limiter.gemspec
|