limiter-ruby 0.1.7 → 0.1.10

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: a055045f6d57536581928a0f095acecb06311ca8389543c2b5a06ff87c69c815
4
- data.tar.gz: bf1e7886581cd9ef3ae10a8f0e92217cec915f9147ebf3bd3f89547714d6087d
3
+ metadata.gz: 2ec1028bbed03695ce7f6722da569723cbe4dbe54a692c8b3b161dde3aa9b3e7
4
+ data.tar.gz: 954bf67ede37fad7012f0b82e375901db14e2ed7f63ca5b7421f286810cba977
5
5
  SHA512:
6
- metadata.gz: c5b1cc36d4bc5b5e6420a0d335129c86575494f3c126d81fe508e9d9dbb88ebc6487dfb70332c62a2f1055a428a833ef939fe207654def88b6286946f2b5e4f5
7
- data.tar.gz: 0ee636990a88646adfc3feff075d5556b45645c24ccaf39c5cd6e2c11e3a152c19195ca13f90076470b89f2a8fbd583f6ec7f731388dd3fceb710981dcab6ec5
6
+ metadata.gz: 7e5b789e9516d90cf049e2027b9bac3c0967ccac67445ba5a1856cfcc21ef22e5d0b63a2dc56da3bc055c2d0a4cece9291638ee42655ad579de39ad68af30d16
7
+ data.tar.gz: 4599777458c083480cc0aff6c2a181e7043090d445f1b514cea6afa68a91df1188ad8fca58bf9b8b26e9e4966d635ce806dfaf829b27226102bd6f39fe2386d5
data/README.md CHANGED
@@ -25,49 +25,24 @@ gem "limiter-ruby"
25
25
  Assuming this is a Ruby on Rails app within ActiveJob
26
26
 
27
27
  ```ruby
28
- class ExpensiveJob < ApplicationJob
28
+ class ApiController < ApplicationController
29
+ before_action :rate_limit
29
30
 
30
- def perform
31
- rate_limit = limiter.check(user.id) # A unique identifier
32
-
33
- if rate_limit.exhausted? # Rate limit got hit
34
- self.class.set(wait: rate_limit.resets_in).perform_later(..args)
35
- return
36
- end
37
-
38
- # Good to continue expensive operation
39
- # ...
31
+ def index
32
+ # Good to continue
40
33
  end
41
34
 
42
35
  private
43
- def limiter
44
- Limiter::Client.new(namespace: "openai", limit: 60, period: 1.minute)
45
- end
46
- end
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
36
+ def rate_limit
37
+ rate_limit = limiter.check(user.id)
56
38
 
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
39
+ if rate_limit.exhausted?
40
+ render json: { error: "Rate limit exceeded" }, status: :too_many_requests
65
41
  end
66
42
  end
67
43
 
68
- private
69
44
  def limiter
70
- Limiter::Points.new(namespace: "shopify", limit: 1000, period: 1.minute)
45
+ Limiter::Client.new(token: ENV["LIMITER_TOKEN"], namespace: "openai", limit: 60, interval: 1.minute)
71
46
  end
72
47
  end
73
48
  ```
@@ -5,15 +5,15 @@ module Limiter
5
5
 
6
6
  BASE_DOMAIN = "https://api.limiter.dev".freeze
7
7
 
8
- attr_reader :namespace, :limit, :period, :identifier, :token
8
+ attr_reader :namespace, :limit, :interval, :identifier, :token
9
9
 
10
- def initialize(namespace:, limit:, period:)
10
+ def initialize(namespace:, limit:, interval:, token: ENV["LIMITER_TOKEN"])
11
11
  @namespace = namespace
12
12
  @limit = limit
13
- @period = period.to_i
14
- @token = ENV["LIMITER_TOKEN"]
13
+ @interval = interval.to_i
14
+ @token = token || ENV["LIMITER_TOKEN"]
15
15
 
