https://github.com/peterzhu2118/rails_throttle
A simple library used to throttle code in your Rails application.
https://github.com/peterzhu2118/rails_throttle
rails ruby security
Last synced: 2 months ago
JSON representation
A simple library used to throttle code in your Rails application.
- Host: GitHub
- URL: https://github.com/peterzhu2118/rails_throttle
- Owner: peterzhu2118
- License: mit
- Created: 2019-04-23T15:12:20.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-07-22T03:08:30.000Z (almost 4 years ago)
- Last Synced: 2025-12-04T17:56:58.846Z (7 months ago)
- Topics: rails, ruby, security
- Language: Ruby
- Homepage: https://rubygems.org/gems/rails_throttle
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# RailsThrottle [](https://circleci.com/gh/peterzhu2118/rails_throttle/tree/master) [](https://badge.fury.io/rb/rails_throttle)
RailsThrottle is a simple library used to throttle code in your Rails application.
## Installation
Add this line to your application's Gemfile:
```ruby
gem "rails_throttle", "~> 0.2.0"
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install rails_throttle
## Usage
- See full documentation here: https://www.rubydoc.info/gems/rails_throttle/0.4.0/RailsThrottle/Throttle
- Example configuration (for production, you can configure other environments in a similar way):
```ruby
# In config/environments/production.rb
RailsThrottle.backend = ActiveSupport::Cache::RedisStore.new "localhost:6379/0"
# Alternatively, also supports any of the other caching strategies of ActiveSupport::Cache.
# For details, see https://api.rubyonrails.org/classes/ActiveSupport/Cache.html.
```
- Example usage:
```ruby
RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => 1
RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => 2
RailsThrottle::Throttle.decrement("foo", period: 2.seconds) # => 1
RailsThrottle::Throttle.reset("foo")
RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds, increment: 4) # => 4
RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => RailsThrottle::ThrottleError
RailsThrottle::Throttle.throttled?("foo", limit: 5) # => true
# ... wait 2 seconds ...
RailsThrottle::Throttle.throttled?("foo", limit: 5) # => false
```
## Development
Just the usual clone and `bundle` to install dependencies. Run `rake test` to run tests.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/peterzhu2118/rails_throttle.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).