limiter-ruby 0.2.4 → 0.2.6

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: 442bbfcc13722e3bbffe3edc6bb8ea56bfd19d84e4c13ba76dcd941b3bcdcb78
4
- data.tar.gz: 6c931ecf9b1a4ac9395e651b624e86fb1e6c2e5ee206be86f8f302bef554e706
3
+ metadata.gz: 0ba0ddf343988f20e25bf09cd9e8d79d07df1012733709dca66742098f9f31a1
4
+ data.tar.gz: 35418ed42f3e2d435f872e964f5738343d2b2b25c9cccf270c7060161bddca0a
5
5
  SHA512:
6
- metadata.gz: 3328f0aa9e901c793feeb41a39ada21502a1e3edfb2960a5d8c18d7548e53e976fcb8bfb279c942a80a1514ee5d05aa431d17e0cd502b5c371ffca6aeab603e6
7
- data.tar.gz: dc5ec8dfa8ac8ffc85888a4b9b18c88cddde408cff24f4df83cd1612b789b6d8e9705b1ecff0b309d337f3d694d0269d5d02247f9ce8e715a12abc009a6e387d
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
- 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)
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::Client.new(token: ENV["LIMITER_TOKEN"], namespace: "openai", limit: 60, interval: 1.minute)
86
+ Limiter::Points.new(namespace: "shopify", limit: 1000, interval: 1.minute)
46
87
  end
47
88
  end
48
89
  ```
@@ -6,7 +6,7 @@ module Limiter
6
6
  delegate :remaining, :points, :to => :response
7
7
 
8
8
  def limiter_path
9
- "/pts/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
9
+ "/pts/#{namespace}/#{limit}/#{formatted_interval}/#{identifier}"
10
10
  end
11
11
 
12
12
  def used(points)
@@ -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.4"
4
+ VERSION = "0.2.6"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: limiter-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bilal Budhani