16
- ErrorHandler.error("LIMITER_TOKEN environment variable is not set") if @token.nil?
16
+ ErrorHandler.error("token is not set") if @token.nil?
17
17
  end
18
18
 
19
19
  def check(identifier)
@@ -21,27 +21,34 @@ module Limiter
21
21
  RateLimitResponse.new(request)
22
22
  end
23
23
 
24
- def formatted_period
25
- case @period
24
+ def formatted_interval
25
+ case @interval
26
26
  when 1..59
27
- (@period).to_s + "s"
27
+ (@interval).to_s + "s"
28
28
  when 60..3599
29
- (@period / 60).to_s + "m"
29
+ (@interval / 60).to_s + "m"
30
30
  when 3600..86_399
31
- (@period / 3600).to_s + "h"
31
+ (@interval / 3600).to_s + "h"
32
32
  when 86_400..1_209_599
33
- (@period / 86_400).to_s + "d"
33
+ (@interval / 86_400).to_s + "d"
34
34
  else
35
- ErrorHandler.error("Invalid period")
35
+ ErrorHandler.error("Invalid interval")
36
36
  end
37
37
  end
38
38
 
39
- def url
40
- "#{BASE_DOMAIN}/ns/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
39
+ def limiter_path
40
+ "/rl/#{namespace}/#{limit}/#{formatted_interval}/#{identifier}"
41
41
  end
42
42
 
43
43
  def request(params = {})
44
- HTTP.get(url, params: { token: token}.merge(params))
44
+ @_conn ||= Faraday.new(url: BASE_DOMAIN) do |conn|
45
+ conn.request :authorization, :Bearer, token
46
+ conn.response :json
47
+ conn.adapter Faraday.default_adapter
48
+ conn.headers["User-Agent"] = "Limiter-Ruby/#{Limiter::VERSION}"
49
+ end
50
+
51
+ @_conn.get(limiter_path, params)
45
52
  end
46
53
  end
47
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limiter
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.10"
5
5
  end
data/lib/limiter.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "http"
4
3
  require "limiter/client"
5
4
  require "limiter/points"
6
5
  require "limiter/rate_limit_response"
data/limiter.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["Bilal Budhani"]
9
9
  spec.email = ["bilal@bilalbudhani.com"]
10
10
 
11
- spec.summary = "Easily Build & Track Rate Limits"
12
- spec.description = "Limiter enables building rate limits for your application with ease. It provides a simple interface to define rate limits and track usage."
11
+ spec.summary = "Toolkit for building rate limits for your application"
12
+ spec.description = "Limiter is a toolkit for building rate limits for your application. It provides a simple interface to define rate limits and track usage."
13
13
  spec.homepage = "https://limiter.dev"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = ">= 2.6.0"
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.require_paths = ["lib"]
32
32
 
33
33
  # Uncomment to register a new dependency of your gem
34
- spec.add_dependency "http", "~> 5.0"
34
+ spec.add_dependency "faraday", "~> 2.0"
35
35
 
36
36
  # For more information and examples about making a new gem, check out our
37
37
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: limiter-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.10
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-22 00:00:00.000000000 Z
11
+ date: 2024-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: http
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '5.0'
27
- description: Limiter enables building rate limits for your application with ease.
28
- It provides a simple interface to define rate limits and track usage.
26
+ version: '2.0'
27
+ description: Limiter is a toolkit for building rate limits for your application. It
28
+ provides a simple interface to define rate limits and track usage.
29
29
  email:
30
30
  - bilal@bilalbudhani.com
31
31
  executables: []
@@ -66,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
68
  requirements: []
69
- rubygems_version: 3.5.3
69
+ rubygems_version: 3.5.18
70
70
  signing_key:
71
71
  specification_version: 4
72
- summary: Easily Build & Track Rate Limits
72
+ summary: Toolkit for building rate limits for your application
73
73
  test_files: []