limiter-ruby 0.2.5 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96c4939d6e56cfa24e78beec860c0a19777d28af350de443db9c2c344a0f683f
4
- data.tar.gz: c8e52e373ba57da0c3499ed5216003c193f55bf20fc3571ae106c8efd8ef22c8
3
+ metadata.gz: adbf765b7ef3aa5873ed8a1ad83b70b23dc1169b447d389f6757f88f83dbc031
4
+ data.tar.gz: 7d36b0b18a42fba6d217c131a2d8c52441fd42927071330a9a4af94ab504db5a
5
5
  SHA512:
6
- metadata.gz: 61d1e3e3e19402fd2e198b7e6cbb0686e173c268dea99f95cacf34942b962e93098dd92e5748c2f9eb4083063c0ae51183005cf38d49fda8206bd607a996d5ad
7
- data.tar.gz: 60bcdb87f03d1a80713fc0d1b50c92050b01dcd187a0d3743f234a56ee7b141b72ffe97a463b688474378ef6d465f57e916c4aee21a9fdc8efecf7cf428ac27c
6
+ metadata.gz: 98feb4fc57be513ba79d1f37050dada93f4e877d31f358acdc8ea42a3e607fc6c04a5cbb1a7fed2b50e34f4b75ce07b0372b3c40fa80b02bfc2e5febecc9d42f
7
+ data.tar.gz: 79d8b18d6a7c6e4648caa3973e59cbe292ecf4ba3f493d5391e8b271e80a60e2d5a04f510bb7bc203ca9f20d8bc681e531b21657a64a8685d29fe20663ebc3ec
data/README.md CHANGED
@@ -20,9 +20,20 @@ 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
- Assuming this is a Ruby on Rails app within ActiveJob
36
+ Sample API controller that checks the rate limit for the current user and increments the request count
26
37
 
27
38
  ```ruby
28
39
  class ApiController < ApplicationController
@@ -34,15 +45,45 @@ class ApiController < ApplicationController
34
45
 
35
46
  private
36
47
  def rate_limit
37
- rate_limit = limiter.check(user.id)
48
+ # check the rate limit for the current user and increment the request count
49
+ rate_limit = limiter.check(current_user.id)
38
50
 
39
- if rate_limit.exhausted?
51
+ if rate_limit.exceeded?
40
52
  render json: { error: "Rate limit exceeded" }, status: :too_many_requests
41
53
  end
42
54
  end
43
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
+ Sample ActiveJob that checks the rate limit for the shop and updates the request count
66
+
67
+ ```ruby
68
+ class DataSyncJob < ApplicationJob
69
+ queue_as :default
70
+
71
+ def perform
72
+ rate_limit = limiter.check(shop.id)
73
+ if rate_limit.exhausted?
74
+ self.class.set(wait: rate_limit.resets_in).perform_later
75
+ return
76
+ end
77
+
78
+ # continue the action
79
+ response = ... # query_cost = 100
80
+
81
+ limiter.used(response.query_cost) # mark 100 points used
82
+ end
83
+
84
+ # Allow 1000 points per minute
44
85
  def limiter
45
- Limiter::Client.new(token: ENV["LIMITER_TOKEN"], namespace: "openai", limit: 60, interval: 1.minute)
86
+ Limiter::Points.new(namespace: "shopify", limit: 1000, interval: 20.seconds)
46
87
  end
47
88
  end
48
89
  ```
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "http"
4
+
3
5
  module Limiter
4
6
  class Client
5
7
 
@@ -47,7 +49,7 @@ module Limiter
47
49
  end
48
50
 
49
51
  def request(params = {})
50
- HTTP
52
+ ::HTTP
51
53
  .auth("Bearer #{Limiter.configuration.api_token}")
52
54
  .headers("User-Agent" => "Limiter-Ruby/#{Limiter::VERSION}")
53
55
  .get(BASE_URL + limiter_path, params: params)
@@ -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).to_i
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limiter
4
- VERSION = "0.2.5"
4
+ VERSION = "0.2.7"
5
5
  end
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.2.5
4
+ version: 0.2.7
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-12-14 00:00:00.000000000 Z
11
+ date: 2024-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http