ruby_rate_limiter 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 440ed0f666d00afdb77283021ab3805b4fa18a08e5a5aea371b04d7b6d6cf299
4
- data.tar.gz: 75309c235f17894fbe85bc034dcfb2126431d421e3c11ef089879fcedc280355
3
+ metadata.gz: 6d487b84e9e255e4a2fe403fc98961ee09083d7be86a5bc929e9a106e9455b87
4
+ data.tar.gz: 28bf084c36db3488761ef6ff9014a0a79743a23c78d1085e55188a2d09262217
5
5
  SHA512:
6
- metadata.gz: 9ae4e96315c08a5e84cadc97260e0a1c08f9f201e5ef1ecbb9a235718370007bbb69d9f8625103f97ab2868a000403350503c754f2ae984b0b088e93ec6626d1
7
- data.tar.gz: 2377e14dd62b12f9e3640781b3be5561a0d6ef0e84301a4d2b52c49928ec2d9f89ac9b510ecb9e467527b34a2bd26c51cf18c132b739ead39e25aae2ba21c663
6
+ metadata.gz: 1ece0c93d76158c3192290bb0eecbcfe09b82b71e23035b8a6616967a3fddd2af174956d72edde13fb9df3bc00fd579d8195c2115ca8586f51dbad0a8771e03d
7
+ data.tar.gz: ac79381bda71677bd9a0fed4935f3d167b1a840f51ef4656df951c2563a0fe4f81d561cd4d61c5420a13ef375672c596a05baf258dfab1bda3205f91446cf711
data/README.md CHANGED
@@ -1,39 +1,100 @@
1
- # RateLimiter
1
+ # Ruby Rate Limiter
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ `ruby_rate_limiter` is a Ruby gem that implements a rate limiting mechanism using the Token Bucket algorithm. This gem allows you to control the rate of requests from users or clients, which can be useful for API rate limiting, request throttling, and more.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rate_limiter`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ ## Features
6
6
 
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
12
-
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
-
15
- If bundler is not being used to manage dependencies, install the gem by executing:
7
+ - Token Bucket Algorithm: A flexible and efficient algorithm for rate limiting.
8
+ - Redis Backend: Utilizes Redis for token storage and management.
9
+ - Customizable Parameters: Configure the bucket size and refill rate to meet your needs.
10
+ - Supports Time Travel: For easier testing with time manipulation.
16
11
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
12
+ ## Installation
22
13
 
23
- ## Development
14
+ Add `ruby_rate_limiter` to your application's Gemfile:
24
15
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
16
+ ```ruby
17
+ gem 'ruby_rate_limiter'
26
18
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
19
+ ```
28
20
 
29
- ## Contributing
21
+ Then run:
30
22
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rate_limiter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rate_limiter/blob/main/CODE_OF_CONDUCT.md).
23
+ ```ruby
24
+ bundle install
25
+ ```
32
26
 
33
- ## License
27
+ Alternatively, you can install the gem directly using:
34
28
 
35
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
29
+ ```ruby
30
+ gem install ruby_rate_limiter
31
+ ```
36
32
 
37
- ## Code of Conduct
33
+ ## Usage
38
34
 
39
- Everyone interacting in the RateLimiter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rate_limiter/blob/main/CODE_OF_CONDUCT.md).
35
+ ### Configuration
36
+
37
+ First, configure the RateLimiter::TokenBucket with a unique identifier for each user and specify the storage backend (e.g., Redis).
38
+
39
+ Create a file in the initialize directory
40
+
41
+ ```ruby
42
+ # config/initializers/ruby_rate_limiter.rb
43
+ require 'ruby_rate_limiter'
44
+ require 'redis'
45
+
46
+ module RateLimiter
47
+ def self.bucket
48
+ @bucket ||= RubyRateLimiter::TokenBucket.new(
49
+ 'user123',
50
+ storage: RubyRateLimiter::Storage::RedisStorage.new(redis_client),
51
+ bucket_size: 10, # Optional: specify bucket size
52
+ refill_rate: 2 # Optional: specify refill rate (tokens per second)
53
+ )
54
+ end
55
+
56
+ private
57
+
58
+ def self.redis_client
59
+ @redis_client ||= Redis.new(url: 'redis://localhost:6379/1')
60
+ end
61
+ end
62
+
63
+ ```
64
+
65
+ ### Usage somewhere, in the controller or better still in a service
66
+
67
+ ```ruby
68
+ class SomeController < ApplicationController
69
+ def some_action
70
+ if RateLimiter.bucket.allow_request?
71
+ # Process the request
72
+ render json: { message: 'Request processed' }
73
+ else
74
+ # Rate limit exceeded
75
+ render status: 429, json: { error: 'Too many requests' }
76
+ end
77
+ end
78
+ end
79
+ ```
80
+
81
+ ```ruby
82
+ class SomeService
83
+ def perform
84
+ if RateLimiter.bucket.allow_request?
85
+ # Process the request
86
+ # Perform the service logic here
87
+ else
88
+ # Rate limit exceeded
89
+ raise 'Too many requests'
90
+ end
91
+ end
92
+ end
93
+
94
+ ```
95
+
96
+ Contributing
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Mutuba/ruby-rate-limiter.
98
+
99
+ License
100
+ The gem is available as open source under the terms of the MIT License.
@@ -1,3 +1,4 @@
1
+ # lib/ruby_rate_limiter/storage/abstract_storage.rb
1
2
  module RubyRateLimiter
2
3
  module Storage
3
4
  class AbstractStorage
@@ -1,11 +1,12 @@
1
+ # lib/ruby_rate_limiter/storage/redis_storage.rb
1
2
  require 'redis'
2
3
  require_relative 'abstract_storage'
3
4
 
4
5
  module RubyRateLimiter
5
6
  module Storage
6
7
  class RedisStorage < AbstractStorage
7
- def initialize
8
- @redis = Redis.new
8
+ def initialize(redis_client = Redis.new)
9
+ @redis = redis_client
9
10
  end
10
11
 
11
12
  def get(key)
@@ -1,3 +1,4 @@
1
+ # lib/ruby_rate_limiter/token_bucket.rb
1
2
  require 'forwardable'
2
3
  require_relative 'storage/abstract_storage'
3
4
  require_relative 'storage/redis_storage'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RateLimiter
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
-
2
+ # lib/ruby_rate_limiter.rb
3
3
  require_relative "ruby_rate_limiter/version"
4
+ require 'ruby_rate_limiter/token_bucket'
5
+ require 'ruby_rate_limiter/storage/abstract_storage'
6
+ require 'ruby_rate_limiter/storage/redis_storage'
7
+
4
8
 
5
9
  module RubyRateLimiter
6
10
  class Error < StandardError; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_rate_limiter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mutuba
@@ -37,13 +37,13 @@ files:
37
37
  - lib/ruby_rate_limiter/storage/redis_storage.rb
38
38
  - lib/ruby_rate_limiter/token_bucket.rb
39
39
  - lib/ruby_rate_limiter/version.rb
40
- homepage: https://github.com/Mutuba/rate-limiter
40
+ homepage: https://github.com/Mutuba/ruby-rate-limiter
41
41
  licenses:
42
42
  - MIT
43
43
  metadata:
44
44
  allowed_push_host: https://rubygems.org
45
- homepage_uri: https://github.com/Mutuba/rate-limiter
46
- changelog_uri: https://github.com/Mutuba/limiter/blob/main/CHANGELOG.md
45
+ homepage_uri: https://github.com/Mutuba/ruby-rate-limiter
46
+ changelog_uri: https://github.com/Mutuba/ruby-rate-limiter/blob/main/CHANGELOG.md
47
47
  post_install_message:
48
48
  rdoc_options: []
49
49
  require_paths: